I want to make a subtraction loop. I am tasked with finding out the number of $100 bills in a sum. For example, user enters $950. There is 9 one hundred dollar bills in that. What I first did was store the user input in date register 1, and compare it to 100.
cmpi.w #100,D1
I want to basically do "if D1 is less than 100, subtract another 100". How can I achieve this loop? I would like to use the branch greater than and less than instructions but am not sure how.
The normal algorithm would be to divide by 100. The M68K DIVU
/DIVS
instructions provide the result and the remainder at the same time:
MOVE #950,D1
DIVU #100,D1
Will leave 0x00320009
in the D1 register, i.e., 0x9 (9) as the result and 0x32 (50) as the remainder.
If you really want a loop, take a look at the following code (assembles and runs on EASy68K (http://www.easy68k.com/)). This is very standard assembly code, and loops on other architectures look very similar. (In fact, this is the first time I've ever written M68K assembly code.)
START:
MOVE #950,D1
Loop:
CMP #100,D1
BPL OverEqual100
BRA Under100
OverEqual100:
SUB #100,D1
ADD #1,D0
BRA Loop
Under100:
SIMHALT
(Also see the comments attached to this answer.)