Little Man Computer
The Little Man Computer (LMC) was created by Dr. Stuart Madnick as an instructional model. The LMC models a simple von Neumann architecture computer, so it has all of the basic features of a modern computer. The LMC can be programmed in machine or assembly code, and the following simulator will allow you to run your programs.
LMC Architecture
The LMC model is based on the concept of a little man locked in a small room. At one end of the room, there are 100 mailboxes (Memory), numbered 0 to 99, that can each contain a 3 digit instruction. Furthermore, there are two mailboxes at the other end labeled INBOX and OUTBOX which are used for receiving and outputting data. In the center of the room, there is a work area containing a simple 2 function (addition and subtraction) calculator known as the Accumulator and a resetable counter known as the Program Counter. The Program Counter is similar to what a doorperson uses to keep track of how many people have entered a facility -- it can count up 1, or it can be reset to 0. As specified by the Von_Neumann_architecture, memory contains both instructions and data. The user loads data into the mailboxes and then signals the little man to begin execution.
Execution Cycle
The little man performs the following steps to execute a program:
- Check the Instruction Counter, Go to the mailbox with that number and fetch the instruction
- Increment the Instruction Counter (That is, make it reference the next mailbox, ready for the next cycle)
- Decode the instruction (Determine the action to be performed, and the mailbox on which to perform it)
- Execute the action
- Repeat the cycle
See also: Instruction_cycle
LMC Commands - Numeric
While the LMC does reflect the actual workings of binary processors, the simplification of decimal numbers was made to minimize the complexity for students who may not be comfortable working in binary/hexadecimal.
Each LMC instruction is a 3 digit decimal number. The first digit represents the command to be performed and the final two digits represent the address of the mailbox affected by the command.
Instructions
1xx - ADD - Take the value stored in mailbox xx and add it to whatever value is currently on the accumulator.
2xx - SUBTRACT - Take the value stored in mailbox xx and subtract it from whatever value is currently on the accumulator.
3xx - STORE - Take the value from the accumulator (non-destructive) and store it in mailbox xx (destructive).
5xx - LOAD - Take the value from mailbox xx (non-destructive) and enter it in the accumulator (destructive).
6xx - BRANCH (unconditional) - Reset the program counter to the value xx. That is, xx will be the next instruction executed.
7xx - BRANCH IF ZERO - If the accumulator contains the value 0, reset the program counter to the value xx. Otherwise, do nothing.
8xx - BRANCH IF POSITIVE - If the accumulator is 0 or positive, reset the program counter to the value xx. Otherwise, do nothing.
901 - INPUT - Go to the INBOX, fetch the value from the user, and put it in the accumulator (destructive)
902 - OUTPUT - Fetch the value from the accumulator (non-destructive), and put it in the OUTBOX for the user to read.
000 - HALT - Stop working.
A Sample Program
This program takes two numbers as input and outputs the difference.
Mailbox | Instruction | Notes |
---|---|---|
00 | 901 | INBOX --> ACCUMULATOR fetch first number |
01 | 399 | ACCUMULATOR --> MEMORY[99] move it out of the way |
02 | 901 | INBOX --> ACCUMULATOR fetch second number |
03 | 398 | ACCUMULATOR --> MEMORY[98] move it out of the way |
04 | 599 | MEMORY[99] --> ACCUMULATOR now that both inputs are retrieved, move the first back to the accumulator |
05 | 298 | ACCUMULATOR = ACCUMULATOR - MEMORY[98] subtract second number |
06 | 902 | ACCUMULATOR --> OUTBOX output the result to the user |
07 | 000 | HALT We're Done! |
LMC Commands - Mnemonic
LOAD mnemonic - LDA numerical/machine code - 5
Load the contents of the given mailbox onto the accumulator (calculator). Note: the contents of the mailbox are not changed.
STORE
mnemonic - STA
numerical/machine code - 3
Store the contents of the accumulator (calculator) to the mailbox of the given address. Note: the contents of the accumlator are not changed.
ADD
mnemonic - ADD
numerical/machine code - 1
Add the contents of the given mailbox onto the accumulator (calculator). Note: the contents of the mailbox are not changed, and the actions of the accumulator are not defined for add instructions that cause sums larger than 3 digits.
SUBTRACT
mnemonic - SUB
numerical/machine code - 2
Subtract the contents of the given mailbox from the accumulator (calculator). Note: the contents of the mailbox are not changed, and the actions of the accumulator are not defined for subtract instructions that cause negative results -- however, a negative flag will be set so that BRP can be used properly (see below).
INPUT
mnemonic - INP
numerical/machine code - 901
Copy the value from the "in box" onto the accumulator (calculator).
OUTPUT
mnemonic - OUT
numerical/machine code - 902
Copy the value from the accumulator (calculator) to the "out box". Note: the contents of the accumlator are not changed.
END
mnemonic - HLT
numerical/machine code - 000
Causes the Little Man Computer to stop executing your program.
BRANCH IF ZERO
mnemonic - BRZ
numerical/machine code - 7
If the contents of the accululator (calculator) are 000, the PC (program counter) will be set to the given address. Note: since the program is stored in memory, data and program instructions all have the same address/location format.
BRANCH IF ZERO OR POSITIVE
mnemonic - BRP
numerical/machine code - 8
If the contents of the accululator (calculator) are 000 or positive (i.e. the negative flag is not set), the PC (program counter) will be set to the given address. Note: since the program is stored in memory, data and program instructions all have the same address/location format.
BRANCH ALWAYS
mnemonic - BRA
numerical/machine code - 6
Set the contents of the program counter to the given address.
DATA
mnemonic - DAT
numerical/machine code - none
This is an assembler instruction which simply loads the value into the next available mailbox. For example, DAT 984 will store the value 984 into a mailbox.