Skip to content

Assembly Language and the Little Man Computer

1.2.4 Types of programming language · Paper 1 · 1 flashcard · 1 question

Assembly language provides the closest human-readable representation of what the CPU actually executes.

Definition
Assembly language is a low-level programming language with a strong correspondence to machine code instructions. It uses mnemonics (short codes) instead of binary.

Relationship to Machine Code

Key Point
Assembly vs 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
The Same Instruction:
text
Machine Code: 0101 0011 0100
Assembly:     LDA 52
Meaning:      Load value from address 52 into accumulator

Why Use Assembly?

Pros & Cons
Pros & Cons (Assembly Language):
AdvantagesDisadvantages
Maximum efficiency - no compiler overheadDifficult to write and debug
Direct hardware controlNot portable (CPU-specific)
Precise timing controlTime-consuming development
Small code sizeEasy to make errors
Used in device drivers, embedded systems, OS kernelsHard 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.

MnemonicInstructionWhat it does
ADDAddAdds the contents of the given memory location to the accumulator
SUBSubtractSubtracts the contents of the given memory location from the accumulator
STAStoreCopies the contents of the accumulator into the given memory location
LDALoadCopies the contents of the given memory location into the accumulator
BRABranch alwaysJumps to the instruction at the given label
BRZBranch if zeroJumps to the given label if the accumulator holds zero
BRPBranch if positiveJumps to the given label if the accumulator holds zero or a positive number
INPInputTakes a value from the user and places it in the accumulator
OUTOutputOutputs the contents of the accumulator
HLTEnd programStops the program
DATData locationReserves a memory location for data, with a starting value if one is given
Key Point
Labels:
• 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 starts

Following and Writing LMC Programs

There is no instruction for comparing two values and no loop construct. Both are built from subtraction and branches.

Key Point
Building constructs in LMC:
• 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
Worked Example
Worked Example: Selection

This program takes two numbers as input and outputs the larger.
assembly
        INP
        STA numA
        INP
        STA numB
        SUB numA
        BRP useB
        LDA numA
        OUT
        HLT
useB    LDA numB
        OUT
        HLT
numA    DAT
numB    DAT
After the second input the accumulator still holds numB, so SUB numA leaves numB minus numA in the accumulator. If that result is zero or positive, numB is at least as large as numA, so BRP jumps to useB and numB is output. Otherwise the program carries on to the next line and outputs numA.
Worked Example
Worked Example: Iteration

This program takes a number as input and counts down from it to 1.
assembly
        INP
        STA count
loop    LDA count
        BRZ end
        OUT
        SUB one
        STA count
        BRA loop
end     HLT
count   DAT
one     DAT 1
Trace for an input of 3:
PassAccumulator after LDA countDoes BRZ branch?Outputcount after STA count
13No32
22No21
31No10
40Yes, to end
The program outputs 3, 2, 1 and then halts.
Exam Tip
Before a calculation or an output, check what is in the accumulator: OUT outputs whatever it holds at that moment, and ADD and SUB change it. Remember that STA does not change the accumulator, and that BRP also branches when the accumulator holds zero.