This homework involves the development of C and MIPS assembly language subroutines to compute the product of two 8 bit signed integers using Booth's algorithm. In addition to the programs you must develop, you will use your C code to generate the 80x86 assembly language code, and then comment this generated code (you will need to learn about the 80x86 instruction set). I've broken this assignment down in to the 3 parts listed below - you are required to do all three parts!
You are to write a (commented) C subroutine the multiplies
two signed integers using Booth's algorithm. The parameters passed to
your subroutine will be two ints (which are 32 bit twos
complement integers), but you may assume that these numbers are in the
range -128 to +127 (could fit in an 8 bit signed int). This restriction
on the range of values means that the loop in your implementation of
Booth's algorithm should iterate 8 times. The prototype
for the subroutine must look like this:
int booth(int x, int y);
The return value should be the product of x and
y. Since you can assume that x and
y are both 8 bit integers, the product will always fit in
a C int.
Your subroutine must compute the product using Booth's algorithm! This will require some bit manipulation and shifting operations. If you have never done this before in C, some examples of these operations are included below.
NOTE: You do NOT have to mimic the hardware implementation described in the book. This means you can use seperate C variables to store the multiplier and partial product, you do not need to squeeze them in to a single 32 bit int. Your algorithm must treat the multiplier as a collection of sequences of 1s, and do a single subtraction at the beginning of each sequence of 1s and an addition at the end of each sequence of 1s (this is Booth's algorithm).
In C the & operator is a bitwise logical AND operation
(just like the MIPS and instruction). The |
operator computes a bitwise logical OR. Here are a few examples of
using the & operator to isolate the value of some bits
in a C int:
|
Using the | (OR) operator is similar.
The C << operator means "shift left". The operand
on the left of the << operator is the value to
shift, and the operand to the right is the number of bits to shift.
This operator always shifts in 0s on the right side. For example the
C expression:
x = y << 1;
means that x should get the value that results from shifting
y one bit to the left (and shifting in a 0). If
x and y are integers, this is equivalent
to saying x = y*2 (we know that shifting an int left is
just like multiplying by 2). Note that you can do this:
x = x << 1;
The C >> operator means "shift right". If the
int being shifted is declared as unsigned
int the resulting shift right will be a logical shift, but if
the C variable is declared as an int the shift will be an
arithmetic shift. Here is code that uses >>:
int x,y;
...
x = x >> 1; /* shift x one bit to the right. Sign is extended since
x is an int (arithmetic shift) */
y = x >> 5; /* shift x 5 bits to the right and store the result in y */
For this first part of the homework you
do not need to include your test code (you will need to write a
main() that calls booth to test it) in your
submission. You should put just the function definition for the
booth function in a file named booth.c and
include this file in your submission.
booth() and comment the codeYou should use a C compiler to generate an 80x86 assembly
language program for your booth function. If you have
access to a PC running Linux or BSD you can use the command line
gcc -S booth.c to generate the assembly language code
for your function. The file you compile should have only
your booth() function, you don't need any test
code or any #includes. The compiler will generate
the file booth.s which will contain the assembly language
code for your function.
Your job is to comment the 80x86 assembly language code generated by the compiler. Since the 80x86 instruction set is very different than the MIPS instruction set, you will need to learn about the 80x86 instruction set (and a little about 80x86 assembly language). Chapter 3 includes a brief discussion of the 80x86 instruction set, and the following hyperlinks provide additional details:
If you don't have access to a C compiler running on an 80x86 machine, you can submit your C code to a web based compiler I have running at monte.cs.rpi.edu/~hollingd/comporg/servx86/. You will need to paste your C subroutine in to a form and submit the form. You will get back any compiler messages generated and (if there are no errors) you get a copy of the resulting assembly language program. Send email to hollingd@cs.rpi.edu if you have any problems with the web based system.
Once you have a 80x86 assembly language code for your
booth function you should comment the code and include
the commented code in a file with your submission. You don't need to
worry about commenting (or understanding) assembler directives, but
you should comment the actual assembly language instructions.
Comments like "shift the multiplier one bit to the right" are good,
comments like "shift right register AX" are not what I'm looking
for...
You also need to write a MIPS version of the booth subroutine.
We will run your booth subroutine with our test program,
so you need to make sure you are adhering to the MIPS assembly
language subroutine calling convention. Specifically, your subroutine
should be written so that:
$a0 and $a1.
$v0.
s registers
($s0, $s1, ...). This doesn't mean you can't use these
registers, it just means that if you use them you must save
the values on the stack and restore them before returning from the subroutine.
You also must write a subroutine named testbooth that
will be used to test your booth subroutine. The
testbooth subroutine will receive as parameters the
addresses of three arrays (of 32 bit integers) and an integer
indicating the length of all three arrays. I'll call the arrays
x, y and z and the integer
length n in order to describe the function of your
subroutine. Your subroutine should compute the product of x[0]
* y[0] and put the result in z[0]. Your subroutine
should do this n times, that is, do this for the first
n elements of the arrays. In C this subroutine would
look something like this:
void testbooth( int *x, int *y, int *z, int n) {
int i;
for (i=0;i<n;i++)
z[i] = booth(x[i],y[i]);
}
Your testbooth function will be passed the
following parameters:
x
will be in $a0.
y
will be in $a1.
z
will be in $a2.
n) in each array
will be in $a3.
The testbooth MIPS assembly language subroutine
should be in the same file as your MIPS booth function
when you submit your code.
Below is some MIPS assembly language that might be useful
for testing your subroutines, including a main that
calls testbooth and then prints out the results.
|
Your submission must include the following:
Commented C source code for your C booth() function.
Commented 80x86 assembly language code for your booth() function.
Commented MIPS assembly language code for your booth and
testbooth subroutines (in the same file).
A README file that tells us what is in each of the files you are submitting, and anything else you think might help use grade your submission.
Submit via email to comporg-submit@cs.rpi.edu with the subject line "4". Complete submission instructions are here. Please make sure you send submissions to comporg-submit@cs.rpi.edu not to comporg@cs.rpi.edu!!!