I've been a bit stuck on this question. Given the following C code:
#include <stdio.h>
#define BUF_SIZE 13
int foo(){
int i;
int B[BUF_SIZE];
for(i = 0; i < BUF_SIZE; i )
B[i] = 5;
return i;
}
int main(){
foo();
return 0;
}
The following Intel-x86 assembly is generated:
1. .file "code.c"
2. .intel_syntax noprefix
3. .text
4. .globl foo
5. .type foo, @function
6. foo:
7. push ebp
8. mov ebp, esp
9. sub esp, 64
10. mov DWORD PTR [ebp-4], 0
11. jmp .L2
12. .L3:
13. mov eax, DWORD PTR [ebp-4]
14. mov DWORD PTR [ebp-56 eax*4], 5
15. add DWORD PTR [ebp-4], 1
16. .L2:
17. cmp DWORD PTR [ebp-4], 12
18. jle .L3
19. mov eax, DWORD PTR [ebp-4]
20. leave
21. ret
22. .size foo, .-foo
23. .globl main
24. .type main, @function
25. main:
26. push ebp
27. mov ebp, esp
28. call foo
29. mov eax, 0
30. pop ebp
31. ret
32. .size main, .-main
33. .ident "GCC: (Debian 6.3.0-18 deb9u1) 6.3.0 20170516"
34. .section .note.GNU-stack,"",@progbits
I'm a bit stuck trying to determine the meaning of line 9 in the assembly. My understanding is that we subtract from the stack register in order to allocate space on the stack for local variables. I know, then, that 52 bytes are being subtracted for the array B, and another 4 bytes for i. But I'm wondering where the other 8 bytes come from? Are those the return values of foo and main? Any help would be appreciated.
CodePudding user response:
The amount of bytes added onto esp is rounded up to maintain some stack alignment. Imagine, you would only add 57 or something. A function you would call, would then need to realign the stack pointer first before storing a 4-byte integer. Everyone is saved that hassle if everyone keeps the stack aligned.
