The following is the setup code for swapadd Recall that the call instruction

The following is the setup code for swapadd recall

This preview shows page 158 - 161 out of 783 pages.

The following is the setup code for swap_add . Recall that the call instruction will already push the return address on the stack. Setup code in swap_add 1 swap_add: 2 pushl %ebp Save old %ebp 3 movl %esp,%ebp Set %ebp as frame pointer 4 pushl %ebx Save %ebx Procedure swap_add requires register %ebx for temporary storage. Since this is a callee save register, it pushes the old value on the stack as part of the stack frame setup. The following is the body code for swap_add : Body code in swap_add 1 movl 8(%ebp),%edx Get xp 2 movl 12(%ebp),%ecx Get yp 3 movl (%edx),%ebx Get x 4 movl (%ecx),%eax Get y 5 movl %eax,(%edx) Store y at *xp 6 movl %ebx,(%ecx) Store x at *yp 7 addl %ebx,%eax Set return value = x+y This code retrieves its arguments from the stack frame for caller . Since the frame pointer has shifted, the locations of these arguments has shifted from positions and relative to the old value of %ebp to positions and relative to new value of %ebp . Observe that the sum of variables x and y is stored in register %eax to be passed as the returned value. The following is the finishing code for swap_add : Finishing code in swap_add 1 popl %ebx Restore %ebx 2 movl %ebp,%esp Restore %esp 3 popl %ebp Restore %ebp 4 ret Return to caller
Image of page 158
3.7. PROCEDURES 139 This code simply restores the values of the three registers %ebx , %esp , and %ebp , and then executes the ret instruction. Note that instructions F2 and F3 could be replaced by a single leave instruction. Different versions of GCC seem to have different preferences in this regard. The following code in caller comes immediately after the instruction calling swap_add : 1 movl %eax,%edx Resume here Upon return from swap_add , procedure caller will resume execution with this instruction. Observe that this instruction copies the return value from %eax to a different register. Practice Problem 3.16 : Given the following C function: 1 int proc(void) 2 { 3 int x,y; 4 scanf("%x %x", &y, &x); 5 return x-y; 6 } GCC generates the following assembly code 1 proc: 2 pushl %ebp 3 movl %esp,%ebp 4 subl $24,%esp 5 addl $-4,%esp 6 leal -4(%ebp),%eax 7 pushl %eax 8 leal -8(%ebp),%eax 9 pushl %eax 10 pushl $.LC0 Pointer to string "%x %x" 11 call scanf Diagram stack frame at this point 12 movl -8(%ebp),%eax 13 movl -4(%ebp),%edx 14 subl %eax,%edx 15 movl %edx,%eax 16 movl %ebp,%esp 17 popl %ebp 18 ret Assume that procedure proc starts executing with the following register values: Register Value %esp 0x800040 %ebp 0x800060
Image of page 159
140 CHAPTER 3. MACHINE-LEVEL REPRESENTATION OF C PROGRAMS code/asm/fib.c 1 int fib_rec(int n) 2 { 3 int prev_val, val; 4 5 if (n <= 2) 6 return 1; 7 prev_val = fib_rec(n-2); 8 val = fib_rec(n-1); 9 return prev_val + val; 10 } code/asm/fib.c Figure 3.19: C Code for Recursive Fibonacci Program. Suppose proc calls scanf (line 12), and that scanf reads values 0x46 and 0x53 from the standard input. Assume that the string "%x %x" is stored at memory location 0x300070 .
Image of page 160
Image of page 161

You've reached the end of your free preview.

Want to read all 783 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes