Assembly language provides the closest human-readable representation of what the CPU actually executes.
Relationship to Machine Code
• Machine code: Binary instructions CPU directly executes (e.g., 00010101)
• Assembly: Human-readable mnemonics (e.g., ADD, LDA, STA)
• One assembly instruction = typically one machine code instruction
• Assembler converts assembly → machine code
Machine Code: 0101 0011 0100
Assembly: LDA 52
Meaning: Load value from address 52 into accumulator
Why Use Assembly?
| Advantages | Disadvantages |
|---|---|
| Maximum efficiency - no compiler overhead | Difficult to write and debug |
| Direct hardware control | Not portable (CPU-specific) |
| Precise timing control | Time-consuming development |
| Small code size | Easy to make errors |
| Used in device drivers, embedded systems, OS kernels | Hard to maintain |
The Little Man Computer (LMC) Instruction Set
The Little Man Computer is a simple model of a processor with one accumulator. Its assembly language has only eleven mnemonics, and exam questions ask you to follow programs written with them and to write your own.
| Mnemonic | Instruction | What it does |
|---|---|---|
| ADD | Add | Adds the contents of the given memory location to the accumulator |
| SUB | Subtract | Subtracts the contents of the given memory location from the accumulator |
| STA | Store | Copies the contents of the accumulator into the given memory location |
| LDA | Load | Copies the contents of the given memory location into the accumulator |
| BRA | Branch always | Jumps to the instruction at the given label |
| BRZ | Branch if zero | Jumps to the given label if the accumulator holds zero |
| BRP | Branch if positive | Jumps to the given label if the accumulator holds zero or a positive number |
| INP | Input | Takes a value from the user and places it in the accumulator |
| OUT | Output | Outputs the contents of the accumulator |
| HLT | End program | Stops the program |
| DAT | Data location | Reserves a memory location for data, with a starting value if one is given |
• A label written before an instruction names that line, so a branch can jump to it
• A label written before DAT names a memory location, so it works as a variable
•
one DAT 1 creates a location called one that holds the value 1 when the program startsFollowing and Writing LMC Programs
There is no instruction for comparing two values and no loop construct. Both are built from subtraction and branches.
• Selection - subtract one value from the other, then use BRZ (the values are equal) or BRP (the result is zero or positive) to jump to a different part of the program
• Iteration - put a label at the start of the loop and use BRA to jump back to it, with a BRZ or BRP that jumps out of the loop when it should end
• Variables - give every stored value its own DAT line, placed after HLT so that data is never executed as an instruction
This program takes two numbers as input and outputs the larger.
INP
STA numA
INP
STA numB
SUB numA
BRP useB
LDA numA
OUT
HLT
useB LDA numB
OUT
HLT
numA DAT
numB DAT
This program takes a number as input and counts down from it to 1.
INP
STA count
loop LDA count
BRZ end
OUT
SUB one
STA count
BRA loop
end HLT
count DAT
one DAT 1
| Pass | Accumulator after LDA count | Does BRZ branch? | Output | count after STA count |
|---|---|---|---|---|
| 1 | 3 | No | 3 | 2 |
| 2 | 2 | No | 2 | 1 |
| 3 | 1 | No | 1 | 0 |
| 4 | 0 | Yes, to end |