140 likes | 220 Views
Tutorial 1. Question 6 Done by Chuan Huey Ling. Assembly Code. Initial Stack Assumption. We assume that the base pointer of the caller is at 8000 and the stack pointer is at 7000 “.global g .type g,@function” refers to g is global and is a function type. pushl %ebp.
E N D
Tutorial 1 Question 6 Done by Chuan Huey Ling
Initial Stack Assumption • We assume that the base pointer of the caller is at 8000 and the stack pointer is at 7000 • “.global g .type g,@function” refers to g is global and is a function type.
pushl %ebp • The base pointer, also known as the frame pointer, is push onto the stack so that it can be loaded when the function returns to the caller.
movl %esp, %ebp • Change the value of ebp to the current esp.
subl $8, %esp • Subtract 8 bytes from esp to move downwards. The space is used to store local and temporary variables if any.
subl $12, %esp • Further subtract another 12 bytes of address from esp.
pushl $4 ……. pushl $0 • The arguments of the function are pushed in reverse order onto the stack.
call f • The return address of the next instruction which is “addl $32, %esp” is push to the stack.
How is the f value return • The return value from f is store in the %eax register (4 bytes) since the return value is an integer. • Normally return value more than 4 bytes will return the address of the value in the stack. • And char or short will be store in the AL and AX register respectively.
addl $32, %esp • Move the esp up 32 bytes to clear the arguments and clean the stack.
movl %eax, -4(%ebp) • The value in the eax are being push on to the stack 4 bytes below ebp.
leave & ret • What leave does is “movl %ebp, %esp” and “popl %ebp”. This is to move the esp back to the base pointer address and pop the caller %ebp value and load it into the base pointer register. From the previous example, ebp becomes 8000 again. • Return will pop the address of the next instruction of the caller.