HOMEWORK #3 CSE 410 Fall 98  

Due: Wednesday October 21, in class


1. [PH] Exercise 3.19 (pp.201-203)


2. Assume a machine with 2-address instructions and several general purpose registers. Each register and memory cell has 32 bits, numbered from 0 (least significant) to 31 (most significant). The machine has a
                        move    x, y
instruction, where x and y could refer to either registers or main memory locations, as well as a standard set of logical and shift instructions. Operands for these could refer to registers or memory, or could be constants or "immediate". For example,
                and     R1, 0xFFF       ;  R1 := R1 and 0xFFF
                or      R1, R2          ;  R1 := R1 or R2 
                rshift  R1, 13          ;  R1 := R1 rshift 13
a) Suppose that a register R1 contains a 6 bit unsigned number in bits 7 though 12. Write a sequence of assembly language instructions that will store these 6 bits in the low order bits of another register R2 and set the high order bits of R2 to zero. R1 should remain unchanged.

b) Give a sequence of instructions that will take the 5 low order bits in a register R2 and store them in bits 17 through 21 of R1, leaving the rest of R1 unchanged. It is permissible to change R2.


3.a) Write a *recursive* function in some familiar higher-level language (e.g., C) that computes the largest integer k such that, for any positive integer n (input), 2^k <= n ; i.e., k is a largest integer approximation to the logarithm (base 2) of n. The algorithm can be defined recursively as follows:
        log_2(n)    =   if n=1 then 0 
                        else 1 + log_2(n/2)
Use at least one local variable in your function, even though this may not be necessary.

b) Suppose that your function is implemented using a stack, stack pointer register (sp), and frame pointer register (fp), as discussed in class. Consider the call:
                x = log_2(9);
Show the contents of the stack and pointer registers at *each* call of log_2, starting with the one above, and after *each* return from the function.