LMC Branch Instructions (for making
The following program will demonstrate how to use the
branching instructions of the LMC to implement decision making. Note: The
Branch Always instruction should only be used to connect paths indicated by
following is the flowchart for a program that will output the largest of two input values. Note: if the values are
equal, both numbers represent the largest value, so either can be output.
SECONDBIG LDA SECOND
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 fourteen line
program above and paste it into the Message Box
- Click on the "Compile
- Click on the
- When prompted, enter
three-digit numbers in the "In-Box", and press the
What you should see
- After the program is
compiled, you should see from mailbox 0 to 10 the instructions 901, 312,
901, 313, 212, 809, 512, 902, 611, 513, 902. The Program Counter
should start at 0 (click on "Reset" if necessary).
- DAT reserves mailbox 12 for
FIRST and mailbox 13 for SECOND.
- SECONDBIG and PROGRAMEND
are identifiers like FIRST and SECOND that the assembly language program
can use to label certain mailboxes. When an identifier appears
before an LMC instruction (e.g. FIRST DAT or PROGRAMEND HLT), the
identifier is declared to the compiler, and the compiler records which
mailbox the instruction is stored in (e.g. mailbox 12 for DAT and mailbox
11 for HLT). When an identifier appears after an LMC instruction
(e.g. STA FIRST or BRP SECONDBIG), the compiler converts that identifier
into a mailbox to complete the machine code instruction (e.g. STA is
machine code 3 and FIRST refers to mailbox 12, so STA FIRST becomes
machine code 312).
- When you click on
"Run" or "Step", the Message Box will describe the
actions of each instruction.
- The first four instructions
store your two input values.
- The SUB instruction is used
to determine which of the two values is larger.
- If the FIRST value is
larger, subtracting it from the SECOND value will cause the Accumulator
to become negative. The BRP instruction will then do nothing, and
the program will continue to mailbox 6. After the LOAD and OUTPUT
instructions to output the FIRST value to the Out-Box, the program will
use the BRA instruction to skip the set of instruction that output the
SECOND value and go straight to the next/last instruction of the program
- If the FIRST value is
smaller (or equal), subtracting it from the SECOND value will leave the
Accumulator with a positive (or zero) value. The BRP instruction
will then update the Program Counter -- 8 means BRANCH on zero or
positive and 09 refers to the new value of the Program Counter (i.e. the
mailbox which holds the first instruction of the sequence that will
output the SECOND value). This BRANCH instructions
causes the LMC to skip the part of the program that outputs the FIRST
value. After outputing the SECOND value, the program continues on
with the next/last instruction of the program -- HLT.