0

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:

    enter image description here

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?

trincot
357k38 gold badges282 silver badges340 bronze badges
asked Apr 16, 2024 at 19:19
1
  • Do you know how to display 999? Do you know how to test forr <= 0? Put those together for the error case. Commented Apr 16, 2024 at 23:13

1 Answer 1

0

There are a few issues in your current code:

  • There is no Halt instruction. This can lead to weird behaviour as the next instruction below Output is whatever C has as value. Be aware that the Halt label is just a label. You really need an explicit Halt instruction.

  • The loop is exited when A is <=0. But when A is negative, that means you have subtracted B one time too many, and C has been incremented one time to many. The loop should be exited before A gets 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 C will 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, B and C are not really helpful names. You could names like Dividend, Divisor and Quotient.

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

answered Jul 24, 2024 at 9:49
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.