2

I am doing this course on x86-64 architecture and we are using virtual studio for that purpose, whether it's debugging or anything. So we have been given a code segment which is as follows:

int func() {
    long long i = 0xf01dab1ef007ba11;
    long long j = 0x0b57ac1e5;
    return i+j;
}

int main() {
    return func();
}

Now when I used the debugger this is the assembly instruction
assembly listing of previous code
I got and here is the stack diagram
stack diagram
for the same. Now if we see in the stack diagram at the address 00000000'0014FDD0 there is an extra 8 byte of allocations. I found the reason in the Microsoft docs and it states that The stack will always be maintained 16-byte aligned, except within the prolog (for example, after the return the address is pushed)... There are few exceptions though.

  • Now here is another code segment
int func() {
    long long i = 0xf01dab1ef007ba11;
    long long j = 0x0b57ac1e5;
    long long k = 0x57abbadabad00;
    return i + j;
}

int main() {
    return func();
}

And again this is the assembly instruction
assembly listing of previous code
and here is the stack diagram
stack diagram
for it. Now I understand that why there is an extra 8 bytes of allocation at the address 00000000'0014FED0, which is to keep the stack 16-byte aligned.

But what I don't understand is why there is an extra allocation at the address `000000000'014FDC8? Is it necessary?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
heartbeat
  • 41
  • 1
  • 7
  • 3
    No, it's not necessary, you can eliminate that 16 byte padding or even the whole stack frame. I suspect it is only because optimization is disabled. – Jester May 24 '21 at 21:31
  • Related: GCC has similar missed-optimization bugs, even with optimization enabled: [Why does GCC allocate more space than necessary on the stack, beyond what's needed for alignment?](https://stackoverflow.com/q/63009070) – Peter Cordes May 25 '21 at 21:18

0 Answers0