previous | start | next

Problem 3.8

The shift arithmetic right instruction is

      sarl k, x ;   x = x >> k
   

But note: k should be a value 0 to 31 and must be either an immediate value or the register %cl (lower order byte of %ecx).

Practice Problem 3.8:
Suppose we want to generate assembly code for the following C
    function:
int shift_left2_rightn(int x, int n)
{
  x <<= 2;
  x >>= n;
  return x;
}
   

The following is a portion of the assembly code that performs the actual shifts and leaves the final value in register %eax.

Two key instructions have been omitted. Parameters x and n are stored at memory locations with offsets 8 and 12, respectively, relative to the address in register %ebp.

1 movl 12(%ebp),%ecx   ; Get x
2 movl 8(%ebp),%eax    ; Get n
3 _____________        ; x <<= 2
4 _____________        ; x >>= n
   

Fill in the missing instructions, following the annotations on the right. The right shift should be performed arithmetically.



previous | start | next