Reputation: 43
Just working with Little Man Computer here and have ran into a problem that is kind of bugging me. Since there is no if's etc, I'm finding it hard to stop odd numbers going into negatives numbers when being subtracted from an even number. For example I take 3 off 10 in a loop and branch of at zero. But that never hits zero. So I want to know how to get the app to stop before it goes into negative, for example 3 subtracted from 10 in a loop will be 10, 7, 4, 1, -2. But I want it to stop at 1, therefore not going into negative and overcoming this problem.
This is what I have so far, and only works for even and even numbers:
INP
STA DEC
INP
LOOP SUB DEC
OUT
BRZ QUIT
BRA LOOP
QUIT HLT
DEC DAT
(to start off, I guess it would be a good idea to declare the second input as a variable, right? Or not?)
It would be greatly appreciated if you could help me out. And come up with a permanent solution to this.
Upvotes: 0
Views: 5855
Reputation: 1
INP
STA first
INP
LOOP SUB first
BRP yesrepeat
ADD one
HLT
yesrepeat OUT
BRA LOOP
first DAT
one dat 001
Upvotes: -1
Reputation: 180937
I know I'll regret this but since this seems fairly straight forward, I'll attempt this without being able to test :-)
INP
STA DEC
INP
LOOP SUB DEC
BRP OUTPUT
ADD DEC <-- we went negative, so add back once
HLT
OUTPUT OUT
BRA LOOP
DEC DAT
Upvotes: 1