Jump to content

Little Man Computer

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Sychen (talk | contribs) at 23:28, 6 February 2007. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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.

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:

  1. Check the Program Counter, go to the mailbox with that number and fetch the instruction
  2. Increment the Program Counter (i.e. add one to the current value so that it now refers to the next mailboxthat will be accessed in the next cycle)
  3. Decode the instruction (which includes determining the action to be performed and the mailbox on which it will be performed)
  4. Execute the action
  5. 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.

MailboxInstructionNotes
00901INBOX --> ACCUMULATOR fetch first number
01308ACCUMULATOR --> MEMORY[08] move it out of the way
02901INBOX --> ACCUMULATOR fetch second number
03309ACCUMULATOR --> MEMORY[09] move it out of the way
04508MEMORY[08] --> ACCUMULATOR now that both inputs are retrieved, move the first back to the accumulator
05209ACCUMULATOR = ACCUMULATOR - MEMORY[09] subtract second number
06902ACCUMULATOR --> OUTBOX output the result to the user
07000HALT 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. DAT can also be used to declare variables. For example, DAT 984 will store the value 984 into a mailbox, and DAT FIRST will declare the variable FIRST and assign it to a mailbox.

Examples

Using the above mnemonics, the assembly language version of the program to subtract two numbers is given below, and this program can be compiled and run on the following LMC simulator.

INP
STA FIRST
INP
STA SECOND
LDA FIRST
SUB SECOND
OUT
HLT
FIRST DAT
SECOND DAT