user22410271
user22410271

Reputation:

multiplication and division with marie

where is the mistake here , exactly in the the division , it returns the value of input x not return the result of the division

ORG 100 
Input
store x
input
store y

/ Summation 
Load x
Add y 
Store sum
/Output of the Summation
Load sum
Output

/ Subtraction
Load x
Subt y
Store sub
/Output of the subtraction
Load sub
Output


/ Multiplication
multloop, Load x
Add multi
Store multi
Load y
Subt one
Store y
Skipcond 400
Jump multloop
/Output of the multiplication
Load multi
Output

/ Division
Load zero 
Store quotient
divloop, Load x
Subt y
Skipcond 800
Jump Adding
Jump output
Adding, Store x
Load quotient
Add one
Store quotient
Jump divloop
/Output of Division
output, Load x
Store quotient
Load quotient
Output
Halt                    

/ Variable Declarations
x, dec 0
y, dec 0
one, dec 1
remainder, dec 0
quotient, dec 0
sum, dec 0
sub, dec 0
multi, dec 0
zero, dec 0

Upvotes: -1

Views: 73

Answers (1)

trincot
trincot

Reputation: 350252

There are a few issues:

  • The multiplication part:

    • does not account for the case where the the input y is 0. In that scenario the exit condition will be false for the next 65535 iterations of the loop! You should check the loop-exit condition before making any updates.

    • does not reset multi back to 0 before starting. This is important when the program is reset back to the top after it has already executed code. In that case the variables maintain their values. So do like you did for the division section and set multi to zero before the loop starts

    • modifies y. This is problematic as the next section of your program (division) needs the original value of y. So make sure not to modify it anywhere in your program. Instead of decrementing y, first copy its value to another variable (like count) and decrement that variable instead.

  • The division part:

    • modifies x. See previous point. Here it is less of a problem, as the division part is the last section in your program, but it would make sense to apply the same rule here: don't modify the input variables, but instead use another variable. You already defined remainder, but you didn't use it. It can serve this purpose.

    • Skipcond 800 is the wrong condition. You need Skipcond 000 here, or else swap the Jump statements that follow it.

Other remarks

Not problematic, but:

  • your program has no good use for the variables sum and sub. The value that is written to sum is immediately read back from it, which is a useless operation when you have no other purpose for it. Same for sub

  • Many seem to add ORG 100 at the front of their code. It really is not needed.

  • If the second input is 0, and the first not, the division loop will get into an infinite loop. This is not wrong, as division by 0 is undefined, but you could add a check for this condition and output some specific value, like 0.

Here is a version of your code with the above points taken into account:

          Input
          Store x
          Input
          Store y

          / Summation 
          Add x 
          Output

          / Subtraction
          Load x
          Subt y
          Output

          / Multiplication
          Load zero
          Store multi   / Initialise
          Load y
          Store count   / Don't modify y! Use different variable
multloop, Load count    / First test the loop condition before any update
          Skipcond 800
          Jump multout
          Subt one
          Store count
          Load x
          Add multi
          Store multi
          Jump multloop
          
          / Output of the multiplication
multout,  Load multi
          Output

          / Division
          Load zero
          Store quotient
          Load x
          Store remainder / Don't update x! Use different variable
          Load y          / Check for division by 0
          Skipcond 400
          Jump divloop
          Jump output
divloop,  Load remainder
          Subt y
          Skipcond 000  / Fix
          Jump Adding
          Jump output
Adding,   Store remainder
          Load quotient
          Add one
          Store quotient
          Jump divloop
          / Output of Division
output,   Load quotient
          Output
          Halt                    

/ Constant Declarations
zero,     Dec 0
one,      Dec 1

/ Variable Declarations
x,        Dec 0
y,        Dec 0
remainder,Dec 0
quotient, Dec 0
multi,    Dec 0
count,    Dec 0

Run it on MARIE.org

Upvotes: 1

Related Questions