15213 Recitation Section CLast Week’s Final ExampleWrite CommentsName the variablesLoopC CodeStack BasicsFunction Stack FramesMaking a CallExample 1ASM of main()Stack at Point 1Stack at Point 2Stack at Point 3Write Commentsmain()Slide 17Example 2: RecursionExample 2 Cont’dStack FrameWrite Comments For BodySlide 22Stack Changes of example_2(3)Slide 24ArraysExample 3 Write C CodeSlide 27Slide 28Loop + ArrayStruct and Linked ListExample 4: Write C CodeSlide 32Understand the LoopName the VariablesSlide 35Slide 3615213 Recitation Section C• Last week’s exercise• Function and stack• Array• Struct and linked-listShimin ChenSept. 23, 2002Outline215213 Recitation C Shimin ChenLast Week’s Final Exampleint func5(int x){ ??? }0x80483c0 push %ebp0x80483c1 mov %esp,%ebp0x80483c3 mov 0x8(%ebp),%ecx0x80483c6 xor %eax,%eax0x80483c8 xor %edx,%edx0x80483ca cmp %ecx,%edx0x80483cc jge 0x80483d70x80483ce mov %esi,%esi0x80483d0 add %edx,%eax0x80483d2 inc %edx0x80483d3 cmp %ecx,%edx0x80483d5 jl 0x80483d00x80483d7 mov %ebp,%esp0x80483d9 pop %ebp0x80483da ret Body315213 Recitation C Shimin ChenWrite Comments0x80483c3 mov 0x8(%ebp),%ecx0x80483c6 xor %eax,%eax0x80483c8 xor %edx,%edx0x80483ca cmp %ecx,%edx0x80483cc jge 0x80483d70x80483ce mov %esi,%esi0x80483d0 add %edx,%eax0x80483d2 inc %edx0x80483d3 cmp %ecx,%edx0x80483d5 jl 0x80483d00x80483d7 ……ecx = xeax = 0edx = 0if (edx>=x) goto L1nopL2:eax += edxedx ++if (edx<x) goto L2L1:int func5(int x){ ??? }415213 Recitation C Shimin ChenName the variables•eax– result, edx--i0x80483c3 mov 0x8(%ebp),%ecx0x80483c6 xor %eax,%eax0x80483c8 xor %edx,%edx0x80483ca cmp %ecx,%edx0x80483cc jge 0x80483d70x80483ce mov %esi,%esi0x80483d0 add %edx,%eax0x80483d2 inc %edx0x80483d3 cmp %ecx,%edx0x80483d5 jl 0x80483d00x80483d7 ……ecx = x;result = 0;i = 0;if (i>=x) goto L1;L2:result += i; i++;if (i<x) goto L2;L1:515213 Recitation C Shimin ChenLoopresult = 0;i = 0;if (i>=x) goto L1;L2:result += i; i++;if (i<x) goto L2;L1: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;615213 Recitation C Shimin ChenC Codeint func5(int x){ int result=0; int i; for (i=0; i<x; i++) result += i; return result;}715213 Recitation C Shimin ChenStack BasicsStack Pointer%espStack GrowsDownDecreasingAddressesStack “Top”•push–decrement %esp–then places value•pop–get value–then increment %esp815213 Recitation C Shimin ChenFunction Stack FramesStack Pointer(%esp)Frame Pointer(%ebp)Return AddrSavedRegisters+LocalVariablesArgumentBuildOld %ebpArgumentsCallerFrame•A caller function calls a callee function915213 Recitation C Shimin ChenMaking a Call%esp%ebpReturn AddrSavedRegisters+LocalVariablesArgumentBuildOld %ebpArguments•Caller:–“push” arguments (in what order?)–“call”: put return address onto stack, jump to the start of callee function•Callee:–save (caller’s) %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”1015213 Recitation C Shimin ChenExample 10x80483e4 push %ebp0x80483e5 mov %esp,%ebp0x80483e7 mov 0xc(%ebp),%eax0x80483ea add 0x8(%ebp),%eax0x80483ed mov %ebp,%esp0x80483ef pop %ebp0x80483f0 ret•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)2.Stack?1115213 Recitation C Shimin ChenASM of main()0x80483f4 push %ebp0x80483f5 mov %esp,%ebp0x80483f7 sub $0x8,%esp0x80483fa add $0xfffffff8,%esp0x80483fd push $0x20x80483ff push $0x10x8048401 call 0x80483e4 <example_1>0x8048406 add $0xfffffff8,%esp0x8048409 push %eax0x804840a push $0x80484780x804840f call 0x8048308 <printf>0x8048414 xor %eax,%eax0x8048416 mov %ebp,%esp0x8048418 pop %ebp0x8048419 ret3.Stack?1.Stack?1215213 Recitation C Shimin ChenStack at Point 1<main>0x80483f4 push %ebp0x80483f5 mov %esp,%ebp0x80483f7 sub $0x8,%esp0x80483fa add $0xfffffff8,%esp0x80483fd push $0x20x80483ff push $0x10x8048401 call 0x80483e4 <example_1>0x8048406 …………old %ebp21%esp%ebp1315213 Recitation C Shimin ChenStack at Point 2<example_2>0x80483e4 push %ebp0x80483e5 mov %esp,%ebp0x80483e7 mov 0xc(%ebp),%eax0x80483ea add 0x8(%ebp),%eax0x80483ed mov %ebp,%esp0x80483ef pop %ebp0x80483f0 retold %ebp21%esp%ebp0x8048406main %ebp1415213 Recitation C Shimin ChenStack at Point 3<main>0x80483ff ………0x8048401 call 0x80483e4 <example_1>0x8048406 …………<example_2>0x80483e4 push %ebp0x80483e5 mov %esp,%ebp0x80483e7 mov 0xc(%ebp),%eax0x80483ea add 0x8(%ebp),%eax0x80483ed mov %ebp,%esp0x80483ef pop %ebp0x80483f0 retold %ebp21%ebp0x8048406main %ebp%esp1515213 Recitation C Shimin ChenWrite Comments 0x80483e4 push %ebp0x80483e5 mov %esp,%ebp0x80483e7 mov 0xc(%ebp),%eax0x80483ea add 0x8(%ebp),%eax0x80483ed mov %ebp,%esp0x80483ef pop %ebp0x80483f0 retint example_1 (int x, int y)eax=yeax+=x1615213 Recitation C Shimin Chenmain()0x80483f4 push %ebp0x80483f5 mov %esp,%ebp0x80483f7 sub $0x8,%esp0x80483fa add $0xfffffff8,%esp0x80483fd push $0x20x80483ff push $0x10x8048401 call 0x80483e4 <example_1>0x8048406 add $0xfffffff8,%esp0x8048409 push %eax0x804840a push $0x80484780x804840f call 0x8048308 <printf>0x8048414 xor %eax,%eax0x8048416 mov %ebp,%esp0x8048418 pop %ebp0x8048419 retexample_1(1,2)printf(“%d\n”,result_example_1)return 0;1715213 Recitation C Shimin ChenC Codeint example_1 (int x, int y){ return x+y;}int main (){ int result; result = example_1 (1, 2); printf ("%d\n", result); return 0;}1815213 Recitation C Shimin ChenExample 2: Recursion0x8048420 push %ebp0x8048421 mov %esp,%ebp0x8048423 sub $0x10,%esp0x8048426 push %esi0x8048427 push %ebx0x8048428 mov 0x8(%ebp),%ebx0x804842b cmp $0x2,%ebx0x804842e jg 0x80484370x8048430 mov $0x1,%eax0x8048435 jmp 0x8048453•Please write C code for the assembly
View Full Document