Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
assembly-process
Two-pass assembler
What is a two-pass assembler?
-
A two-pass assembler translates assembly language into machine code in two stages
-
It produces an object program that can be stored, loaded, and run later
-
To execute this program, another utility called a loader is needed
-
The assembler scans the source code twice:
-
In the first pass, it identifies all labels and records their memory addresses
-
In the second pass, it replaces those labels with the correct memory addresses in the machine code
-
-
This ensures the final machine code is accurate and ready to be executed
Example
-
The following program loads the value of
A(5), adds the value ofB(3), stores the result (8) inC, then halts:
LOAD A ADD B STORE C HALT
A: DATA 5
B: DATA 3
C: DATA 0
-
Pass 1:
-
The assembler reads each line and assigns memory addresses to labels (e.g. A, B, C), but does not translate the instructions yet
-
|
Address |
Instruction |
Label |
Add to symbol table |
|---|---|---|---|
|
|
LOAD A |
||
|
|
ADD B |
||
|
|
STORE C |
||
|
|
HALT |
||
|
|
DATA 5 |
|
A = |
|
|
DATA 3 |
|
B = |
|
|
DATA 0 |
|
C = |
Symbol table created:
|
Label |
Address |
|---|---|
|
A |
|
|
B |
|
|
C |
|
-
Pass 2:
-
The assembler replaces labels with their corresponding memory addresses and outputs machine code
-
In this example we assume the following opcodes for simplicity:
-
|
Mnemonic |
Opcode |
|---|---|
|
LOAD |
01 |
|
ADD |
02 |
|
STORE |
03 |
|
HALT |
00 |
|
DATA |
– |
-
Now generate the object (machine) code:
|
Address |
Assembly |
Machine code |
Explanation |
|---|---|---|---|
|
|
LOAD A |
|
LOAD from address 04 |
|
|
ADD B |
|
ADD value at address 05 |
|
|
STORE C |
|
STORE into address 06 |
|
|
HALT |
|
Stop program |
|
|
DATA 5 |
|
Data value 5 |
|
|
DATA 3 |
|
Data value 3 |
|
|
DATA 0 |
|
Data value 0 (placeholder) |
Final object program:
01 04
02 05
03 06
00 00
00 05
00 03
00 00
Program tracing
What is a trace table?
-
A trace table is a table used to manually track the values of variables as a program runs
-
It helps to follow the flow of a program line by line, checking whether the logic works as expected
-
Trace tables are used to:
-
Understand how a program works
-
Debug errors in the logic
-
Predict outputs before running the program
-
Check variable values at each step
-
Example
-
An assembly program that loads the value from
A(4) into the accumulator, adds the value fromB(2), stores the result (6) intoC, and halts
LOAD A ADD B STORE C HALT
A: DATA 4
B: DATA 2
C: DATA 0
-
To trace the program, assumptions must be made:
-
Accumulator (ACC): Used for calculations
-
Memory is addressed from 00 onwards
-
Simple instructions:
-
LOAD X→ ACC = memory[X] -
ADD X→ ACC = ACC + memory[X] -
STORE X→ memory[X] = ACC -
HALT→ Stop
-
-
Trace table
|
Step |
Instruction |
ACC (Accumulator) |
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 outcome:
-
ACC = 6
-
C = 6
-
The program has successfully added A and B, then stored the result in C
Responses