System Programming
System Programming
System Programming
Chih-Hung Wang 1
Chapter 2: Assembler (Part-1)
參考書目
Leland L. Beck, System Software: An Introduction to
Systems Programming (3rd), Addison-Wesley, 1997.
Role of Assembler
Source Object
Executable
Code
Loader
2
Chapter 2 -- Outline
Basic Assembler Functions
Machine-dependent Assembler Features
Machine-independent Assembler Features
Assembler Design Options
3
Introduction to Assemblers
Fundamental functions
Translating mnemonic operation codes to their machine language
equivalents
Assigning machine addresses to symbolic labels
Machine dependency
Different machine instruction formats and codes
4
Example Program (Fig. 2.1)
Purpose
Reads records from input device (code F1)
Copies them to output device (code 05)
At the end of the file, writes EOF on the output device, then
RSUB to the operating system
Program (See Fig. 2.1)
5
SIC Assembly Program (Fig. 2.1)
Line numbers Mnemonic opcode
(for reference)
comments
Address labels
operands
6
SIC Assembly Program (Fig. 2.1)
Indicate comment lines
Index addressing 7
SIC Assembly Program (Fig. 2.1)
8
Example Program (Fig. 2.1)
Data transfer (RD, WD)
a buffer is used to store record
buffering is necessary for different I/O rates
the end of each record is marked with a null character (0016)
the end of the file is indicated by a zero-length record
9
Assembler Directives
Pseudo-Instructions
Not translated into machine instructions
Providing information to the assembler
10
Object Program
Header
Col. 1 H
Col. 2~7 Program name
Col. 8~13 Starting address (hex)
Col. 14-19 Length of object program in bytes (hex)
Text
Col.1 T
Col.2~7 Starting address in this record (hex)
Col. 8~9 Length of object code in this record in bytes (hex)
Col. 10~69 Object code (69-10+1)/6=10 instructions
End
Col.1 E
Col.2~7 Address of first executable instruction (hex)
(END program_name)
11
Fig. 2.3 (Object Program)
12
Assembler Tasks
The translation of source program to object code requires
us the accomplish the following functions:
Convert mnemonic operation codes to their machine language equ
ivalents (e.g. translate STL to 14 - Line 10)
Convert symbolic operands to their equivalent machine addresse
s format (e.g. translate RETARD to 1033 - Line 10)
Build machine instructions in the proper format
Convert the data constants specified in the source program int
o their internal machine representations (e.g. translate EOF t
o 454F46) - Line 80
Write object program and the assembly listing
13
Example of Instruction Assem
ble
STCH BUFFER,X 549039
8 1 15
opcode x address
m
(54)16 1 (001)2 (039)16
Forward reference
14
Forward Reference
A reference to a label (RETADR) that is defined later in
the program
Solution
Two passes
First pass: does little more than scan the source program for label
definition and assign addresses (such as those in the Loc column in
Fig. 2.2).
Second pass: performs most of the actual instruction translation pr
eviously defined.
15
Difficulties: Forward Refere
nce
Forward reference: reference to a label that is defined l
ater in the program.
16
Two Pass SIC Assembler
Pass 1 (define symbols)
Assign addresses to all statements in the program
Save the addresses assigned to all labels for use in Pass 2
Perform assembler directives, including those for address ass
ignment, such as BYTE and RESW
Pass 2 (assemble instructions and generate object pr
ogram)
Assemble instructions (generate opcode and look up addresses)
Generate data values defined by BYTE, WORD
Perform processing of assembler directives not done during Pa
ss 1
Write the object program and the assembly listing
17
Two Pass SIC Assembler
Read from input line
LABEL, OPCODE, OPERAND
Source
program
Intermediate Object
Pass 1 Pass 2
file codes
18
Assembler Data Structures
Operation Code Table (OPTAB)
Symbol Table (SYMTAB)
Location Counter (LOCCTR)
OPTAB
Pass 1
Intermediate Object
file Program
Source
Pass 2
SYMTA
LOCCTR
B
19
Location Counter (LOCCTR)
A variable that is used to help in the assignment of addr
esses, i.e., LOCCTR gives the address of the associated l
abel.
LOCCTR is initialized to be the beginning address specifi
ed in the START statement.
After each source statement is processed during pass 1, t
he length of assembled instruction or data area to be gen
erated is added to LOCCTR.
20
Operation Code Table (OPTA
B)
Contents:
Mnemonic operation codes (as the keys)
Machine language equivalents
Instruction format and length
Note: SIC/XE has instructions of different lengths
During pass 1:
Validate operation codes
Find the instruction length to increase LOCCTR
During pass 2:
Determine the instruction format
Translate the operation codes to their machine language equivalents
21
COPY 1000
SYMTAB
FIRST 1000
CLOOP 1003
ENDFIL 1015
EOF 1024
THREE 102D
Contents: ZERO 1030
Label name RETADR 1033
Label address LENGTH 1036
Flags (to indicate error conditions) BUFFER 1039
RDREC 2039
Data type or length
During pass 1:
Store label name and assigned address (from LOCCTR) in SYMTAB
During pass 2:
Symbols used as operands are looked up in SYMTAB
Implementation:
a dynamic hash table for efficient insertion and retrieval
Should perform well with non-random keys (LOOP1, LOOP2).
22
Fig. 2.2 (1) Program with Ob
ject code
23
Fig. 2.2 (2) Program with Ob
ject code
24
Fig. 2.2 (3) Program with Ob
ject code
25
Figure 2.1 (Pseudo code Pass
1)
26
Figure 2.1 (Pseudo code Pass
1)
27
Figure 2.1 (Pseudo code Pass
2)
28
Figure 2.1 (Pseudo code Pass
2)
29