I saw that the stack is aligned by 4 bytes on x86-32 and 8 bytes on x86-64. but how does this alignment work? For example in the code below, it has the following output
#include<stdio.h>
int main()
{
short x = 2;
short y = 4;
return 0;
}
Debugging I have the following output
(gdb) disas main
Dump of assembler code for function main:
0x0000555555554660 <+0>: push rbp
0x0000555555554661 <+1>: mov rbp,rsp
0x0000555555554664 <+4>: mov WORD PTR [rbp-0x2],0x2
0x000055555555466a <+10>: mov WORD PTR [rbp-0x4],0x4
=> 0x0000555555554670 <+16>: mov eax,0x0
0x0000555555554675 <+21>: pop rbp
0x0000555555554676 <+22>: ret
End of assembler dump.
(gdb) i r rbp
rsp 0x7fffffffe710 0x7fffffffe710
(gdb) x/xb $rbp - 0x2
0x7fffffffe70e: 0x02
Notice that the initial address of rbp (0x7fffffffe710) has the binary value 11111111111111111111111111111111110011100010000, with 4 lower bits zeroed 2 ^ 4 = 16, does this mean that the alignment is 16 bytes? Already at the address of variable x (0x7fffffffe70e) it has the binary value 11111111111111111111111111111111110011100001110, it has 1 lower bit reset 2 ^ 1 = 2, after all, what is the stack alignment? Would it be the starting address of rbp?