#include /* Simple program demonstrating function calls and stack updates Use gdb to walk through the assembly code and see how the stack is updated. Useful commands for exploring the stack are: "info registers" - lists register contents (pay attention to eip, esp, ebp) "x /32xw $esp" - displays last 32 words added to the stack The last command is especially useful when stepping through the code line by line, and seeing how arguments are added and removed from the stack. For a review of how the stack should update, review the "Procedures and Stacks" lecture slides. */ int func(int a, int b) { return a+b; } int main(void) { int d = func(22,33); printf("%d\n", d); return 0; } /* You should see something like the following in gbd Dump of assembler code for function func: 0x00001faa push %ebp 0x00001fab > mov %esp,%ebp 0x00001fad > sub $0x8,%esp 0x00001fb0 > mov 0xc(%ebp),%eax 0x00001fb3 > add 0x8(%ebp),%eax 0x00001fb6 > leave 0x00001fb7 > ret End of assembler dump. This is what the call stack looks like by line func+3. %ebp+12a = 22 %ebp+8b = 33 %ebp+4return address = 0x00001fd9 %ebpold %ebp = 0xbffff338 %ebp-4 %ebp-8== %esp, from the instruction at func+3 above The return value a+b is placed into the %eax register. */