In Assembly - Call Mul from a procedure Create a procedure, and in that procedur
ID: 3734702 • Letter: I
Question
In Assembly
- Call Mul from a procedure Create a procedure, and in that procedure multiply two numbers together using mul. Then call the procedure from the main procedure.
- Do bitwise mulitiplication using only shift and add statements In your multiplication procedure multiply two numbers not using mul but by combining a fixed number of shift and add commands. For this part you only need to be able to handle 8 bit numbers so you don't need to use a loop for this part
- Add loop to bitwise multiplication procedure Instead of using a fixed number of bitwise multipliers, use a loop to run the bitwise multiplication
Explanation / Answer
CODE:
public int mul(int a, int b) {
if (a ==0 || b == 0) {
return 0;
}
if (a == 1) {
return b;
}
else
if (b == 1) {
return a;
}
int result = 0;
int x = a;
boolean isORNeeded = false;
while (b != 0 ) {
if (b == 1) {
break;
}
if ((b & 1) == 1) //......... Carry needed, odd number.........
{
result += x;
isORNeeded = true;
}
a <<= 1; // this will Doubles the a
b >>= 1; // this will make Half of the b
System.out.println("a=["+a+"], b=["+b+"], result=["+result+"]");
}
return (isORNeeded ? (a | x) : a); // a + result;
}