我应该在Assembly中编写一个程序来计算除法运算中的商。被除数a和除数b的值分别存储在RAM [R0]和RAM [R1]中。被除数a是非负整数,除数b是正整数。将商存储在RAM [R2]中。我的问题是它适用于所有测试用例,例如
RO R1 R2
2 2 1
8 4 2
0 3 0
我的问题是,即使它适用于R0 = R1 = R2的情况。当R0 = R1 = 1时,它不起作用。即使它应该是1,R2仍然输出0。这是我的代码
// Storing the value in R0 into variable a
@R0
D = M // D = R0
@a
M = D // a = R0
// Storing the value in R1 into variable b
@R1
D = M // D = R1
@b
M = D // b = R1
// Initializing R2 to 0
@0
D = A // D = 0
@R2
M = D // R2 = 0
(NOTZERO)
@a
D = M // D = a
@b
D = D - M // D = a - b
@END
D;JLT // (a-b) < 0 go to END
@ZERO
D;JEQ // If (a - b == 0) go to ZERO
@R2
M = M + 1 // R2 = R2 + 1
@b
D = M // D = b
@a
M = M - D // a = a - b
@NOTZERO // Going back to start of loop
0;JMP
(ZERO)
@R2
M = M + 1 // R2 = R2 + 1
(END)
@END
0;JMP