The code below is generated by gcc from a simple scanf program. My question is that
- Why these 3 addresses of variables are not consecutive when allocated?
- If not, when could I speculate the number of variables generated from stack by watching the
clause like
add esp, N
which is often at the end of a routine? Is it related with calling convention? - In this example, why compiler does not generated the
add esp, 20h
with it?
C code
#include <stdio.h>
int main() {
int x;
printf ("Enter X:\n");
scanf ("%d", &x);
printf ("You entered %d...\n", x);
return 0;
};
asm
main proc near
var_20 = dword ptr -20h
var_1C = dword ptr -1Ch
var_4 = dword ptr -4
push ebp
mov ebp, esp
and esp, 0FFFFFFF0h
sub esp, 20h
mov [esp+20h+var_20], offset aEnterX ; "Enter X:"
call _puts
mov eax, offset aD ; "%d"
lea edx, [esp+20h+var_4]
mov [esp+20h+var_1C], edx
mov [esp+20h+var_20], eax
call ___isoc99_scanf
mov edx, [esp+20h+var_4]
mov eax, off set aYouEnteredD___ ; "You entered %d...\n"
mov [esp+20h+var_1C], edx
mov [esp+20h+var_20], eax
call _printf
mov eax, 0
leave
retn
main endp