See also: Instruction cycle
To execute a program, the little man performs these steps:
While the LMC does reflect the actual workings of binary processors, the simplicity of decimal numbers was chosen to minimize the complexity for students who may not be comfortable working in binary/hexadecimal.
Some LMC simulators are programmed directly using 3-digit numeric instructions and some use 3-letter mnemonic codes and labels. In either case, the instruction set is deliberately very limited (typically about ten instructions) to simplify understanding. If the LMC uses mnemonic codes and labels then these are converted into 3-digit numeric instructions when the program is assembled.
The table below shows a typical numeric instruction set and the equivalent mnemonic codes.
This program (instruction 901 to instruction 000) is written just using numeric codes. The program takes two numbers as input and outputs the difference. Notice that execution starts at Mailbox 00 and finishes at Mailbox 07. The disadvantages of programming the LMC using numeric instruction codes are discussed below.
LOAD the first value back into the calculator (erasing whatever was there)
Assembly language is a low-level programming language that uses mnemonics and labels instead of numeric instruction codes. Although the LMC only uses a limited set of mnemonics, the convenience of using a mnemonic for each instruction is made apparent from the assembly language of the same program shown below - the programmer is no longer required to memorize a set of anonymous numeric codes and can now program with a set of more memorable mnemonic codes. If the mnemonic is an instruction that involves a memory address (either a branch instruction or loading/saving data) then a label is used to name the memory address.
Without labels the programmer is required to manually calculate mailbox (memory) addresses. In the numeric code example, if a new instruction was to be inserted before the final HLT instruction then that HLT instruction would move from address 07 to address 08 (address labelling starts at address location 00). Suppose the user entered 600 as the first input. The instruction 308 would mean that this value would be stored at address location 08 and overwrite the 000 (HLT) instruction. Since 600 means "branch to mailbox address 00" the program, instead of halting, would get stuck in an endless loop.
To work around this difficulty, most assembly languages (including the LMC) combine the mnemonics with labels. A label is simply a word that is used to either name a memory address where an instruction or data is stored, or to refer to that address in an instruction.
When a program is assembled:
In the assembly language example which uses mnemonics and labels, if a new instruction was inserted before the final HLT instruction then the address location labelled FIRST would now be at memory location 09 rather than 08 and the STA FIRST instruction would be converted to 309 (STA 09) rather than 308 (STA 08) when the program was assembled.
Labels are therefore used to:
The program below will take a user input, and count down to zero.
The program below will take a user input, square it, output the answer and then repeat. Entering a zero will end the program. (Note: an input that results in a value greater than 999 will have undefined behaviour due to the 3 digit number limit of the LMC).
Note: If there is no data after a DAT statement then the default value 0 is stored in the memory address.
In the example above, [BRZ ENDLOOP] depends on undefined behaviour, as COUNT-VALUE can be negative, after which the ACCUMULATOR value is undefined, resulting in BRZ either branching or not (ACCUMULATOR may be zero, or wrapped around). To make the code compatible with the specification, replace:
with the following version, which evaluates VALUE-COUNT instead of COUNT-VALUE, making sure the accumulator never underflows:
Another example is a quine, printing its own machine code (printing source is impossible because letters cannot be output):
This quine works using self-modifying code. Position 0 is incremented by one in each iteration, outputting that line's code, until the code it is outputting is 1, at which point it branches to the ONE position. The value at the ONE position has 0 as opcode, so it is interpreted as a HALT/COB instruction.
"Little Man Computer". Illinois State University. May 1, 2000. Archived from the original on February 27, 2009. Retrieved March 8, 2009. https://web.archive.org/web/20090227005033/http://www.acs.ilstu.edu/faculty/javila/lmc/ ↩
Yurcik, W.; Osborne, H. (2001). "A crowd of Little Man Computers: Visual computer simulator teaching tools". Proceedings of the 2001 Winter Simulation Conference (Cat. No.01CH37304). Vol. 2. p. 1632. doi:10.1109/WSC.2001.977496. ISBN 0-7803-7307-3. S2CID 18907923. 0-7803-7307-3 ↩
Yurcik, W.; Brumbaugh, L. (2001). "A web-based little man computer simulator". Proceedings of the thirty-second SIGCSE technical symposium on Computer Science Education - SIGCSE '01. p. 204. doi:10.1145/364447.364585. ISBN 1581133294. S2CID 14794750. 1581133294 ↩
Osborne, H.; Yurcik, W. (2002). "The educational range of visual simulations of the Little Man Computer architecture paradigm". 32nd Annual Frontiers in Education. pp. S4G–S19. doi:10.1109/FIE.2002.1158742. ISBN 0-7803-7444-4. S2CID 10324295. 0-7803-7444-4 ↩