A-Level Computer Science / Unit 4: CPU Operation and Low-Level Processing

4.2.1 From Machine Code to Assembly Language

🔒 Lesson slides are available to signed-in users. Sign in

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.

Machine code: binary instructions encoded in the form recognised by a particular processor.

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.

Common misconception: Machine code is not a single universal language. A binary instruction for one processor family may have a different meaning—or no valid meaning—on another processor.

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.
Opcode: the part of an instruction that identifies the operation.
Operand: additional information used by the operation, such as a value, address, register or symbolic reference.

Some instructions need no explicit operand. For example, OUT uses the value already in the accumulator, while END terminates the program.

Common mistake: The operand does not define the operation. The opcode defines the operation; the operand supplies information used by it.

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.

Mnemonic: a short symbolic name used to represent an instruction's opcode.

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.
Exam tip: Describe assembly language as low-level and processor-specific. Saying only that it is “easier than binary” is incomplete.

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.

Assembly source ADD SCORE Readable mnemonic and symbolic operand
Assembler Looks up the opcode and resolves SCORE
Machine instruction 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

Assembler: system software that translates assembly-language source into machine code for a particular processor.

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.
Common misconception: The assembler does not execute the assembly program instruction by instruction. Its main role is translation.

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
Important: Do not memorise these fictional binary opcodes. The learning point is the translation relationship, not these particular codes.

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.

Fictional assembler Read the source instruction

The assembler separates the mnemonic from any operand before translation.

Assembly mnemonic LDM Textual opcode
Assembly operand #19 Value, symbol or none
Machine opcode 00101 5 bits
Machine operand 00000010011 11 bits in this fictional model
Mnemonic lookup LDM maps to opcode 00101.
Operand interpretation #19 represents the immediate value 19.
Machine instruction 00101 00000010011

Step 1 of 5

Read the assembly source

The assembler reads one source instruction and identifies its separate parts.

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

  1. Explain the relationship between an assembly instruction and a machine-code instruction.
  2. Distinguish among opcode, operand and mnemonic.
  3. Explain why OUT may not require an explicit operand.
  4. Explain why machine code written for one processor may not run on another processor.
  5. State two tasks performed by an assembler.
  6. Explain the purpose of the label in LOOP: ADD TOTAL.
  7. 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.
Final check: Can you explain the complete relationship: mnemonic → opcode lookup, operand → binary value or resolved address, assembly source → machine-code output?