M.A.R.I.E divide one number by another

Michael Quiles picture Michael Quiles · Oct 25, 2010 · Viewed 9.3k times · Source

I'm learning assembly using the MARIE program but I can't figure out hot to do this question from the book:

Divide one number by another and store the quotient and the remainder in two different memory locations.

This is what I have so far, what am I doing wrong? FYI there is no divide or multiplication built into the program so I have to do it using a loop but I guess I'm missing something.

The program can be had here http://computerscience.jbpub.com/ecoa/2e/downloads/MarieSim-v1.3.01.zip

ORG 100
Input           / Enter a number
Store X         / Saves the number 
Input           / Enter a number
Store Y         / Saves the number
Load Zero       / Move 0 into AC
Store Z         / Set Z to 0
If, Load Z      / Load Z
Skipcond 400    / If AC=0 (Z=0), skip the next instruction
Jump Endif      / Jump to Endif if X is not greater than 1
Then, Load X
Subt Y          / X - Y
Store X         / X = X - Y
Endif, Load Z   / Load Z into AC
Add One         / Add 1 to Z
Store Z         / Z = Z + 1
Output          / Print to screen
Halt            / Terminate program
X, Dec 0        / X has starting value
Y, Dec 0        / Y has starting value
Z, Dec 0
One, Dec 1      / Use as a constant
Zero, Dec 0     / Use as a constant
END

Answer

Johan Bezem picture Johan Bezem · Nov 29, 2011

If you want to divide using repeated subtraction, your program had better have some form of a loop.

The way your program is structured, it will run straight onto the Halt instruction after subtracting Y from X only once, and Z will end up being one.

It would be best to manually go through the code and execute each step on a piece of paper, then you'll see where you go wrong. And BTW, the comment on the Jump Endif is wrong, it's not X but Z you are checking upon.

You may want to modify your code, and then your question if it still presents problems.