15213 Recitation Section C Shimin Chen Sept 23 2002 Outline Last week s exercise Function and stack Array Struct and linked list Last Week s Final Example int func5 int x 0x80483c0 0x80483c1 0x80483c3 0x80483c6 0x80483c8 0x80483ca 0x80483cc 0x80483ce 0x80483d0 0x80483d2 0x80483d3 0x80483d5 0x80483d7 0x80483d9 0x80483da 15213 Recitation C push mov mov xor xor cmp jge mov add inc cmp jl mov pop ret ebp esp ebp 0x8 ebp ecx eax eax edx edx ecx edx 0x80483d7 esi esi edx eax edx ecx edx 0x80483d0 ebp esp ebp 2 Body Shimin Chen Write Comments int func5 int x 0x80483c3 0x80483c6 0x80483c8 0x80483ca 0x80483cc 0x80483ce 0x80483d0 0x80483d2 0x80483d3 0x80483d5 0x80483d7 15213 Recitation C mov xor xor cmp jge mov add inc cmp jl 0x8 ebp ecx eax eax edx edx ecx edx 0x80483d7 esi esi edx eax edx ecx edx 0x80483d0 3 ecx x eax 0 edx 0 if edx x goto L1 nop L2 eax edx edx if edx x goto L2 L1 Shimin Chen Name the variables eax result edx i 0x80483c3 0x80483c6 0x80483c8 0x80483ca 0x80483cc 0x80483ce 0x80483d0 0x80483d2 0x80483d3 0x80483d5 0x80483d7 15213 Recitation C mov xor xor cmp jge mov add inc cmp jl 0x8 ebp ecx eax eax edx edx ecx edx 0x80483d7 esi esi edx eax edx ecx edx 0x80483d0 4 ecx x result 0 i 0 if i x goto L1 L2 result i i if i x goto L2 L1 Shimin Chen Loop result 0 i 0 if i x goto L1 L2 result i i if i x goto L2 L1 15213 Recitation C result 0 i 0 if i x goto L1 do result i i while i x L1 result 0 i 0 While i x result i i result 0 for i 0 i x i result i 5 Shimin Chen C Code int func5 int x int result 0 int i for i 0 i x i result i return result 15213 Recitation C 6 Shimin Chen Stack Basics push Decreasing Addresses decrement esp then places value pop get value then increment esp Stack Pointer esp 15213 Recitation C 7 Stack Grows Down Stack Top Shimin Chen Function Stack Frames A caller function calls a callee function Caller Frame Arguments Frame Pointer ebp Return Addr Old ebp Saved Registers Local Variables Stack Pointer esp 15213 Recitation C 8 Argument Build Shimin Chen Making a Call Caller push arguments in what order call put return address onto stack jump to the start of callee function Callee save caller s ebp eb p set up stack frame save callee saved registers if want to use ebx esi edi put return value in eax restore ebp and esp ret to jump to the Return Addr esp 15213 Recitation C 9 Arguments Return Addr Old ebp Saved Registers Local Variables Argument Build Shimin Chen Example 1 Please draw the stack at the marked points Write C code for the assembly code gdb x s 0x8048478 0x8048478 IO stdin used 4 d n int example 1 int x int y 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 15213 Recitation C push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 10 2 Stack Shimin Chen ASM of main 0x80483f4 0x80483f5 0x80483f7 0x80483fa 0x80483fd 0x80483ff 0x8048401 0x8048406 0x8048409 0x804840a 0x804840f 0x8048414 0x8048416 0x8048418 0x8048419 15213 Recitation C push mov sub add push push call add push push call xor mov pop ret ebp esp ebp 0x8 esp 0xfffffff8 esp 0x2 0x1 0x80483e4 example 1 0xfffffff8 esp eax 0x8048478 0x8048308 printf eax eax ebp esp ebp 11 1 Stack 3 Stack Shimin Chen Stack at Point 1 main 0x80483f4 push 0x80483f5 mov 0x80483f7 sub 0x80483fa add 0x80483fd push 0x80483ff push 0x8048401 call example 1 0x8048406 15213 Recitation C ebp esp ebp 0x8 esp 0xfffffff8 esp 0x2 0x1 0x80483e4 ebp 2 esp 12 old ebp 1 Shimin Chen Stack at Point 2 old ebp example 2 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 2 1 0x8048406 ebp esp 15213 Recitation C 13 main ebp Shimin Chen Stack at Point 3 main 0x80483ff 0x8048401 0x8048406 example 2 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 15213 Recitation C call 0x80483e4 example 1 push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 14 ebp old ebp 2 esp 1 0x8048406 main ebp Shimin Chen Write Comments int example 1 int x int y 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 15213 Recitation C push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 15 eax y eax x Shimin Chen main 0x80483f4 0x80483f5 0x80483f7 0x80483fa 0x80483fd 0x80483ff 0x8048401 0x8048406 0x8048409 0x804840a 0x804840f 0x8048414 0x8048416 0x8048418 0x8048419 15213 Recitation C push mov sub add push push call add push push call xor mov pop ret ebp esp ebp 0x8 esp 0xfffffff8 esp 0x2 0x1 0x80483e4 example 1 0xfffffff8 esp eax 0x8048478 0x8048308 printf eax eax ebp esp ebp 16 example 1 1 2 printf d n result example 1 return 0 Shimin Chen C Code int example 1 int x int y return x y int main int result result example 1 1 2 printf d n result return 0 15213 Recitation C 17 Shimin Chen Example 2 Recursion Please write C code for the assembly code Draw the stack changes of calling example 2 3 int example 2 int x 0x8048420 0x8048421 0x8048423 0x8048426 0x8048427 0x8048428 0x804842b 0x804842e 0x8048430 0x8048435 15213 Recitation C push mov sub push push mov cmp jg mov jmp ebp esp ebp 0x10 esp esi ebx 0x8 ebp ebx 0x2 ebx 0x8048437 0x1 eax 0x8048453 18 Shimin Chen Example 2 Cont d 0x8048437 0x804843a 0x804843d 0x804843e 0x8048443 0x8048445 0x8048448 0x804844b 0x804844c 0x8048451 0x8048453 0x8048456 0x8048457 0x8048458 0x804845a 0x804845b 15213 Recitation C add lea push call mov add lea push call add lea pop pop mov pop ret 0xfffffff4 esp 0xfffffffe ebx eax eax 0x8048420 example 2 eax esi 0xfffffff4 esp 0xffffffff ebx eax eax 0x8048420 example 2 esi eax 0xffffffe8 ebp esp ebx esi ebp esp ebp 19 Shimin Chen Stack Frame example 2 0x8048420 0x8048421 0x8048423 0x8048426 0x8048427 push mov sub push push ebp esp ebp 0x10 esp esi ebx lea pop pop mov pop ret 0xffffffe8 ebp esp ebx esi ebp esp ebp x rtrn addr ebp old ebp 0x8048453 0x8048456 0x8048457 0x8048458 0x804845a 0x804845b 15213 Recitation C 20 old esi esp old ebx Shimin Chen Write Comments For Body 0x8048428 0x804842b 0x804842e 0x8048430 0x8048435 0x8048437 0x804843a 0x804843d 0x804843e 0x8048443 0x8048445 0x8048448 0x804844b 0x804844c 0x8048451 0x8048453 15213 Recitation C mov cmp jg mov jmp add lea push call mov add lea push call add 0x8 ebp ebx 0x2 ebx 0x8048437 0x1 eax 0x8048453 0xfffffff4 esp 0xfffffffe ebx eax eax 0x8048420 example 2 eax esi 0xfffffff4 esp 0xffffffff ebx eax eax 0x8048420 example 2 esi eax 21 ebx x if x 2 goto L1 eax 1 goto L2 L1 push x 2 example 2 esi eax push x 1 example 2 eax esi L2
View Full Document