Reputation: 11
I am writing a calculator that is supposed to add, subtract, multiply, and divide. Once the user is done with an operand it is supposed to directly go to the next one as well.
This is my code:
ORG 100 /Start Here
INPUT
Store X /Store 1st input value in X
INPUT
Store Y / Store 2nd input value in Y
choice, INPUT
Store C /Store choice in C
if, Load C
Subt A
Skipcond 400
Jump elseif1
JnS ADDITION /Call subroutine
Jump end
elseif1, Load C
Subt S
Skipcond 400
Jump elseif2
JnS SUBTRACT /Call subroutine
Jump end
elseif2, Load C
Subt M
Skipcond 400
Jump elseif3
JnS MULTIPLY /Call subroutine
Jump end
elseif3, Load C
Subt D
Skipcond 400
Jump elseif4
JnS DIVISION /Call subroutine
Jump end
elseif4, Load C
Subt Q
Skipcond 400
Jump choice /re-enter choice
Halt /stop the program
end, Output /Display the value
Halt /Stop
X, DEC 0
Y, DEC 0
C, DEC 0
/add two numbers
ADDITION Load X
Add Y
JumpI ADDITION / Indirect jump to return.
/subtract two numbers
SUBTRACT Load X
Subt Y
JumpI SUBTRACT / Indirect jump to return.
/multiply two numbers using continuous add loop
MULTIPLY loop, Load num
Add X
Store num
Load Y
Subt one
Store Y
Skipcond 400
Jump loop
Load num
JumpI MULTIPLY / Indirect jump to return.
/divide two numbers and return the quotient
DIVIDE If, Load X
Subt Y
Skipcond 800
Jump EndIf
Store X
Load quo
Add one
Store quo
Jump If
EndIf, Load quo
JumpI DIVIDE / Indirect jump to return.
/constants values
one, DEC 1
num, DEC 0
rem, DEC 0
quo, DEC 0
A, DEC 97 /a
M, DEC 109 /m
S, DEC 115 /s
D, DEC 100 /d
Q, DEC 113 /q
But I get "Operand undefined" errors and I don't know how to fix them.
I'm sure this is an obvious error to others, but I don't see what is wrong. What is the mistake?
Upvotes: 0
Views: 862
Reputation: 350310
There are several issues with your labels:
You have a missing comma after some label declarations, which is invalid syntax. See the next bullet points for more issues in these lines:
ADDITION Load X
and
SUBTRACT Load X
There are some double labels on the same line, which is not valid syntax. Each line can only define one label. This is related to the next bullet point:
The labels that define the start of subroutines should reserve one word for storing the return address, so they should not be followed immediately by the code of the subroutine. You should reserve that word by specifying something like Hex 0
, and then have the first instruction of your subroutine start at the next line.
So change this:
MULTIPLY loop, Load num
to:
MULTIPLY, Hex 0
loop, Load num
Similarly where you have DIVIDE
and If
, it should be:
DIVIDE, Hex 0
If, Load X
You also need ADDITION
and SUBTRACTION
to be followed by a comma and Hex 0
, and have the first instruction start on the next line:
ADDITION, Hex 0
Load X
A similar correction is needed for SUBTRACTION
You call a subroutine by the name DIVISION
, but its actual name is DIVIDE
. Make sure the names match.
Once you fix these issues, the code will assemble correctly. Then you'll see more logical problems:
The variables num
and quo
are not reset to 0 once they have been used for a multiplication of division. This means that if you perform another such calculation, the results will likely be wrong. You need to add code to reset these variables back to 0.
The division code is not correct. For instance, if you divide 4 by 2, you'll get 1 instead of 2. The issue is that SkipCond 800
will skip when the accumulator is greater than 0, while you want to also skip when the accumulator is zero.
Note that the division algorithm is not correct.
Upvotes: 0