I am tasked to write a program that performs an integer division on two positive numbers X and Y:
quotient = X % Y
remainder = remainder of division
display quotient and remainder
if X or Y is <= 0, display the value 999 (as an error)
Use divide algorithm, using subtraction:
This is the program I have so far:
ORG 100
Input
Store A
Input
Store B
If, Load A
Skipcond 800
Jump EndIf
Then, Load A
Subt B
Store A
Load C
Add One
Store C
Jump If
EndIf, Load C
Halt, Output
C, DEC 0
A, DEC 0
B, DEC 0
One, DEC 1
But I don't see how to add this "error". I think the code for division is correct, but adding "error" to it puts me in a stalemate. How can I integrate the error condition and output?
-
Do you know how to display 999? Do you know how to test forr <= 0? Put those together for the error case.Erik Eidt– Erik Eidt2024年04月16日 23:13:01 +00:00Commented Apr 16, 2024 at 23:13
1 Answer 1
There are a few issues in your current code:
There is no
Haltinstruction. This can lead to weird behaviour as the next instruction belowOutputis whateverChas as value. Be aware that theHaltlabel is just a label. You really need an explicitHaltinstruction.The loop is exited when
Ais<=0. But whenAis negative, that means you have subtractedBone time too many, andChas been incremented one time to many. The loop should be exited beforeAgets negative. See the condition in the flowchart you have included.The assignment says to output two values (quotient and remainder), but your code only outputs one value (intended to be the quotient). The remainder should be the value in
A(but see previous point), so you should output its value too.
Less of a problem, but:
I see no good reason why you would store your program from address 100 onwards. Just drop that
ORG 100.When you restart the program (using a possibility in a MARIE simulator), the contents of
Cwill still have the old value, so that will impact the new result you would expect. It is therefore good practice to clear/reset any variables that could have changed value in a previous run.Your code would be more readable if you would use more descriptive variable names.
A,BandCare not really helpful names. You could names likeDividend,DivisorandQuotient.
As to your core question: to implement the "validity" check on both inputs, don't try to mix this code with the loop you have. Just add code before the loop that makes that verification and outputs the value 999 when one of the inputs is not strictly positive.
Clear
Store Quotient / Clear the quotient (when restarting)
Input
Store Dividend
Input
Store Divisor
/ Validate input
Skipcond 800
Jump Invalid
Load Dividend
Skipcond 800
Jump Invalid
Loop, Load Dividend
Subt Divisor
/ Exit before storing negative result:
Skipcond 000
Jump Continue / When Dividend - Divisor >= 0
Jump Exit
Continue,Store Dividend
Load Quotient
Add One
Store Quotient
Jump Loop
Exit, Load Quotient
Output
/ Also output remainder
Load Dividend
Output
Halt
Invalid, Load ErrorCode
Output
Halt
/ Use descriptive names
Quotient, DEC 0
Dividend, DEC 0
Divisor, DEC 0
One, DEC 1
/ Add a constant for the error code
ErrorCode, DEC 999
You can run this code on MARIE.js.org