I've managed to code an LMC program which works on this simulator for performing integer division. However if there is a remainder it goes into an infinite loop.
I'm trying to think of a way to just keep the quotient regardless of whether there is a remainder, but I'm stuck.
One idea was to increase the dividend by the original divisor and then check for a negative value for DIVISOR
before branching. However, there is only "Branch if Zero" or "Branch if positive" available, so I would probably have to re-write the program from scratch to use the inverted logic.
Can anyone please provide a version which can handle non-exact division?
// CANT HANDLE NOT-EXACT DIVISION
INP DIVIDEND
STA DIVIDEND
INP DIVISOR
STA DIVISOR
LOOP LDA DIVIDEND
BRZ END
SUB DIVISOR
STA DIVIDEND
LDA QUOTIENT
ADD ONE
STA QUOTIENT
BRA LOOP
END LDA QUOTIENT
OUT
SUB QUOTIENT
STA QUOTIENT
HLT
DIVIDEND DAT
DIVISOR DAT
QUOTIENT DAT 0
ONE DAT 1