4.2.1 From Machine Code to Assembly Language
A processor executes binary machine instructions. Assembly language gives programmers a more readable way to express those low-level operations by replacing binary opcodes with short mnemonic names and allowing operands to be written as values, addresses or symbols.
By the end of this section, you should be able to:
- Explain the relationship between assembly language and machine code.
- Distinguish an opcode from an operand.
- Explain why instruction sets and machine-code encodings are processor-specific.
- Identify mnemonics, operands, labels and comments in assembly source code.
- Explain why an assembler is required before an assembly program can run.
- Follow an original example from assembly notation to illustrative machine code.
Where This Page Fits
This is the first page in 4.2 Assembly Instructions and Addressing. It establishes the language concepts needed for the later pages.
| Page | Main focus |
|---|---|
| 4.2.1 | Machine code, assembly notation, mnemonics, operands, labels and assemblers. |
| 4.2.2 | How a two-pass assembler builds a symbol table and resolves references. |
| 4.2.3 | Immediate, direct, indirect, indexed and relative addressing. |
| 4.2.4 | The required instruction set organised by purpose. |
| 4.2.5 | Tracing complete programs, registers, memory, output and jumps. |
Machine Code: The Processor's Binary Instructions
A processor is designed to recognise a particular set of binary instruction patterns. Each pattern represents a small operation such as loading a value, adding, storing, comparing or changing program flow.
A machine-code program is therefore a sequence of binary instructions. The meaning of each pattern is determined by the processor's instruction-set design.
Opcode and Operand
The processor must know what operation to carry out and, where required, what value, address or register the operation should use.
| Instruction part | Question answered | Possible content |
|---|---|---|
| Opcode | What operation should happen? | Load, add, store, compare, jump, output or end. |
| Operand | What value or location should the operation use? | A number, address, register or symbolic label. |
Some instructions need no explicit operand. For example, OUT uses the value
already in the accumulator, while END terminates the program.
Assembly Language: A Readable Low-Level Form
Assembly language represents processor instructions using short textual codes called
mnemonics. Instead of remembering a binary opcode, a programmer can
write a form such as LDM, ADD, STO or
JMP.
An assembly instruction normally contains a mnemonic followed by an operand when the operation needs one.
| Assembly instruction | Mnemonic | Operand | Plain-language meaning |
|---|---|---|---|
LDM #19 |
LDM |
#19 |
Load the immediate value 19 into the accumulator. |
ADD SCORE |
ADD |
SCORE |
Add the content stored at the location labelled SCORE. |
OUT |
OUT |
None | Output the character represented by the accumulator value. |
How Assembly Language Relates to Machine Code
For a given processor, an assembly mnemonic corresponds to a machine-code opcode. The assembler converts the mnemonic to its binary pattern and converts or resolves the operand into the representation expected by the instruction.
ADD SCORE
Readable mnemonic and symbolic operand
01010 01010000010
Illustrative binary encoding
The binary pattern above belongs to a fictional teaching processor used on this page. Real processors define their own instruction formats and encodings.
| Feature | Assembly language | Machine code |
|---|---|---|
| Opcode representation | Mnemonic such as ADD |
Binary opcode pattern |
| Operand representation | Number, register, address or symbol | Encoded binary field or fields |
| Readability | Designed for programmers | Designed for direct processor decoding |
| Execution | Must first be assembled | Can be fetched and decoded by the matching processor |
Labels, Symbolic Addresses and Comments
Assembly source can contain names that make the program easier to understand and modify. A label can identify an instruction or a memory location without requiring the programmer to know its final numerical address.
; Add a stored bonus to the current score
ADD BONUS
STO TOTAL
END
BONUS: 7
TOTAL: 0
| Source feature | Purpose | Example |
|---|---|---|
| Label | Provides a symbolic name for an instruction or data location. | BONUS: |
| Symbolic operand | Refers to the location identified by a label. | ADD BONUS |
| Comment | Documents the source for human readers and is not translated as a processor instruction. | ; Add a stored bonus |
The detailed process for assigning addresses to labels—including forward references—is covered in 4.2.2 How a Two-Pass Assembler Works.
The Role of an Assembler
During translation, an assembler carries out several related tasks:
- recognises valid mnemonics;
- looks up the corresponding binary opcodes;
- checks the required form of each operand;
- assigns or resolves addresses represented by labels;
- reports source-code errors it can detect;
- produces machine-code or object-code output.
Worked Example: Translating a Tiny Program
The following fictional processor uses a 16-bit instruction: a 5-bit opcode followed by an 11-bit operand field. The codes are original teaching examples rather than a real instruction set.
| Mnemonic | Illustrative 5-bit opcode |
|---|---|
LDM |
00101 |
ADD |
01010 |
OUT |
11100 |
END |
11111 |
Assume that the assembler has assigned address 642 to the label
SCORE.
| Assembly source | Assembler action | Illustrative machine instruction |
|---|---|---|
LDM #19 |
Look up LDM and encode 19 as an 11-bit value. |
00101 00000010011 |
ADD SCORE |
Look up ADD, resolve SCORE to 642 and encode the address. |
01010 01010000010 |
OUT |
Use the OUT opcode; this fictional format leaves the unused operand bits as zero. |
11100 00000000000 |
END |
Use the END opcode; no source operand is required. |
11111 00000000000 |
Interactive: Assembly-to-Machine Translator
Choose an assembly instruction and trace how the fictional assembler identifies the mnemonic, finds the opcode, resolves the operand and emits a 16-bit machine instruction.
Common Mistakes and Misconceptions
- The CPU executes machine code, not the assembly source text directly.
- A mnemonic represents an opcode; it is not the operand.
- Not every instruction requires an explicit operand.
- A symbolic label is resolved by the assembler; it is not stored as text in the final machine instruction.
- Assembly language and machine code are processor-specific.
- Comments help the programmer but are not translated into processor instructions.
- The fictional binary encodings on this page are teaching examples, not syllabus codes to memorise.
Practice
Core questions
- Explain the relationship between an assembly instruction and a machine-code instruction.
- Distinguish among opcode, operand and mnemonic.
- Explain why
OUTmay not require an explicit operand. - Explain why machine code written for one processor may not run on another processor.
- State two tasks performed by an assembler.
- Explain the purpose of the label in
LOOP: ADD TOTAL. - Explain why a comment is not represented in the final machine-code program.
Identify the parts
For each line, identify the mnemonic, operand and any label:
START: LDM #12
ADD BONUS
OUT
BONUS: 4
Reasoning challenge
A student says, “Assembly language is portable because the same word ADD can be used on many processors.” Explain why this conclusion is unreliable.
Review
| Concept | Essential idea |
|---|---|
| Machine code | Binary instructions recognised by a particular processor. |
| Assembly language | A processor-specific low-level language using mnemonics and readable operands. |
| Opcode | Identifies the operation. |
| Operand | Supplies a value, address, register or symbolic reference when needed. |
| Label | A symbolic name for an instruction or data location. |
| Assembler | Translates assembly source into machine code and resolves symbols. |