0

Let's take the following example I have from a single function:

first_function:
    pushq   %rbp
    movq    %rsp, %rbp
    movq $2, -8(%rbp)
    movq $4, -16(%rbp)
    ...
    pop %rbp
    ret

If we look at the stack before the ..., it gives us:

>>> x/4g $rbp-16

0x7fffffffe410: 0x0000000000000004  0x0000000000000002
0x7fffffffe420: 0x0000000000000000  0x00000000004000bd

Or for me, an easier way to visualize it is:

+----------------+--------------------+---------------------------+
| 0x7fffffffe420 | 0x00000000004000bd | # function return address |
+----------------+--------------------+---------------------------+
| 0x7fffffffe418 | 0x0000000000000000 | # from push %rbp          |
+----------------+--------------------+---------------------------+
| 0x7fffffffe410 | 0x0000000000000002 | # from mov $2, -8(%rbp)   |
+----------------+--------------------+---------------------------+
| 0x7fffffffe408 | 0x0000000000000004 | # from mov $4, -16(%rbp)  |
+----------------+--------------------+--------------------------

My question then is wouldn't a sub-function call (for example, if I called another function call in the ... section) possibly clobber all the two variables I've added above (2, and 4)?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
David542
  • 104,438
  • 178
  • 489
  • 842
  • 3
    The compiler knows there isn’t a function call within this function. If there were, the compiler would generate different code. – prl Aug 30 '20 at 07:11
  • Studying compiler output is a good way to learn assembly language, but you should always enable optimization, because otherwise the compiler generates very misleading code. – prl Aug 30 '20 at 07:14

0 Answers0