CS 14 -- Lab 4
In today's lab, we will work on procedure calls in assembly. We
will work both with non-recursive and recursive procedures.
Simple exponentiation
In this lab, we're going to calculate exponents in a
straightforward manner. We are going to assume, for simplicity,
that both the base and exponents that we're using are
non-negative integers.
If we consider, when y > 0, that xy = x
* xy-1, then we can write a recursive procedure
that will rely on itself to calculate xy-1,
and multiply that result by x. When y = 0, we
simply observe that xy = 1, irrespective of
the value of x. So, in Java pseudo-code, the procedure
would be:
public static int calcExponent (int x, int y) {
if (y == 0) {
return 1;
} else {
return x * calcExponent(x, y-1);
}
}
An instruction you will need
In order to multiply numbers, you can use the mul
instruction, which is an R-type instruction that is used
like so:
mul $t0, $t1, $t2 # $t0 = $t1 * $t2
The assignment itself
-
Login to the romulus via VNC and use the following
command to copy a file into your directory:
cp ~sfkaplan/public/cs14/calcexponent.s .
-
Bring up your copy of the calcexponent.s file in
xemacs. Examine both the main procedure as
well as the printvalue procedure. Note that this
latter procedure uses some magic instructions to open a little
output window and print numbers into it. It is a simple
procedure that doesn't use any of the callee-preserved
registers, and so it needs no activation frame.
-
Find the comment labeled TASK 1. Insert two
procedure calls to printvalue, such that the base is
printed, and then the exponent is printed. The value to be
printed must be passed in register $a0.
-
Load the simulator and test your program. Is the
printvalue procedure called? Does it emit the
base and the exponent as expected. Do you see how the
procedure call is working? Observe the register $ra
when the jal instruction is executed.
-
Find the comment labeled TASK 2. Write the
calcexponent procedure. This procedure should expect
the base value to have been passed by the caller in register
$a0, and the exponent value to have been passed in
register $a1. It should calculate the exponent, as
described above, and return its result in register $v0.
Be sure to preserve any callee-preserved registers that the
method uses!
-
Find the comment labeled TASK 3. Insert two
procedure calls; the first should be to calcexponent
to perform the exponentiation, and the second should be to
printvalue, where the result of the exponentiation
should be emitted.
Finishing the lab
Email your code to me like so:
mail -s "CS 14 lab-4" sfkaplan@cs.amherst.edu < calcexponent.s
Be sure to exit xemacs and logout.
This assignment is due Thursday, March 3rd at
10:00 am.
Scott F. Kaplan
Last modified: Thu Feb 24 10:23:33 EST 2005