15213 Recitation Section C Last Week s Final Example int func5 int x 0x80483c0 0x80483c1 0x80483c3 0x80483c6 0x80483c8 0x80483ca 0x80483cc 0x80483ce 0x80483d0 0x80483d2 0x80483d3 0x80483d5 0x80483d7 0x80483d9 0x80483da Shimin Chen Sept 23 2002 Outline Last week s exercise Function and stack Array Struct and linked list 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 15213 Recitation C Write Comments 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 2 Shimin Chen Name the variables int func5 int x 0x80483c3 0x80483c6 0x80483c8 0x80483ca 0x80483cc 0x80483ce 0x80483d0 0x80483d2 0x80483d3 0x80483d5 0x80483d7 Body eax result edx i ecx x eax 0 edx 0 if edx x goto L1 nop L2 eax edx edx if edx x goto L2 L1 Shimin Chen 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 1 Loop result 0 i 0 if i x goto L1 L2 result i i if i x goto L2 L1 C Code 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 15213 Recitation C 5 int func5 int x int result 0 int i for i 0 i x i result i return result Shimin Chen 15213 Recitation C Stack Basics Decreasing Addresses A caller function calls a callee function Caller Frame Arguments pop get value then increment esp Shimin Chen Function Stack Frames push decrement esp then places value 6 Frame Pointer ebp Stack Grows Down Saved Registers Local Variables Stack Pointer esp Stack Top 15213 Recitation C 7 Shimin Chen Return Addr Old ebp Stack Pointer esp 15213 Recitation C 8 Argument Build Shimin Chen 2 Making a Call Example 1 Caller push arguments in what order call put return address onto stack jump to the start of callee function Callee save caller s ebp ebp 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 Please draw the stack at the marked points Write C code for the assembly code Arguments gdb x s 0x8048478 Return Addr Old ebp 0x8048478 IO stdin used 4 d n Saved Registers Local Variables Argument Build Shimin Chen int example 1 int x int y 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 15213 Recitation C 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 push mov mov add mov pop ret ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 10 2 Stack Shimin Chen Stack at Point 1 1 Stack 3 Stack Shimin Chen 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 3 Stack at Point 2 Stack at Point 3 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 main 0x80483ff 0x8048401 0x8048406 example 2 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 call 0x80483e4 example 1 push mov mov add mov pop ret 15213 Recitation C 15213 Recitation C ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp 15 esp 1 0x8048406 main ebp Shimin Chen main int example 1 int x int y push mov mov add mov pop ret old ebp 2 14 Write Comments 0x80483e4 0x80483e5 0x80483e7 0x80483ea 0x80483ed 0x80483ef 0x80483f0 ebp esp ebp 0xc ebp eax 0x8 ebp eax ebp esp ebp ebp eax y eax x Shimin Chen 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 4 C Code Example 2 Recursion Please write C code for the assembly code int example 1 int x int y return x y Draw the stack changes of calling example 2 3 int example 2 int x int main int result 0x8048420 0x8048421 0x8048423 0x8048426 0x8048427 0x8048428 0x804842b 0x804842e 0x8048430 0x8048435 result example 1 1 2 printf d n result return 0 15213 Recitation C 17 Shimin Chen push mov sub push push mov cmp jg mov jmp 15213 Recitation C 18 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 Shimin Chen Stack Frame 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 ebp esp ebp 0x10 esp esi ebx 0x8 ebp ebx 0x2 ebx 0x8048437 0x1 eax 0x8048453 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 Shimin Chen 15213 Recitation C 20 old esi esp old ebx Shimin Chen 5 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 C Code ebx x if x 2 goto L1 eax 1 goto L2 L1 if n 2 result 1 else result example 2 n 2
View Full Document