LMC Add and Subtract Instructions
The following program will demonstrate the add and subtract
instructions of the LMC. Note: The Accumulator (calculator) in
the LMC is only designed to work with non-negative three-digit
numbers. Mathematical operations which produce values greater
than 999 or less than 000 can cause undefined effects to occur.
Program
INP
STA FIRST
INP
ADD FIRST
OUT
INP
SUB FIRST
OUT
HLT
FIRST DAT
What you should do
- Click on the "LMC Simulator Applet" link to start the LMC
simulator.
- Clear the Message Box and all of the LMC mailboxes -- click
the "Clear Messages" button and the "Clear" button if necessary.
- Copy the ten line program above and paste it into the Message
Box
- Click on the "Compile Program" button.
- Click on the "Run" button.
- When prompted, enter three-digit numbers in the "In-Box", and
press the "Enter" button.
What you should see
- After the program is compiled, you should see from mailbox 0 to 7
the instructions 901, 309, 901, 109, 902, 901, 209, 902. The
Program Counter
should start at 0
(click on "Reset" if necessary).
- DAT is the tenth instruction of your program, so it refers to
mailbox 9 (0-indexed counting). FIRST is the identifier that has
been declared to represent this mailbox in the assembly language
program.
- When you click on "Run" or "Step", the Message Box will describe
the actions of each instruction.
- After the first INP instruction, the Accumulator has a copy of
the
first value entered in the In Box.
- After the STA instruction, the input value is copied from the
Accumulator to mailbox 9.
- After the second INP instruction, the Accumulator has a copy of
the
second value entered into the In Box (try values that will lead to
three and four digit sums).
- After the ADD instruction, the Accumulator value represents the
sum of the two input values -- 1
means ADD and 09 refers to the mailbox where the value to be added to
the Accumulator is stored.
- After the third INP instruction, the Accumulator has a copy of
the third value entered into the In Box (try values that will lead to
positive and negative results).
- After the SUB instruction, the Accumulator value represents the
difference between the two input values -- 2
means SUBTRACT and 09 refers to the mailbox where the value to be
subtracted from the Accumulator is stored.