%ebp is saved.
Let us resume our example from the previous section. We add the following local variables:
strBuffer: an array of 32 ASCII characters
strPtr: a 32-bit pointer
cmpChar: a single character, but 16 bits are allocated
These local variables will, then, have the following symbolic definitions:
strBuffer = oldbp-32 # offset from %ebp to strBuffer
strPtr = strBuffer-4 # offset from %ebp to strPtr
cmpChar = strPtr - 2 # offset from %ebp to cmpChar
Unless these local variables should be initialized, the allocation
is a simple addl instruction. Let's look at the complete entry
code of the subroutine:
sub1:
pushl %ebp # save the old value of ebp
movl %esp,%ebp # initialize this frame
addl $cmpChar,%esp # adjust stack pointer to reserve for local var
Note that addl is used instead of subl because
cmpChar is already a negative value.
To clean up the stack right before this subroutine returns, we need the following code:
movl %ebp,%esp # deallocate local var
popl %ebp # restore old value of ebp
ret # time to return
Copyright © 2009-04-16 by Tak Auyeung