Booth’s Algorithm Option: The goal of this assignment is to provide you with exp
ID: 3698206 • Letter: B
Question
Booth’s Algorithm Option: The goal of this assignment is to provide you with experience in using the "Logical and Shift Instructions". For this assignment, you are to implement Booth's multiplication algorithm on two 32-bit signed integers and get a 64-bit integer result. I want you to write a function called "Multiply" that is passed two signed integers (in $a0 and $a1) and returns their 64-bit product across registers $v0 and $v1. Register $v0 should return the most-significant 32-bits of the product and $v1 should contain the least-significant 32-bits of the product. Be sure that you DO NOT use any form of the multiply (i.e., MUL) assembly language instruction, and be sure to follow the MIPS register convensions when implementing "Multiply." Your "main" program should: 1) Read two 32-bit integer variables MULTIPLICAND and MULTIPLIER from memory. Initialize the value -30 for the MULTIPLICAND and 983 for the MULTIPLIER. 2) call your Multiply function with these values 3) Store the 64-bit product back to memory in a variable called PRODUCT The .data section of your program should look something like: .data MULTIPLICAND: .word -30 MULTIPLIER: .word 983 PRODUCT: .word 0,0
Explanation / Answer
#include <stdio.h>
int Add (int x, int y);
long long Booth (int x, int y);
int main() {
int hex1, hex2;
long long product;
printf(" Enter Multiplicand & Multiplier in hex: ");
scanf(" %x %x", &hex1, &hex2);
product = Booth(hex1, hex2);
printf("Multiplicand = 0x%08X As signed = %+d ", hex1, hex1);
printf("Multiplier = 0x%08X As signed = %+d ", hex2, hex2);
printf("Product = 0x%16X As signed = %+d ", product, product);
}
int Add (int x, int y) {
return x + y;
}
long long Booth (int multiplicand, int multiplier) {
int i;
long long product;
long long productLH;
long long productRH;
int productLHI;
int productRHI;
int cOut;
int negMultiplicand;
negMultiplicand = Add (~multiplicand, 1);
product = (long long) Add (0, multiplier);
for (i = 0; i < 32; i++) {
if (((product & 1) == 1) && (cOut == 0)) {
productLH = (product & 0xFFFFFFFF00000000);
productRH = (product & 0x00000000FFFFFFFF);
productLH = (productLH >> 32);
productLHI = (int) (productLH & 0x00000000FFFFFFFF);
productRHI = (int) productRH & (0x00000000FFFFFFFF);
productLHI = Add(productLHI, negMultiplicand);
product = (long long) Add(productLHI, 0);
product = ((product << 32) & 0xFFFFFFFFFFFFFFFF);
product = (long long) Add((int)product, productRHI);
}
else if (((product & 1) == 0) && (cOut == 1)) {
productLH = (product & 0xFFFFFFFF00000000);
productRH = (product & 0x00000000FFFFFFFF);
productLH = (productLH >> 32);
productLHI = (int) (productLH & 0x00000000FFFFFFFF);
productRHI = (int) productRH & (0x00000000FFFFFFFF);
productLHI = Add(productLHI, multiplicand);
product = (long long) Add(productLHI, 0);
product = ((product << 32) & 0xFFFFFFFFFFFFFFFF);
product = (long long) Add((int)product, productRHI);
}
cOut = (product & 1);
product = product >> 1;
}
return product;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.