Assignment 8, due Oct 22
Part of
the homework for 22C:60, Fall 2010
|
Always write your name legibly as it appears on your University ID and on the class list!
h = 0;
l = b;
repeat the following n times {
if l is odd thebn h = h + a
hl = hl >> 1}
now hl = a × b
The problem: Write a Hawk subroutine to multiply two numbers using the above algorithm. The 32-bit multiplicands are passed in R3 and R4 and the 64-bit product is returned in R3 (the low half) and R4 (the high half). (1 point)
a) Write the RANDOM routine for the Hawk, using the multiply and divide routines in the hawk monitor. You will probably need to compute the constants q and r using a calculator. (1 point)constant a = 16,807
constant m = 2,147,483,647
constant q = m / a
constant r = m % anote seed will always be in the range 1 to m-1, inclusive.
random() is {
seed = a * (seed % q) - r * (seed / q)
if seed < 0 then seed = seed + m}
b) Can you come up with an efficient way to multiply by the constant a using shift and add sequences? (0.5 points)
c) Can you come up with an efficient way to multiply by the constant r using shift and add sequences? (0.5 points)