Little Man Computer¶
Peter Higginson's LMC implementation - Help Page from Peter Higginson's LMC implementation
I have also created a GitHub repository dedicated to LMC examples.
Glossary¶
Knowing the definition of the following words is required to fully understand the User Guide.
| Word / Phrase | Definition | 
|---|---|
| Accumulator | Stores the result of the last operation or calculation. | 
| Program Counter | Stores the memory location of the next operation to be executed. | 
Basic Instructions¶
The following is a list of the basic instructions that can be performed on LMC.
| Instruction | Description | 
|---|---|
| HLT | Signifies the end of the program. | 
| INP | Waits for numeric input and stores it in the accumulator. | 
| OUT | Outputs the contents of the accumulator. | 
| ADD | Adds the contents of the given memory address to the accumulator. | 
| SUB | Subtracts the contents of the given memory address from the accumulator. | 
| STA | Stores the contents of the accumulator to the given memory address. | 
| LDA | Loads the contents of the given memory address to the accumulator. | 
| DAT | Stores the given value in the memory address of the instruction. | 
Example (basic_calc)¶
Using the knowledge you've gained above, you should be able to build a simple calculator program like the one below:
    INP    # Waits for input and stores it in the accumulator
    STA 99 # Stores the contents of the accumulator to the memory address: 99
    INP    # Waits for input and stores it in the accumulator
    ADD 99 # Add the contents of memory address: 99 to the accumulator
    OUT    # Outputs the contents of the accumulator
    HLT    # Signify the end of the program
Labels¶
In larger programs, you may be storing lots of data, so using raw memory locations all over the place probably isn't the best of ideas. Labels solve this problem, and make you code neater at the same time.
Labels represents a memory address, and can be defined anywhere in your program.
Representing an instruction (labels_basic)¶
Here we are using a label to represent the memory location of an instruction.
START INP    # Waits for input and stores it in the accumulator
      STA 99 # Stores the contents of the accumulator to the memory address: 99
      INP    # Waits for input and stores it in the accumulator
      ADD 99 # Add the contents of memory address: 99 to the accumulator
      OUT    # Outputs the contents of the accumulator
      HLT    # Signify the end of the program
Representing a memory location for data (labels_advanced)¶
Here we are using a label to represent the memory location of data.
START INP     # Waits for input and stores it in the accumulator
      STA NUM # Stores the contents of the accumulator to the memory address of the label: NUM
      INP     # Waits for input and stores it in the accumulator
      ADD NUM # Add the contents of memory address of the label: NUM to the accumulator
      OUT     # Outputs the contents of the accumulator
      HLT     # Signify the end of the program
NUM   DAT 1   # Defines the label: NUM as containing data, and sets it to a preset of 1
Branching¶
Branching is the process of executing a different sequence of instructions.
Instructions¶
The following are instructions relevant to branching.
| Instruction | Description | 
|---|---|
| BRA | Branches to the given memory address. | 
| BRZ | Branches to the given memory address, if the value in the Accumulator is 0. | 
Example (improved_calc)¶
Continuing with the trend of a calculator, let's build a dual-purpose calculator using the skills learnt above.
START    INP          # Waits for input and stores it in the accumulator
         STA MODE     # Stores the contents of the accumulator into the memory location of the label: MODE
         INP          # Waits for input and stores it in the accumulator
         STA VALUE_O  # Stores the contents of the accumulator into the memory location of the label: VALUE_O
         INP          # Waits for input and stores it in the accumulator
         STA VALUE_T  # Stores the contents of the accumulator into the memory location of the label: VALUE_T
         LDA MODE     # Loads the contents of the memory location of the label: MODE to the accumulator
         BRZ ADDITION # Branches to the memory location of the label: ADDITION is the contents of the accumulator are 0
         BRA SUBTRACT # Branches to the memory location of the label: SUBTRACT
ADDITION LDA VALUE_O  # Loads the contents of the memory location of the label: VALUE_O to the accumulator
         ADD VALUE_T  # Add the contents of memory address of the label: NUM to the accumulator
         BRA END      # Branches to the memory location of the label: END
SUBTRACT LDA VALUE_O  # Loads the contents of the memory location of the label: VALUE_O to the accumulator
         SUB VALUE_T  # Subtract the contents of memory address of the label: NUM to the accumulator
         BRA END      # Branches to the memory location of the label: END
END      OUT          # Outputs the contents of the accumulator
         HLT          # Signify the end of the program
MODE     DAT 1        # Defines the label: MODE as containing data, and sets it to a preset of 1
VALUE_O  DAT 1        # Defines the label: VALUE_O as containing data, and sets it to a preset of 1
VALUE_T  DAT 2        # Defines the label: VALUE_T as containing data, and sets it to a preset of 2
Except as otherwise noted, the content of this page is licenced under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Mozilla Public License 2.0.