Assembly Language
Little Man Computer (LMC)
Assembly language is a low-level programming language. Each assembly language is specific to a particular computer architecture. Assembly language uses mnemonics to represent low-level machine instructions or opcodes. Many operations require one or more operands in order to form a complete instruction. Most assembly languages let you use different modes of addressing to specify the value (immediate addressing) or location/memory address of an operand. Assembly languages also let you use labels in the code to point to specific memory locations or registers.
LMC
LMC (Little Man Computer) is an example of basic assembly language used for educational purposes. It consists of only 11 mnemonics (e.g. INP, OUT, STA, BRP, etc.) and is based on one memory addressing mode: direct addressing.
Link for LMC: http://peterhigginson.co.uk/LMC/
Translation Process
Because assembly language use a relatively basic syntax and a limited number of mnemonics, the translation process to convert low level code into machine code (aka object code or binary code) is fairly straightforward. This process is called “assembling” and is preformed by an assembler: a piece of software utility used to translate/assemble low-level code into binary code.
Because assembly depends on the machine code instructions, every assembler has its own assembly language which is designed for exactly one specific computer architecture.
Assembly language usually has one statement per machine instruction so there is the 1:1 relationship between a low-level instruction and a machine instruction. Basic computer architectures will result in a reduced number of instructions. More complex architectures will result in a wider range of instructions making it easier to write assembly code. (Find our more about RISC – Reduced Instruction Set – and CISC – Complex instruction Set – processors)
Unlike assembly language, most high-level programming languages are generally portable across multiple architectures but require interpreting or compiling, a much more complicated task than assembling.
Assembler
Team 101 computing have created an online assembler for the LMC language to demonstrate the process of translating LMC code to machine/binary code. You can use it to load your own LMC programs and assemble them into binary.
In the early days of computer science (1950s, 1960s) computer programs could be “printed” using hole punched cards or tape. LMC assembler let you preview the resulting hole punched tape for your programs.
LMC simulators are based on the Little Man Computer (LMC) model of a computer, created by Dr. Stuart Madnick in 1965. The LMC simulator is generally used to for educational purposes, because it models a simple Von Neumann architecture computer which has all of the basic features of a modern computer. It is programmed using assembly code.
The LMC simulator demonstrates how assembly code is processed by the CPU using the Fetch, Decode and Execute (FDE) Cycle. It shows the impact of the FDE cycles on the main registers found inside the CPU:
- The PC (Program Counter),
- The CIR (Current Instruction Register),
- The MAR (Memory Address Register),
- The MDR (Memory Data Register),
- The Accumulator.
The LMC simulator also demonstrates how the CPU interacts with the Random Access Memory (RAM) and how both data and instructions are stored in the RAM. On our LMC simulator the RAM consists of 100 memory cells (also called mailboxes). Each cell has a unique memory location (a number between 00 and 99).
Most assembly instructions consist of an opcode and an operand.
When coding in assembly code we use mnemonics (e.g. INP, ADD, SUB, LDA, STA, etc.) for the opcode of the instruction. A lookup table (See at the end of this blog post) is used to check the corresponding opcode matching a mnemonic. With the LMC simulator, the operand is a memory location (Direct Addressing) of where the data used by the instruction can be fetched from (e.g. LDA, ADD, SUB instructions) or needs to be stored (e.g. STA instruction).
Labels can also be used instead of hard coding memory locations in the code. Labels are extremely useful when using branching instructions (BRA, BRZ, BRP), as they can be used to locate a specific instruction in the code/RAM. They can also be used to name memory cells in the RAM used to temporary store values. (Similar to the use of variables in a high level code).
LMC simulators can be used to implement simple programs to perform basic mathematical operations such as adding or subtracting numbers, comparing or sorting numbers, etc.
LMC Simulators
Little Man Computer: Mini Challenges
By completing this set of mini challenges you will write programs using Little Man Computer using all the instructions of the LMC instruction set. You will start with basic Input / Process / Output challenges based on sequencing. You will then investigate branching instructions (BRP, BRZ, BRA) to write programs using selection (a.k.a. branching) and iteration (a.k.a. loops).
- Write LMC code to do the following: 134 + 57 – 21 and store result in memory location 88
- Given two numbers. Swap them. Consider two ways.
- Find the maximum of two numbers entered by user
- Given two numbers. If the first number is greater than the second, then display the sum of these numbers, otherwise the difference
- Enter number 5 and output results have to be as follows 1,2,3,4,5
- Find sum of even numbers. Input 10. Output 2+4+6+10=22
- Ascending and descending order for input number Input =5 Result= 1,2,3,4,5,4,3,2,1
- Find max of 3 numbers
- Multiplication of 2 numbers
- Power A of B . Example A=5, B=3 Result=125
Additional tasks:
https://www.101computing.net/little-man-computer-mini-challenges/
LMC Simulators:
Quizizz Little Man Computer (LMC)
- Analyse the code snippet below.
Line number | Address | Instruction |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
00
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 |
LDA 16
STA 13 LDA 13 BRZ 10 SUB 14 STA 13 LDA 15 ADD 16 STA 15 BRA 02 LDA 15 OUT HLT DAT 00 DAT 01 DAT 00 DAT 03 |
-
State what the code does. [1]
-
Identify the line number where the condition is used. [1]
Note: use the table below for an explanation of mnemonics.
Mnemonic | Explanation |
INP | Retrieve user input and stores it in the accumulator. |
OUT | Output the value stored in the accumulator. |
LDA | Load the Accumulator with the contents of the memory address given. |
STA | Store the value in the Accumulator in the memory address given. |
ADD | Add the contents of the memory address to the Accumulator |
SUB | Subtract the contents of the memory address from the Accumulator |
BRP | Branch/Jump to the address given if the Accumulator is zero or positive. |
BRZ | Jump to the address given in case the Accumulator is zero. |
BRA | Jump to the address given. |
HLT | Stop the code |
DAT | Used to associate a label to a free memory address. An optional value can also be used to be stored at the memory address. |