📑 Contents

Chapter 4.2: Assembly Language

9618 AS Computer Science

📚 Learning Objectives
🌟 Did You Know?

Assembly language is a low-level programming language that equates to machine code but is more readable. It uses mnemonics to represent instructions, making it easier for programmers to understand. Each CPU has its own version of machine code and assembly language!

High Level Languages Python, C++, Java Assembly Language Mnemonics: LDD, ADD, STO Machine Code Binary: 0100 01101001 Compiler Assembler CPU Executes

1. Machine Code vs Assembly Language

1.1 What is Machine Code?

Machine code is the only programming language that a CPU can directly use. It is a first-generation language written in binary code. Every different type of computer chip has its own set of machine code instructions.

📖 Features of Machine Code

1.2 Structure of Machine Instructions

A machine code instruction is a binary code with a defined number of bits that comprises:

📝 Components of Machine Instruction

Programmers must consider:

1.3 What is Assembly Language?

Assembly language is a second-generation language - a low-level language related to machine code where opcodes are written as mnemonics (abbreviated text commands) and there is a character representation for an operand.

Feature Machine Code Assembly Language
Generation 1st Generation 2nd Generation
Format Binary (0s and 1s) Mnemonics (LDD, ADD, STO)
Readability Very difficult for humans Human-readable
Translation Direct execution Requires Assembler
Relationship One assembly instruction = One machine code instruction
LDD Opcode 200 Operand (memory address) "What to do" "Where to find data"

2. Assemblers and The Assembly Process

2.1 What is an Assembler?

An assembler is a language translator used to convert assembly language programs into machine code. It checks syntax to ensure only opcodes from the appropriate instruction set are used.

📖 Key Points About Assemblers

2.2 Uses of Assembly Language

Although assembly languages are easier to program with than machine code, they are still time-consuming and specialist work. They are mainly reserved for:

Common Uses: Writing specific instructions for embedded systems such as washing machines, air-conditioning control, device drivers, and real-time systems.

2.3 Two-Pass Assembler

A two-pass assembler translates assembly language into machine code in two stages. It scans the source code twice to ensure accurate machine code generation.

📝 Two-Pass Assembler Process

Pass 1 - Analysis:

Pass 2 - Synthesis:

Assembly Code PASS 1 Analysis Build Symbol Table Symbol Table PASS 2 Synthesis Generate Machine Code Object Code

3. Symbolic vs Absolute Addressing

3.1 Symbolic Addressing

Symbolic addressing uses labels (identifier names) instead of memory addresses. This is how we write programs in high-level languages - using variable names, functions, and data structures.

📖 Advantages of Symbolic Addressing
Example Program (Symbolic):
LDD REVENUE
SUB COST
STO PROFIT
This calculates profit = revenue - cost (much more readable!)

3.2 Absolute Addressing

Absolute addressing refers directly to a memory location using numeric addresses. The computer cannot process symbolic addresses, so they must be converted to absolute addresses.

Same Program (Absolute):
LDD #4454
SUB #3326
STO #4410
Same logic, but much harder to understand the purpose!

3.3 Symbol Table

The assembler builds a symbol table containing all symbolic names and their corresponding memory addresses. This is created during Pass 1.

Label Address
REVENUE 4454
COST 3326
PROFIT 4410
LOAD A ADD B STORE C A: DATA 5 Pass 1 Symbol Table A → 04 B → 05 C → 06 Pass 2 Machine Code 01 04 (LOAD from 04) 02 05 (ADD from 05) 03 06 (STORE to 06)
💡 Exam Tip

Remember: Labels are for humans (readability), Addresses are for computers (execution). The assembler's job is to convert one to the other using the symbol table.

4. Instruction Groups

Assembly language instructions can be grouped into categories. Each category serves a specific purpose in program execution.

4.1 Data Movement Instructions

Data movement instructions allow data stored at one location to be copied into the accumulator. This data can then be stored elsewhere, used in calculations, comparisons, or output.

Opcode Operand Explanation
LDD <address> Direct Addressing: Load content of <address> to ACC
LDI <address> Indirect Addressing: Load content of address stored at <address>
LDX <address> Indexed Addressing: Load from <address + IX>
LDR #n Immediate: Load number n to Index Register (IX)
LDM #n Immediate: Load number n to Accumulator (ACC)
MOV <register> Move contents of ACC to the register (e.g., IX)
STO <address> Store contents of ACC into specified address

4.2 Input and Output Instructions

These instructions allow data to be read from input devices (keyboard) or sent to output devices (screen).

Opcode Explanation
IN Take input from keyboard and store its ASCII value in ACC
OUT Output the character stored in ACC to the screen
Memory Address 200 Value: 78 ACC Accumulator IX Register Output Screen LDD 200 OUT

4.3 Arithmetic Operation Instructions

These instructions perform calculations on data stored in the accumulator. The result is stored back in the accumulator, overwriting the original data.

Opcode Operand Explanation
ADD <address> Add the contents of <address> to ACC
SUB <address> Subtract the contents of <address> from ACC
INC <register> Increment the contents of register (ACC or IX) by 1
DEC <register> Decrement the contents of register (ACC or IX) by 1

4.4 Unconditional and Conditional Instructions

A jump means changing the Program Counter (PC) to a specified address, so the next instruction executed is from that address, not the next sequential location.

Opcode Operand Explanation
JMP <address> Unconditional jump to the specified address
JPE <address> Jump to address if previous compare was TRUE (equal)
JPN <address> Jump to address if previous compare was FALSE (not equal)
CMP <address> Compare contents of ACC with contents of <address>
CMP #n Compare contents of ACC with the number n
CMI <address> Indirect: Compare ACC with contents at address stored in <address>
END - Return control to the operating system
💡 Exam Tip

Remember: JPE = Jump if Previous Equal (true), JPN = Jump if Previous Not equal (false). The CMP instruction must come BEFORE the conditional jump!

CMP #10 Equal? JPE Label Continue... YES NO

5. Addressing Modes

An addressing mode defines how a value should be found when it needs to be loaded into a register. When an instruction requires a value, there are different ways of identifying that value.

5.1 Immediate Addressing

The operand IS the actual value to be used. No memory lookup is needed - the value is in the instruction itself.

📖 Immediate Addressing

Example: LDM #4

Options for defining the value:

5.2 Direct Addressing

The operand is the memory address of the value to be used. Also called absolute addressing.

📖 Direct Addressing

Example: LDD 200

Immediate Addressing LDM #4 → ACC = 4 ACC: 4 Direct Addressing LDD 200 → look up Address: 200 Value: 78 ACC: 78

5.3 Indirect Addressing

The operand is the address of the address to be used. This is similar to direct addressing but with one more step - it "hops" twice.

📖 Indirect Addressing

Example: LDI 200

5.4 Indexed Addressing

The operand plus the contents of the Index Register (IX) gives the actual address to be used.

📖 Indexed Addressing

Example: LDX 200

Indirect: LDI 200 LDI 200 Addr: 200 → 20 Addr: 20 → 5 ACC = 5 Indexed: LDX 200 LDX 200 IX = 4 200 + 4 = 204 Addr: 204 → 17 ACC = 17

5.5 Relative Addressing

The operand is an offset from the current instruction address. The actual address used is calculated by adding the offset to the current address.

📖 Relative Addressing

Example: JMP +5

Mode Description Example Result
Immediate Operand IS the value LDM #4 ACC = 4
Direct Operand is the address LDD 200 ACC = memory[200]
Indirect Operand points to address LDI 200 ACC = memory[memory[200]]
Indexed Operand + IX register LDX 200 ACC = memory[200 + IX]
Relative Current address + offset JMP +5 PC = PC + 5
Addr 100 Addr 105 Addr 110 Addr 115 Addr 120 Current JMP +15 (relative) Target
🧠 Memory Trick: Addressing Modes

6. Tracing Assembly Programs

A trace table is used to manually track the values of variables as a program runs. It helps follow the flow line by line, checking whether logic works as expected.

📖 Purpose of Trace Tables

6.1 Example Program

📝 Program: Add Two Numbers
LOAD A      ; Load value from A into ACC
ADD B       ; Add value from B to ACC  
STORE C     ; Store result in C
HALT        ; Stop program
A: DATA 4   ; Data value 4
B: DATA 2   ; Data value 2
C: DATA 0   ; Placeholder for result

6.2 Trace Table

Step Instruction ACC Memory[A] Memory[B] Memory[C]
0 (Start) 0 4 2 0
1 LOAD A 4 4 2 0
2 ADD B 6 4 2 0
3 STORE C 6 4 2 6
4 HALT 6 4 2 6
Final Result: ACC = 6, C = 6. The program successfully added A (4) and B (2), storing the result (6) in C.
LOAD A ACC = 4 ADD B ACC = 4+2=6 STORE C C = 6 HALT Done!

7. Bitwise Operations & Computer Arithmetic

7.1 Bitwise Logic Operations

Bitwise operations work on individual bits of data. The operand for these operations is called a mask because it can effectively "cover" some bits and only affect specific bits.

Operation Description Example
AND Bits are 1 only if BOTH inputs are 1 1010 AND 1100 = 1000
OR Bits are 1 if EITHER input is 1 1010 OR 1100 = 1110
XOR Bits are 1 if inputs are DIFFERENT 1010 XOR 1100 = 0110
NOT Inverts all bits NOT 1010 = 0101

7.2 Status Register Flags

Computer arithmetic could lead to incorrect answers if overflow occurs. Values stored in the Status Register can identify specific conditions.

📖 Status Register Flags
💡 Exam Tip

These flags are automatically set by the CPU after arithmetic operations. Conditional jump instructions (JPE, JPN) use these flags to make decisions!

8. Glossary

📖 Key Terms

Accumulator (ACC): A register where arithmetic and logic results are stored.

Addressing Mode: The method used to identify the location of data for an instruction.

Assembler: A program that translates assembly language into machine code.

Assembly Language: A low-level programming language using mnemonics to represent machine code instructions.

Direct Addressing: The operand specifies the memory address of the data.

Immediate Addressing: The operand is the actual data value to be used.

Indexed Addressing: The address is calculated by adding the operand to the index register.

Index Register (IX): A register used in indexed addressing to offset the base address.

Indirect Addressing: The operand points to a memory location that contains the actual address of the data.

Instruction Set: The complete collection of instructions that a CPU can execute.

Machine Code: Binary code that the CPU can directly execute.

Mnemonic: A short, human-readable code representing a machine instruction (e.g., LDD, ADD).

Opcode: The part of an instruction that specifies the operation to be performed.

Operand: The part of an instruction that specifies the data or address for the operation.

Relative Addressing: The address is calculated as an offset from the current instruction address.

Status Register: A register containing flags that indicate the status of the CPU after operations.

Symbol Table: A table created by the assembler mapping labels to memory addresses.

Two-Pass Assembler: An assembler that scans the source code twice - first to build the symbol table, then to generate machine code.

9. Exam-Style Questions

1. Explain the relationship between assembly language and machine code. [4 marks]

Answer:

  • Assembly language is a low-level language related to machine code
  • Assembly uses mnemonics to represent machine code instructions
  • There is a one-to-one relationship between assembly instruction and machine code instruction
  • Assembly language must be translated by an assembler into machine code before execution

Additional points for deeper understanding: Machine code is in binary format directly executable by the CPU, while assembly language is human-readable. Each CPU has its own specific machine code and assembly language.

2. Describe the purpose of a two-pass assembler and explain what happens in each pass. [6 marks]

Answer:

  • A two-pass assembler translates assembly language into machine code in two stages
  • Pass 1: Scans the program looking for symbols, labels, and variables
  • Pass 1 creates a symbol table containing labels and their addresses
  • Pass 1 maintains a Location Counter to assign addresses to each instruction
  • Pass 2: Replaces symbolic addresses with absolute addresses using the symbol table
  • Pass 2 replaces symbolic opcodes with binary opcodes to generate machine code

Additional points: Two passes are needed because forward references (jumping to labels defined later in the code) cannot be resolved in a single pass. The symbol table from Pass 1 enables correct address resolution in Pass 2.

3. Explain the difference between immediate addressing and direct addressing, giving an example of each. [4 marks]

Answer:

  • Immediate addressing: The operand IS the actual value to be used
  • Example: LDM #4 loads the value 4 directly into the accumulator
  • Direct addressing: The operand is the memory address where the value is stored
  • Example: LDD 200 loads the value stored at memory address 200 into the accumulator

Additional points: Immediate addressing is faster (no memory lookup needed) but limited to constants. Direct addressing requires one memory access but allows dynamic values.

4. Explain how indexed addressing works and give one situation where it would be useful. [4 marks]

Answer:

  • The operand plus the contents of the Index Register (IX) gives the actual address
  • Example: If IX = 4 and instruction is LDX 200, the actual address is 200 + 4 = 204
  • The value at address 204 is loaded into the accumulator
  • Useful for: Accessing arrays or lists - the base address is the operand, and IX acts as the index

Additional points: By incrementing IX in a loop, the program can sequentially access array elements. This is similar to array[i] in high-level languages where 'i' is the index.

5. Describe what is meant by indirect addressing and explain why it might be used instead of direct addressing. [4 marks]

Answer:

  • In indirect addressing, the operand is the address of the address to be used
  • Example: LDI 200 - look at address 200 to find another address (e.g., 20), then use that address to find the actual data
  • It is used for pointers and dynamic data structures
  • It allows the same instruction to access different data by changing the pointer

Additional points: Indirect addressing provides flexibility - the data location can change without modifying the instruction. Useful for linked lists, parameter passing, and implementing variables in high-level languages.

9. Exam-Style Questions (Continued)

6. What is a symbol table and why is it needed in the assembly process? [4 marks]

Answer:

  • A symbol table is a table created during Pass 1 of assembly that maps labels to memory addresses
  • It contains all symbolic names used in the program and their corresponding addresses
  • It is needed because the computer cannot process symbolic addresses directly
  • During Pass 2, labels in instructions are replaced with actual addresses from the symbol table

Additional points: Without a symbol table, forward references (jumping to labels not yet defined) would be impossible to resolve. It enables programmers to use meaningful names instead of numeric addresses.

7. Explain the difference between the opcode and the operand in an assembly language instruction. [3 marks]

Answer:

  • The opcode tells the CPU what operation to perform (e.g., LDD, ADD, STO)
  • The operand provides the data or address needed to complete the operation
  • Example: In ADD 200, ADD is the opcode (add operation), and 200 is the operand (address of value to add)

Additional points: Some instructions may not require an operand (e.g., HALT). Some may have multiple operands in more complex processors.

8. A program contains the instruction: JMP LOOP. Explain how this instruction would be processed by a two-pass assembler. [5 marks]

Answer:

  • Pass 1: The assembler records the label "LOOP" and its address in the symbol table
  • The JMP instruction itself is noted but not yet translated
  • Pass 2: The assembler looks up "LOOP" in the symbol table to find its address
  • The label "LOOP" is replaced with the actual numeric address
  • The opcode JMP is translated to its binary equivalent, producing complete machine code

Additional points: If LOOP is defined after the JMP instruction (forward reference), two passes are essential. Pass 1 identifies where LOOP is, Pass 2 uses that information.

9. Describe the purpose of a trace table and explain how it is used to debug an assembly language program. [4 marks]

Answer:

  • A trace table tracks the values of variables as a program executes step by step
  • Each row shows the state after each instruction: register values, memory contents, flags
  • It helps identify logic errors by showing where values go wrong
  • Programmers can compare expected values with actual values at each step

Additional points: Trace tables are essential for exam questions that ask you to trace a program. Always show the starting values and update each column as instructions execute.

10. Explain the purpose of the CMP instruction and describe how it is used with conditional jump instructions. [5 marks]

Answer:

  • CMP compares the contents of the accumulator with a value or memory contents
  • It sets flags in the status register based on the comparison result
  • The comparison is done by subtraction: ACC - operand (result is discarded, only flags are set)
  • JPE (Jump if Previous Equal) jumps if comparison was true (values equal)
  • JPN (Jump if Previous Not equal) jumps if comparison was false (values not equal)

Additional points: The CMP instruction must come BEFORE the conditional jump. Multiple conditional jumps can follow one CMP (if checking same comparison). CMP does not change the accumulator value.

9. Exam-Style Questions (Continued)

11. State three advantages of using symbolic addressing instead of absolute addressing in assembly language programs. [3 marks]

Answer:

  • Makes programs easier to read and understand - labels like "TOTAL" are more meaningful than addresses like "2345"
  • Makes programs easier to modify - if a variable's address changes, only need to change it once in the symbol table
  • Reduces errors - programmers don't need to remember and track numeric addresses

Additional point: Self-documenting code - the label names describe the purpose of the data.

12. Explain the role of the accumulator in assembly language programming. [4 marks]

Answer:

  • The accumulator is a special register used for arithmetic and logic operations
  • It holds one of the operands for calculations and stores the result of the operation
  • Data must be loaded into the accumulator before it can be processed
  • It acts as a temporary storage area during data manipulation

Additional points: In simple processors, the accumulator is the only general-purpose register. Most instructions implicitly use the accumulator - for example, ADD automatically adds to the accumulator contents.

13. Explain what happens during the fetch-decode-execute cycle when executing the instruction LDD 150. [6 marks]

Answer:

  • Fetch: The instruction LDD 150 is fetched from memory using the Program Counter (PC)
  • The instruction is stored in the Memory Data Register (MDR)
  • Decode: The Control Unit decodes the instruction to understand it's a load operation
  • The operand 150 is identified as a memory address
  • Execute: The address 150 is sent to memory via the address bus
  • The data at address 150 is retrieved and stored in the accumulator

Additional points: The PC is incremented after fetch to point to the next instruction. Direct addressing means one memory access during execute phase.

14. Describe how relative addressing differs from direct addressing, and give one use for each. [4 marks]

Answer:

  • Direct addressing: Uses an absolute memory address specified in the instruction
  • Used for accessing fixed data locations or variables at known addresses
  • Relative addressing: Calculates the address by adding an offset to the current instruction address
  • Used for loops and conditional jumps where code might be relocated

Additional points: Relative addressing produces position-independent code - the program works correctly regardless of where it's loaded in memory. Direct addressing is simpler but less flexible.

15. Explain why assembly language programs require more instructions than equivalent high-level language programs. [3 marks]

Answer:

  • Each assembly instruction performs a very simple task (load, store, add)
  • High-level language statements must be broken down into many smaller operations
  • Example: "result = a + b + c" in high-level language needs multiple loads, adds, and store in assembly

Additional points: High-level languages provide built-in abstractions (loops, functions, objects) that compile to many machine instructions. Assembly programmers must code every detail explicitly.

10. Exam Success Tips (Part 1)

💡 Addressing Modes - Quick Reference
💡 Two-Pass Assembler - Remember This!
💡 CMP and Jumps - The Order Matters!
💡 Tracing Programs - Step by Step
💡 Common Instruction Patterns

10. Exam Success Tips (Part 2)

❌ Common Mistakes to Avoid
🧠 Memory Tricks
⚠️ Key Points for Exam Questions
🌟 Quick Reference Table
Symbol Meaning Example
# Denary number #48
B Binary number B00110000
& Hexadecimal number &30
<address> Memory address 200, MyLabel
ACC Accumulator Main working register
IX Index Register Used in indexed addressing

11. Key Takeaways

📌 Chapter Summary

Machine Code & Assembly Language

Two-Pass Assembler

Addressing Modes

Instruction Groups

Assembly Mnemonics Labels, Opcodes Assembler Pass 1 Pass 2 Machine Code Binary 0100 0110...