# MCS-284 Chapter 3 Homework (Fall 2002)

When turning in a homework problem, if it is one from the book, you should indicate the exercise number. If it is not from the book, you should indicate the number I give with an x in it, as in 3.x1 below. These will be the reference numbers I use in reporting back your standing on the homework.
• Problem 3.x1: Consider the following MIPS procedure:
```loop:
beq \$a1, \$zero, finish
j loop
finish:
jr \$ra
```
Assume `\$a0` and `\$a1` are used for arguments to the procedure, which are nonnegative integers k and n, respectively. Assume that `\$v0` is used for the procedure's result value. Write a simple mathematical formula expressing this result value in terms of k and n, and briefly explain how you reach this conclusion.

Also, write an alternate version of the program that only uses one branch or jump instruction each time through the loop, rather than both a branch and a jump each time. You may use additional branch or jump instructions, if you like, that are executed only once regardless of how many times the loop is taken.

• Do exercise 3.15 from page 200. This problem involves two different classifications of memory accesses. One concerns what is moving between the memory and processor: an instruction or a data value. The other concerns the direction of movement: from memory to the processor (a read) or from processor to memory (a write).

• Problem 3.x2: Write (in MIPS assembly language) a procedure named `whence`, such that after calling the procedure with the instruction
```jal whence
```
the procedure will return (to the next instruction after the `jal whence`) with the `\$v0` register containing the address in memory where that `jal whence` instruction occurs.

Instructor: Max Hailperin