Interpret local variables

Alo

After I read about the function and stack from http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames I have a question about local variables.

Snapshot from the article:


push ebp ; save the value of ebp mov ebp, esp ; ebp now points to the top of the stack sub esp, 12 ; space allocated on the stack for the local variables 

This means that access to local variables can be obtained by referring to ebp. Consider the following C code snippet and associated build code:

 a = 10; b = 5; c = 2; mov [ebp - 4], 10 ; location of variable a mov [ebp - 8], 5 ; location of b mov [ebp - 12], 2 ; location of c 

Remember that clicking basically does the following:

 sub esp, 4 ; "allocate" space for the new stack item mov [esp], X ; put new stack item value X in 


Why local variables are not pushed onto the stack as follows:
 push 10 push 5 push 2 

instead

 sub esp, 12 mov [ebp - 4], 10 ; location of variable a mov [ebp - 8], 5 ; location of b mov [ebp - 12], 2 ; location of c 
+6
assembly
source share
2 answers

This is more a matter of semantics rather than technical correctness: push and pop are used to save and restore registers or values; but providing local variables for the function does not match this regular push / pop target. So, the stack is manually controlled here (except for push ebp and pop ebp , because here we really want to save and restore ebp in the true sense of push / pop ).

+1
source share

Practically speaking, if you know how much stack space you need and reserve in one operation, you can then use the mov command, which is faster than pressing it immediately (especially now, when the offset calculation has dedicated equipment). It is also possible that the legacy component is due to the fact that push did not immediately become available on x86 until the release of the 80186/80188 processors. By then, the sub / mov convention had become a well-established pattern.

+1
source share

All Articles