Loading...
Little Man Computer makes accumulator operations visible. Trace the program counter, accumulator and named data locations rather than reading assembly as though it were a high-level expression.
What you'll be able to do
LMC is a simplified stored-program model with memory mailboxes, an accumulator and a program counter. Labels name locations; they are not separate arithmetic variables outside memory. INP obtains a value into the accumulator, OUT outputs its current value, and HLT stops execution. DAT reserves a data location with an optional starting value.
LDA reads a mailbox into the accumulator; STA copies the accumulator into a mailbox. ADD and SUB combine the accumulator with the contents of an addressed mailbox. These operations do not automatically replace the stored operand. After an arithmetic operation, use STA if the calculated value must be saved for later. State the input range and memory assumptions in the task.
The program below reads two non-negative inputs whose sum fits the model, stores the first, adds it to the second and outputs the total. With inputs 7 then 4, the accumulator holds 7 after the first INP, 4 after the second, and 11 after ADD first. The mailbox first remains 7.
Trace one instruction at a time and record only actual outputs, not every temporary accumulator value. The program counter normally advances to the next instruction, but branches change that sequence. Data placed after HLT is not executed on the normal path; omitting a stopping instruction can make the machine interpret data as instructions.
BRA branches unconditionally. BRZ branches when the accumulator is zero. BRP branches on the non-negative condition in the usual OCR LMC model, including zero. An explicit BRZ can be useful when distinguishing equality from a strictly positive case. Follow any sign/underflow convention stated by a question or simulator.
A label such as done supplies a target address; it does not perform a comparison itself. Test branch code with values on both sides of a threshold and exactly at it. Negative arithmetic representation varies between teaching simulators, so do not invent universal wrapping behaviour when the exercise only needs a comparison or a non-negative arithmetic path.
INP
STA first
INP
ADD first
OUT
HLT
first DAT 0Scroll horizontally to see the full code or table.| Instruction | Accumulator after | first after | Output |
|---|---|---|---|
| INP (7) | 7 | 0 | — |
| STA first | 7 | 7 | — |
| INP (4) | 4 | 7 | — |
| ADD first | 11 | 7 | — |
| OUT | 11 | 7 | 11 |
Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.