I am trying to write a LMC program that takes two integer inputs, divides them, and produces the quotient and remainder. To start, I broke down the problem into 4 stages:
1.Divide num1 by num2 to get the quotient, q.
2.Multiply q and num2 to get the exact multiplied number, and store it in num2.
3.Subtract num2 from num1, and store the answer in rem.
4.Output q and rem.
Here is the code I wrote:
INP
STA NUM1
LDA NUM1
STA ORG
INP
STA NUM2
BRZ END
LDA 99
STA Q
LOOP LDA NUM1
BRZ END
LDA NUM1
SUB NUM2
STA NUM1
LDA Q
ADD ONE
STA Q
BRA LOOP
LDA Q
STA NUM3
LDA NUM2
SUB ONE
STA NUM2
LOOP LDA NUM2
BRZ END
LDA NUM3
ADD Q
STA NUM3
LDA NUM2
SUB ONE
STA NUM2
BRA LOOP
LOAD ORG
SUB NUM3
STA REM
END LDA Q
OUT Q
LDA REM
OUT REM
HLT
NUM1 DAT
NUM2 DAT
ORG DAT
Q DAT
ONE DAT 1
TOTAL DAT
NUM3 DAT
REM DAT
When I try and run the code in an LMC simulator, it does not produce a result, instead it continues calculating indefinitely. How can I make it work? Is there a better way to do this? Any help is greatly appreciated.
There are several issues with your program:
OUTdoes not take an argument. It should not beOUT Q, but justOUT. A good LMC simulator should complain about this.LOOPis defined twice as a label. This is ambiguous. A good LMC simulator should complain about this.BRA LOOPis unreachable until the instructions atEND. It is dead code.NUM1becomes zero, but that might never happen. For instance if you divide 4 by 5, then the subtraction will give a negative overflow, and the accumulator will not be zero (actually it is not defined by the language which value it would then have). Instead of usingBRZthere, you should build your loop-condition logic usingBRP.Qis taken from mailbox 99, but that assumes that your program does not occupy mailbox 99. This is true, but it is better to use a label for aDAT 0.LDA NUM1) is not necessary, as the accumulator already has that value.The algorithm that you want to implement is a bit longwinded. After you have subtracted the second number from the first and no more subtractions are possible, you will already have the remainder (if you do it right).
Your algorithm should also deal differently with a 0 divisor: in that case maybe output some predefined value to indicate that the quotient is undefined, like 999.
Here is how you could code that. This is a runnable snippet with some example input you can adapt: