9618 AS Computer Science
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!
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.
A machine code instruction is a binary code with a defined number of bits that comprises:
Programmers must consider:
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 | |
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.
Although assembly languages are easier to program with than machine code, they are still time-consuming and specialist work. They are mainly reserved for:
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.
Pass 1 - Analysis:
Pass 2 - Synthesis:
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.
LDD REVENUESUB COSTSTO PROFITAbsolute addressing refers directly to a memory location using numeric addresses. The computer cannot process symbolic addresses, so they must be converted to absolute addresses.
LDD #4454SUB #3326STO #4410The 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 |
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.
Assembly language instructions can be grouped into categories. Each category serves a specific purpose in program execution.
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 |
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 |
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 |
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 |
Remember: JPE = Jump if Previous Equal (true), JPN = Jump if Previous Not equal (false). The CMP instruction must come BEFORE the conditional jump!
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.
The operand IS the actual value to be used. No memory lookup is needed - the value is in the instruction itself.
Example: LDM #4
Options for defining the value:
#48 - Denary value 48#B00110000 - Binary equivalent#&30 - Hexadecimal equivalentThe operand is the memory address of the value to be used. Also called absolute addressing.
Example: LDD 200
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.
Example: LDI 200
The operand plus the contents of the Index Register (IX) gives the actual address to be used.
Example: LDX 200
The operand is an offset from the current instruction address. The actual address used is calculated by adding the offset to the current address.
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 |
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.
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
| 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 |
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 |
Computer arithmetic could lead to incorrect answers if overflow occurs. Values stored in the Status Register can identify specific conditions.
These flags are automatically set by the CPU after arithmetic operations. Conditional jump instructions (JPE, JPN) use these flags to make decisions!
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.
Answer:
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.
Answer:
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.
Answer:
LDM #4 loads the value 4 directly into the accumulatorLDD 200 loads the value stored at memory address 200 into the accumulatorAdditional points: Immediate addressing is faster (no memory lookup needed) but limited to constants. Direct addressing requires one memory access but allows dynamic values.
Answer:
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.
Answer:
LDI 200 - look at address 200 to find another address (e.g., 20), then use that address to find the actual dataAdditional 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.
Answer:
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.
Answer:
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.
Answer:
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.
Answer:
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.
Answer:
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.
Answer:
Additional point: Self-documenting code - the label names describe the purpose of the data.
Answer:
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.
Answer:
Additional points: The PC is incremented after fetch to point to the next instruction. Direct addressing means one memory access during execute phase.
Answer:
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.
Answer:
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.
| 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 |