Write a Hack Assembly Language program to calculate the quotient from a division operation. The values of dividend a and divisor b are stored in RAM[0] (R0) and RAM[1] (R1), respectively. The dividend a is a non-negative integer, and the divisor b is a positive integer. Store the quotient in RAM[2] (R2). Ignore the remainder.
Example: if you are given two numbers 15 and 4 as dividend a (R0) and divisor b (R1), respectively, the answer will be 3 stored in R2.
test cases:
| RAM[0] | RAM[1] | RAM[2] |
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 0 | 2 | 0 |
// Storing R0 value into a new variable a
R0
X = Y // X= R0
a
Y = X // a = R0
// Storing R1 value into a new variable b
R1
X = Y // X = R1
b
Y= X // b = R1
// Initializing R2 as 0
0
X = Z // X = 0
R2
Y = X // R2 = 0
(NOTZERO)
a
X = Y // X = a
b
X = X - Y // X = a - b
END
X;JLT // (a-b) < 0 go to END
ZERO
X;JEQ // If (a - b == 0) go to ZERO
R2
Y = Y + 1 // R2 = R2 + 1
b
X = Y // X = b
a
Y = Y - X // a = a - b
NOTZERO // Going back to start of loop
0;JMP
(ZERO)
R2
Y = Y + 1 // R2 = R2 + 1
(END)
END
0;JMP
Get Answers For Free
Most questions answered within 1 hours.