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
1 Answer 1
There are a few issues:
The multiplication part:
does not account for the case where the the input
yis 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
multiback 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 setmultito zero before the loop startsmodifies
y. This is problematic as the next section of your program (division) needs the original value ofy. So make sure not to modify it anywhere in your program. Instead of decrementingy, first copy its value to another variable (likecount) 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 definedremainder, but you didn't use it. It can serve this purpose.Skipcond 800is the wrong condition. You needSkipcond 000here, or else swap theJumpstatements that follow it.
Other remarks
Not problematic, but:
your program has no good use for the variables
sumandsub. The value that is written tosumis immediately read back from it, which is a useless operation when you have no other purpose for it. Same forsubMany seem to add
ORG 100at 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
output, Load x; Store quotientof course it printsx(also note you decrementxin the loop, so it's actually the remainder)Skipcond 800should beSkipcond 000, you want to skip looping if the result is negative.yto zero so your division will not work.