The Little Man Computer

The Little Man Computer is a simplified model of a computer, which tells us a lot about how computers work with data and instructions. In the following video, we take a closer look at the syntax and operation of a LMC!

To try out the Little Man Computer for yourself, hop over to the NERDfirst Resources page → http://resources.nerdfirst.net/lmc

 

Little Man Computer Basics

The Little Man Computer is described as a little man in a room. In front of him are 100 mailboxes, labelled from 0 to 100. Each mailbox can be filled with a three digit number. The man will start from mailbox 0, and interpret each number as an instruction. He will then carry out the instruction before moving to the next box, and starting the whole process over. The mailbox that is being inspected is indicated by the program counter.

The LMC can take in input and provide output via special mailboxes designed for this purpose. It also uses a box called the accumulator as a temporary location to perform arithmetic operations.

The numbers represent the following instructions:

1xxAdd mailbox xx to accumulator
2xxSubtract mailbox xx from accumulator
3xxStore accumulator to mailbox xx
5xxLoad from mailbox xx and overwrite accumulator
6xxSet program counter to xx
7xxSet program counter to xx if accumulator is zero
8xxSet program counter to xx if accumulator is positive
901Read from input (blocking) to accumulator
902Copy value from accumulator to output
000Halt the program

 

Let’s Try It!

The NERDfirst Little Man Computer has an “Export” feature, which allows you to copy your program to a text file. You can then also use the “Import” feature to import such a file back onto the LMC.

The following snippets can be imported into the program this way. Simply triple click on each of the below code blocks, and copy and paste it into the NERDfirst LMC’s “Import” feature.

Basic Plus

The following program performs a simple addition operation and gives the result back via the output:

["901","399","901","199","902","000","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]

“901” reads from the user input, while “399” stores the value into mailbox 99. We read the second value to the accumulator before directly adding the first value to it from mailbox 99, using the command “199”. Finally, we output the result using “902”, and the program halts.

Jumps

A jump can be used when we want to move to a different mailbox and continue execution there. The following example was shown in the video:

["624","","","","","","","","","","","","","","","","","","","","","","","","625","636","","","","","","","","624","","","647","","","","","","633","","","","","657","","","","","642","","","","","666","","","","","","652","","","675","","","","","","","","663","674","","","","","","","","","","","","","","","","","","","","","","","",""]

Commands beginning with “6” set the program counter to a new position. In this code, we set the program counter to follow a circle shape at the center of the LMC. Since a halt instruction is never encountered, this program never ends. You will have to manually stop it.

Conditionals and Loops

["901","810","000","","","","","","","1","902","209","601","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]

This program counts down to zero from the number you have input. There are three things of interest here:

Firstly – We use a conditional jump at square 001. It only jumps to square 010 if the accumulator value is positive. If it is zero, we “fall through” the instruction to the next one, which is a halt instruction.

The second line has two features of interest. It decrements the accumulator value, before using an unconditional jump to go back up to the first line, where the check is performed. The unconditional jump here is used for neatness. Should you have a program spread over multiple lines, using jumps can be helpful.

In addition, notice the “1” in cell 009. It’s only there to aid with the decrement operation. Note how there’s actually no way to increment or decrement a number – We can only add or subtract a number from a square. That’s why we have to store 1 somewhere!

 

A word before we continue

What we have seen above is enough to write a program using features analogous to what you get in your day-to-day programming languages. You can already express simple math, iterations, and conditionals using addition, subtraction and jumps.

However, more advanced features are available through metaprogramming. Metaprogramming refers to a program that can modify itself during execution. When executed correctly, metaprogramming can simplify programming tasks, or make a program exponentially more useful.

Metaprogramming works on the Little Man Computer because data is indistinguishable from instructions – They’re all just numbers in mailboxes, and can live side-by-side. What this means is, we can use math operations to manipulate a cell, then have the LMC execute it as an instruction.

On the next page, we’ll take a closer look at what metaprogramming with the LMC entails!