Lab 4
Multiplication, Division, and Data Conversion
Objectives
In this lab exercise you will develop functions which multiply
and divide numbers, using nested subroutine calls.
What you
will do in lab today
- Download the file
multest.asm. Load it into the LC-3 editor, then read it over and be
sure you understand how it works. It has a very simple main program which
calls a function MULT1. This function takes the two numbers in R0 and R1,
multiplies them, and returns the answer in R2. It leaves R0 and R1
unchanged. Note that it only works if R1 is strictly positive
(i.e., it is greater than zero). Can you see why?
- Assemble the program,
then load it into the simulator and run it. Confirm that it works. Try
loading different values into X and Y, then run it again. Be sure to try
at least one calculation where X is negative.
- Now write a new function
called MULT and add it to the file. This function must work just like
MULT1, except that it must calculate correctly even if R1 is zero or
negative. It would be possible to write MULT as a self-contained
function; but for this lab you must have MULT call MULT1 (this is
to give you experience with calling one function from inside another
function). How would you make this work? Hint: Can you convert a
multiplication where the second number is negative to one where it is
positive, and still get the right answer? Note: Be sure that R0 and R1 are
unchanged after calling MULT.
- Remember that when you
call a function with the JSR or JSRR instruction, the return address is
stored in R7. So if you call a function from inside a function (such as
MULT1 from MULT) be sure to save R7 before calling the inner function, and
restore it after you return from the inner function; you could accomplish
the same thing by saving R7 as you enter every function, and restoring it
as you leave.
- Test MULT with several
sets of data to be sure it calculates properly.
- Now design an assembly
language function called DIV1 which works like this: It takes two numbers
in R0 and R1. It returns with R2 equal to (R0 / R1) [using integer
division], and R3 equal to (R0 % R1) [i.e., the remainder, which is equal
to R0 modulo R1]. Be sure it returns with R0 and R1 unchanged. This
function only has to work for R0 and R1 strictly positive. You should
work this out on paper first.
- Add your function to the
file, assemble it, and test it.
- When
done, submit your multest.asm to web-based turnin.
See you next week.