CIE IGCSE | 4.2.2 Assembly and Machine Code

Lesson objective

Explain assembly language and machine code, identify mnemonics, opcodes and operands, and describe why an assembler is needed.

Learn

4.2.2 | ASSEMBLY AND MACHINE CODE

01 | TWO REPRESENTATIONS OF PROCESSOR INSTRUCTIONS

Assembly language is a low-level language that uses short, memorable names called mnemonics for processor instructions.

Machine code consists of encoded instructions that the target processor can execute directly. These encodings are commonly shown as binary values, although hexadecimal may be used to make them easier for people to read.

Assembly is easier for a person to interpret than raw machine code, but the processor does not directly execute the assembly source text. An assembler is needed to translate it.

02 | FIRST, UNDERSTAND THE INSTRUCTION

An instruction specifies an operation for the processor to carry out. Some instructions also specify information needed for that operation.

In this lesson, operands are memory addresses unless stated otherwise
Part of a teaching instructionMeaning
ADDAdd a value to the accumulator
21Use the value stored at memory address 21

The whole assembly instruction is ADD 21. It means “add the value at address 21 to the current accumulator value”. It does not mean “add the literal number 21”.

Different processors and addressing modes have different rules. Here we use one simple, explicitly defined model to avoid mixing those rules.

03 | MNEMONIC, OPCODE AND OPERAND

An assembler maps the mnemonic and operand into their target encodings
TermPurposeExample in our teaching model
MnemonicReadable assembly name for an operationADD
OpcodeMachine-instruction field identifying the operation0010 means ADD
OperandInformation identifying data or a location to useAddress 21, encoded as 00010101

The mnemonic is a name used in assembly source. The encoded opcode tells the processor which operation to perform. The operand here identifies where to obtain the data.

An operand is not always an address: other instruction formats can use a literal value or a register. Some instructions have no explicit operand, while others have several. Always follow the format given in the question.

04 | DEFINE OUR TEACHING PROCESSOR

For the worked examples, imagine a processor with an accumulator (ACC), a register that holds working values and calculation results.

These are invented instructions, not a specific real processor’s syntax
MnemonicInvented opcodeEffect
LOAD0001Copy the value at the operand address into ACC
ADD0010Add the value at the operand address to ACC
STORE0011Copy ACC to the operand address; leave ACC unchanged

Each instruction is 12 bits: a 4-bit opcode followed by an 8-bit address operand. The operand can encode addresses 0 to 255.

This model is for understanding the relationship between representations. Real processors can use different instruction lengths, encodings and register arrangements.

05 | SEE THE TWO FIELDS

Together, the fields form 0010 00010101. The space separates the fields for readability; it is not an extra bit.

The operand value is 21 because 00010101 represents 16 + 4 + 1. The opcode’s meaning comes from the processor’s instruction set, not from treating 0010 as a quantity to add.

06 | WHAT THE ASSEMBLER DOES

An assembler translates assembly source into the corresponding machine code for the target processor.

LOAD 20
ADD 21
STORE 22
Translation for the invented processor defined above
Assembly sourceOpcode fieldAddress fieldMachine instruction
LOAD 200001000101000001 00010100
ADD 210010000101010010 00010101
STORE 220011000101100011 00010110

The assembler recognises each mnemonic, selects its opcode and encodes the operand according to the target format.

Translation is not execution. Producing these encodings does not itself add the numbers. The processor executes the resulting instructions when the program runs.

In this simple model, each assembly instruction corresponds to one machine instruction. Real assemblers may also support directives or pseudo-instructions, so not every line of an assembly source file must create exactly one instruction.

07 | TRACE THE PROGRAM

Initially, ACC = 0. Memory address 20 contains 7, address 21 contains 5, and address 22 contains 0.

Follow the data value separately from its address
StepInstructionWhat happensACC afterwardsMemory 22 afterwards
StartNone yetInitial state00
1LOAD 20Copy the value 7 from memory 20 into ACC70
2ADD 21Add the value 5 from memory 21 to ACC120
3STORE 22Copy ACC to memory 221212

The result is 12, not 41. Addresses 20 and 21 tell the processor where the numbers are; they are not the numbers being added.

STORE changes the value in memory 22. It does not alter the earlier values at addresses 20 and 21 in this model.

08 | CONNECT IT TO FETCH, DECODE, EXECUTE

When the program runs, its machine instructions are stored in memory. The processor fetches an instruction, decodes it and executes it.

For our encoded ADD 21, the instruction register holds 0010 00010101. The control unit interprets the opcode as ADD and the operand as address 21. The value at that address is obtained and added to ACC using the ALU.

The assembler created the encoding before execution. The control unit interprets the encoding during execution. These are different jobs.

In the earlier trace, ACC was 7 and memory 21 held 5, so executing the ADD instruction produces ACC = 12.

09 | WHY USE ASSEMBLY?

Mnemonics are easier to recognise than long sequences of encoded bits. Assembly also exposes details such as processor operations, registers and addresses, which can be useful for specialised hardware work.

However, it remains harder to write and debug than many high-level programs. A wrong operand can refer to the wrong location even if the instruction is valid.

Assembly is machine dependent: its instructions and translation must match the target architecture. An assembler for a different instruction set cannot simply give the same source an equivalent meaning without appropriate support or changes.

10 | VALID CODE CAN STILL BE WRONG

Suppose the programmer accidentally writes ADD 22 instead of ADD 21. The assembler may accept it because address 22 is valid.

In the initial example, address 22 holds 0, so the program would produce 7 instead of 12. This is a logic error, not necessarily a syntax error.

An assembler can report invalid source forms or unsupported instructions, but it cannot guarantee that a valid program solves the intended problem. The program still needs testing.

11 | COMPARE ASSEMBLY AND MACHINE CODE

Both are low-level representations; their roles differ
PointAssembly languageMachine code
RepresentationMnemonics and operandsEncoded instruction bits
Human readabilityEasier to interpret than raw encodingsHarder to interpret directly
ExecutionNeeds an assembler to translate sourceDirectly executable by the matching processor
Processor relationshipTied to the target instruction setTied to the target instruction set

Exam-style explanation: assembly uses mnemonics such as ADD, whereas the processor executes encoded machine instructions. Therefore an assembler is required to translate the source into machine code matching the target instruction set.

12 | COMMON MISTAKES

  • Assembly executes directly: its source must be translated by an assembler.
  • The mnemonic and operand are the same: the mnemonic names the operation; the operand supplies information it uses.
  • An address operand is always a literal value: follow the addressing model given.
  • The assembler performs the calculation: translation produces instructions; execution performs their operations.
  • A machine-code instruction is just data to add: its fields specify an operation and relevant information.
  • Every processor uses these encodings: this lesson’s format is invented.
  • Successful assembly guarantees a correct result: logic errors still need testing.

MATCH THE INSTRUCTION TERM

Terminology

Terminology

Assembly language

A low-level language using mnemonics for processor instructions.

Machine code

Encoded instructions a matching processor can execute directly.

Assembler

A translator converting assembly source into machine code.

Mnemonic

A short, memorable assembly name for an instruction.

Opcode

The field of an encoded instruction identifying the operation.

Operand

Information specifying data or a location used by an instruction.

Instruction

A command specifying an operation for the processor.

Instruction set

The machine instructions supported by a processor.

Accumulator (ACC)

A register holding working values or arithmetic results in our teaching model.

Memory address

A reference identifying a memory location, distinct from its stored value.

Source code

Instructions written in a source language, such as assembly.

Translation

Converting a source representation into another form, such as machine code.

Execution

Carrying out the operations specified by instructions.

Machine dependence

Being tied to a particular target architecture or instruction set.

Logic error

A fault that causes incorrect behaviour despite code being structurally valid.

Instruction trace

A step-by-step record of the effects of executing instructions.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which statements about assembly are correct?
2. What does an assembler do?
3. In our teaching ADD 21, which statements are correct?
4. In our 12-bit model, which fields are correct?
5. Memory 20 holds 7 and memory 21 holds 5. After LOAD 20 then ADD 21, what is ACC?
6. In our model, what does STORE 22 do?
7. Why can ADD 22 be accepted but wrong for the intended program?
8. Which comparisons are correct?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Define assembly language.

2. Explain why an assembler is needed.

3. Compare a mnemonic and an opcode.

4. Describe the meaning of ADD 21 in this lesson’s model.

5. Describe the difference between translation and execution.

6. Explain why assembly is machine dependent.

7. Explain why successfully assembled code can still produce an incorrect result.

APPLY IT | TRACE AND CHANGE

Memory 20 contains 9, memory 21 contains 4 and memory 22 initially contains 0. Trace LOAD 20, ADD 21, STORE 22. Then predict the result if ADD 21 is replaced with ADD 22, using the same initial state.

Reveal suggested answer

Original: ACC becomes 9, then 13; STORE writes 13 to memory 22 and leaves ACC at 13. Changed version: LOAD gives 9, ADD 22 adds the initial 0, and STORE writes 9. The source can remain valid while the result changes.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 4.2.2 Assembly and Machine Code is coming soon. Complete the matching task and written questions in your exercise book.