I am trying to create a program in LMC which checks if a number evenly divides into another one (such as 10/2), with the program below I am able to do divison and halt the program as required
INP
STA DIVIDEND
INP
STA DIVISOR
LOOP LDA DIVIDEND
BRZ END
SUB DIVISOR
STA DIVIDEND
LDA ANSWER
ADD INC
STA ANSWER
BRP LOOP
END LDA ANSWER
OUT
SUB ANSWER
STA ANSWER
HLT
DIVIDEND DAT 0
DIVISOR DAT 0
ANSWER DAT 0
INC DAT 1
I need to find a way, within the same program, for LMC to recongzie that the numbers that user inputed do NOT evenly divide (such as 10/3) in which case it is to output zero and ask for two new numbers. I have tried doing this with another loop but if seems to always conflict with the loop I have for subtraction. I have tried whatever I could think and don't know how to make LMC fulfill this requiremnet while also retaining the requirement of halting the program when inputs do evenly divide
You'll need to check that the
SUB DIVISORresulted in negative overflow. If that happens you know that the dividend was not an integer multiple of the divisor and can output zero and repeat the program.Since at that point the answer data might already be non-zero, you should also reset it. This is what you did at the last two instructions just before the
HLT, but of course that doesn't serve much purpose when you're going to halt. It is any way good practice do perform such a reset at the start of your program, as the LMC has a user-control to restart the program at any moment, and then you want to be sure you really start with a clean slate.Here is your updated program. You can run it here: