Little Man Computer

DOWNLOAD - LMC version 0.3 (link added 2012/12/02)


Imagine a little man locked inside a mail-sorting office!

In front of him are one hundred pigeonholes.  To one side is a serving hatch through which he can accept input from the outside world, and through which he passes output.  The little man can write 3-digit codes onto pieces of paper and store them in the pigeon holes according to a set of rules.

The Little Man Computer is a simplified model of computer CPU.  It is used as an instructional tool for teaching low-level programming.

System architecture. 

The little man has access to a workbench (called the accumulator or ACC) upon which he can store the result of his current calculation, and he has a resettable counter (called the program counter or PC) which keeps track of which pigeonhole he should look for his next instruction.

Computer 'programs' can be written for the Little Man Computer by storing single 3-digit instructions in the pigeonholes.  This represents the way a computer stores program data in the addressable main memory before execution.  The 3-digit instructions are comprised of a code used to indicate the current instruction to be performed, and a number indicating the address of the pigeonhole from which the little man must retrieve data.  Thus the pigeonholes represent addressable memory locations (from 00 to 99), and can contain both instructions and data.

Still awake?  Good, then roll your eyes and scroll down...

The instruction set

The Little Man Computer uses the following instructions, which can be either encoded directly into memory addresses or compiled using a simple assembly language.

Instruction                    Mnemonic          Code
ADD                            ADD               1..
SUBTRACT                       SUB               2..
STORE                          STA               3..
LOAD                           LDA               5..
INPUT                          INP               901
OUTPUT                         OUT               902
BRANCH ALWAYS                  BRA               6..
BRANCH IF ZERO                 BRZ               7..
BRANCH IF ZERO OR POSITIVE     BRP               8..



A value (between 00-99) substituted for the .. corresponds to a memory address.

For example:

399 means "STORE the current value in the accumulator at memory address 99".

598 means "LOAD the value in memory address 98 and put this into the accumulator, so that the accumulator has this new value".

712 means "if the value in the accumulator is zero then set the program counter to point at 12, so the next instruction will be taken out of memory address 12".

Mnemonics

Mnemonics allow the programmer to use handy short-cuts for instructions, rather than having to rely on numeric codes.  This means that we can write programs using an assembly language which is then compiled automatically into the memory addresses.

Labels

Labels are ways of referring to memory locations without having to remember the exact value to which a line of code, or a memory address refers.  Any code you see that is not part of the instruction set is a label and it can be substituted for a memory address. 

DOWNLOAD - LMC version 0.3 (link added 2012/12/02)

Example programs
(mail me for more help)

Doubles it! 

           INP
           STA NUMBER
           ADD NUMBER
           OUT
NUMBER     DAT
  
This program starts by taking input from the user.  The value entered (which will be between -99 and 999) is then stored at memory address with the label 'NUMBER'.  The next command adds the value at address with the label 'NUMBER' onto the accumulator (effectively this program doubles the value entered by the user). 


Checks X > Y

           INP 
           STA X 
           INP
           STA Y 
           SUB X 
           BRP SWAP
           BRA NEXT
SWAP       LDA X
 
           STA TEMP
           LDA Y
           STA X 
           LDA TEMP
           STA Y
NEXT       HLT


This program starts by taking two inputs from the user.  The first input is stored in 'X' and the second is stored in 'Y'.  The program switches the values of 'X' and 'Y' so that the largest value is always stored in 'X.

Square it!


START      LDA ZERO
           STA RESULT
           STA COUNT
           INP
           BRZ END
           STA VALUE  
LOOP       LDA RESULT

           ADD VALUE
           STA RESULT
           LDA COUNT
           ADD ONE 
           STA COUNT
           SUB VALUE
           BRZ ENDLOOP
           BRA LOOP   
ENDLOOP    LDA RESULT

           OUT 
           BRA START  
END        HLT
RESULT     DAT 0
COUNT      DAT 0    
ONE        DAT 1
VALUE      DAT 0
ZERO       DAT 0      

This program repeatedly takes an input and squares that value.  For example input '5' and the program will out '25' because 5x5 = 25.

Integer Division


START      INP
           STA Y 
           INP 
           STA X
LOOP       LDA Y
 
           SUB X 
           STA Y 
           LDA COUNT
           ADD ONE
           STA COUNT 
           LDA Y
           BRP NEXT 
           BRA DISPLAY
NEXT       BRA LOOP
DISPLAY    LDA COUNT
 
           SUB ONE 
OUT        HLT
ONE        DAT 001
COUNT      DAT 000


This program takes two inputs, 'Y' and 'X' respectively and performs integer division upon them.  So the output of '8' and '3' is 2, as 3 will go into 8 two times.

DOWNLOAD - LMC version 0.3 (link added 2012/12/02)

No comments:

Post a Comment