Question: The C code given below produces the following x86 assembly. Draw the stack frames for each of the following, showing the relevant stack contents and
The C code given below produces the following x86 assembly. Draw the stack frames for each of the following, showing the relevant stack contents and the addresses (write the addresses in relation to ebp, for example, ebp, ebp -4, etc.)
a) Stack frame of main at calll _f
b) Stack frame of function f at calll _g
c) Stack frame of function g after movl -4(%ebp), %eax
C Code:
int g(int x) { return x+3;}
int f(int x) { return g(x);}
int main() { return f(8);}
C Code and Assembly Equivalent:
int gCint x) return x+3;) int f(int x) return g(x);) int main) return f(8); pushl %ebp movl %esp, %ebp pushl %eax movl 8(%ebp), %eax movl %eax,-4(%ebp) movl -4(%ebp), %eax addl S3, %eax addl S4, %esp popl %ebp pushl %ebp movl %esp, %ebp subi S8. Siesp movi 8(%ebp), %eax movl %eax,-4(%ebp) movl -4(%ebp), %eax movi %eax, (%esp) call g addl S8, %esp popl %ebp pushl %ebp movl %esp, %ebp sub S24, %esp movi S8, %eax movi ss,(%esp) movl %eax,-8(%ebp) calll f addl S24,%esp popl %ebp int gCint x) return x+3;) int f(int x) return g(x);) int main) return f(8); pushl %ebp movl %esp, %ebp pushl %eax movl 8(%ebp), %eax movl %eax,-4(%ebp) movl -4(%ebp), %eax addl S3, %eax addl S4, %esp popl %ebp pushl %ebp movl %esp, %ebp subi S8. Siesp movi 8(%ebp), %eax movl %eax,-4(%ebp) movl -4(%ebp), %eax movi %eax, (%esp) call g addl S8, %esp popl %ebp pushl %ebp movl %esp, %ebp sub S24, %esp movi S8, %eax movi ss,(%esp) movl %eax,-8(%ebp) calll f addl S24,%esp popl %ebp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
