Assembly Language & Little Man Computer (OCR A-Level Computer Science): Revision Notes
Assembly Language & Little Man Computer
Overview
Assembly Language is a low-level programming language that provides a way to write instructions directly for a computer's CPU. It operates close to the hardware, allowing precise control over memory and CPU operations. Assembly language is specific to a computer's architecture, making it more efficient but harder to write and understand than high-level languages. One tool to learn assembly language concepts is the Little Man Computer (LMC), a simple educational model that mimics the CPU's basic operations. LMC uses a limited set of instructions to illustrate how assembly works.
Purpose and Need for Assembly Language
- Direct Hardware Control: Assembly allows programmers to control hardware at a granular level, ideal for tasks that require high efficiency and precision.
- Efficient Resource Use: Assembly code can be highly optimised, which is critical in systems with limited memory or processing power, such as embedded systems.
- Understanding CPU Operations: Learning assembly provides insights into how CPUs execute instructions, memory addressing, and programme control flow.
Little Man Computer (LMC)
The Little Man Computer is a simplified CPU model that helps beginners understand assembly language concepts without the complexities of a real CPU. It uses an imaginary "little man" who follows basic instructions to perform tasks with a limited set of commands, representing core concepts in assembly programming.
LMC Components
- Mailbox (Memory): A set of numbered storage locations, or "mailboxes," where data and instructions are stored. Each mailbox holds a 3-digit instruction or data value.
- Accumulator (Calculator): A single register where arithmetic calculations are performed and results are stored.
- Program Counter: A register that keeps track of the address of the next instruction to execute.
- Input/Output: Used to simulate simple input and output operations for reading and displaying data.
LMC Instruction Set
Here are the basic instructions for LMC, each represented by a 3-digit code:
| Mnemonic | Instruction Code | Description |
|---|---|---|
| ADD | 1XX | Add the value at address XX to the accumulator. |
| SUB | 2XX | Subtract the value at address XX from the accumulator. |
| STA | 3XX | Store the value from the accumulator into address XX. |
| LDA | 5XX | Load the value at address XX into the accumulator. |
| BRA | 6XX | Branch always (jump) to address XX. |
| BRZ | 7XX | Branch to address XX if the accumulator is zero. |
| BRP | 8XX | Branch to address XX if the accumulator is positive. |
| INP | 901 | Input a value into the accumulator. |
| OUT | 902 | Output the value in the accumulator. |
| HLT | 000 | Halt the programme. |
Example Programmes in LMC
Simple Addition Program
This programme adds two numbers entered by the user and outputs the result.
INP // 901 - Input first number into the accumulator
STA 10 // 310 - Store the first number at address 10
INP // 901 - Input second number into the accumulator
ADD 10 // 110 - Add the number at address 10 to the accumulator
OUT // 902 - Output the result
HLT // 000 - Halt the programme
Explanation:
- The first input value is stored in mailbox 10.
- The second input is added to the value in mailbox 10, with the result displayed.
Simple Subtraction Program
This programme subtracts one user-entered number from another.
INP // 901 - Input the first number
STA 10 // 310 - Store the first number in address 10
INP // 901 - Input the second number
SUB 10 // 210 - Subtract the number at address 10 from the accumulator
OUT // 902 - Output the result
HLT // 000 - Halt the programme
Explanation:
- The first number is stored, and then the second input is subtracted from it, with the result displayed.
Loop Example: Count Down to Zero
This programme accepts a positive integer and counts down to zero, displaying each number.
INP // 901 - Input a number
STA 10 // 310 - Store the input in address 10
LDA 10 // 510 - Load the value at address 10 into the accumulator
OUT // 902 - Output the current number
SUB 12 // 212 - Subtract 1 (at address 12) from the accumulator
STA 10 // 310 - Store the updated value back in address 10
BRP 06 // 806 - Branch to address 06 if the accumulator is positive
HLT // 000 - Halt the programme
// Data
12 DAT 1 // Stores the value 1 to decrement the counter
Explanation:
- The input number is repeatedly decremented and displayed until it reaches zero, using the
BRPinstruction to check for a positive number.
Tracing LMC Programmes
Tracing a programme involves following each instruction step-by-step, noting changes to the accumulator and mailbox values. Here's an example trace for the Simple Addition Program above, assuming inputs 4 and 3.
| Step | Instruction | Accumulator | Mailbox 10 | Output |
|---|---|---|---|---|
| 1 | INP (901) | 4 | ||
| 2 | STA 10 (310) | 4 | 4 | |
| 3 | INP (901) | 3 | 4 | |
| 4 | ADD 10 (110) | 7 | 4 | |
| 5 | OUT (902) | 7 | 4 | 7 |
| 6 | HLT (000) | - | - | - |
Note Summary
Common Mistakes
- Incorrect Addresses: Using incorrect addresses in LMC can result in unexpected behaviour, as it could reference or modify data.
- Forgetting to Halt: If the
HLTcommand is missing, the programme may continue running, leading to errors. - Accumulator Overflow: Since LMC typically simulates limited memory, very large numbers or unanticipated results can cause overflow errors.
Key Takeaways
- Assembly Language is a low-level language used for efficient hardware control, suitable for systems programming and performance-critical applications.
- Little Man Computer (LMC) provides a simplified way to learn the principles of assembly language, focusing on a limited set of instructions.
- LMC Programs use basic commands like ADD, SUB, STA, and LDA to manipulate values in memory and control the flow of execution.
- Tracing and debugging assembly language code requires careful attention to each instruction and the changes it makes to memory and registers.