Machine-Dependent Assembler Features
Machine dependent assembler features z
z
Instruction formats and addressing modes Program relocation
• Generate an object program that can be placed anywhere in the physical memory.
Machine-dependent assembler features
2
1
Review of instruction formats 8
z
SIC Format
z
Format 1 (1 byte)
z
Format 2 (2 bytes)
z
Format 3 (3 bytes)
z
Format 4 (4 bytes)
1
15
opcode x
address
8 op
SIC/XE
8
4
4
op
r1
r2
6 op 6 op
1 11 1 1 1
12
n i x b p e disp 1 11 1 1 1
20
n i x b p e address
Machine-dependent assembler features
3
Review of instruction formats (cont’d) z
Register-to-register instructionsop r1,r2
• z
Register-to-register instructions are shorter and do not require another memory reference. Use register-toregister instructions instead of register-to-memory instructions whenever possible.
Extended format (4 bytes):
• •
+op
If the displacements required for both program-counter relative and base relative addressing are too large to fit into a 3-byte instruction, then the 4-byte extended format must be used. The programmer has to specify this form of addressing when it is required. Machine-dependent assembler features
4
2
Review of addressing modes z
Immediate addressing:
• • z
Indirect addressing:
• • z
op #c; op #m
Using the immediate addressing, the operand is already present as part of the instruction and need not be fetched from anywhere. 55 LDA #3
op @m; op @c
The use of indirect addressing often avoids the need for another instruction. 70 J @RETADR
PC-relative or Base-relative addressing:
•
Instructions that refer to memory are normally assembled using either
• •
the program-counter relative mode or the base relative mode.
z
• The assembler directive BASE is used in conjunction with base relative addressing. Index addressing: op m,X
z
Direct addressing:
•
160
STCH
BUFFER,X
Machine-dependent assembler features
5
Addressing modes for SIC/XE Flag bits p bit=1: program-counter relative addressing mode e bit=1: extended instruction format Assembler language notation c indicates a constant between 0 and 4095. m indicates a memory address or a constant value larger than 4095. Note 4 Format 4 instruction D Direct-addressing instruction A Assembler selects either programcounter relative or base-relative mode S Compatible with instruction format for standard SIC machine Machine-dependent assembler features
6
3
“START 0” z
It indicates a relocatable program.
• The program will be translated exactly as if it were to be loaded at machine address 0.
Machine-dependent assembler features
7
Register-to-register instructions z
The translation of register-to-register instructions must convert the register mnemonics to numbers.
• •
The register mnemonics can be stored in a predefined table, or in the SYMTAB.
• SYMTAB would be preloaded with the register names (A,X, etc.) and their values (0, 1, etc.). 125 RDREC CLEAR X B410
1 Machine-dependent assembler features
8
4
Register-to-memory instructions z
Most of register-to-memory instructions are assembled using either
• • z
program-counter relative addressing or
• •
TA=(PC)+displacement (12-bit) The displacement must be between –2048 and +2047.
base relative addressing.
• •
TA=(BASE)+displacement (12-bit) The displacement must be between 0 to 4095.
If the required displacement is too large, then the 4-byte extended instruction format must be used.
• 15
The programmer must specify the 4-byte format by adding the prefix + to the operation code in the source statement. CLOOP
125 RDREC
+JSUB
RDREC
CLEAR
X
Machine-dependent assembler features
4B101036
9
Register-to-memory instructions (cont’d) z
If extended format is not specified, the assembler may first attempt to translate the instruction using program-counter relative addressing. If the required displacement is out of range, the assembler then attempts to use base relative addressing. If neither form of relative addressing is applicable, then the instruction cannot be properly assembled and the assembler must generate an error message.
Machine-dependent assembler features
10
5
Register-to-memory instructions (cont’d) 15
0006
CLOOP
125
1036
RDREC
6
1
1
+JSUB RDREC ... CLEAR X
1
1
1
1
4B101036 B410
20
op
n
i
x
b
p
e
address
010010
1
1
0
0
0
1
0000,0001,0000,0011,01 10
4B101036
Machine-dependent assembler features
11
A SIC/XE program (Fig. 2.5) An assembler directive
a reloctable program
4-byte instruction
immediate operand
indirect addressing
Machine-dependent assembler features
12
6
A SIC/XE program (cont’d)
register-toregister instruction
Machine-dependent assembler features
13
A SIC/XE program (cont’d)
Machine-dependent assembler features
14
7
Program from Fig 2.5 with object code
Machine-dependent assembler features
15
Program from Fig 2.5 with object code (cont’d)
Machine-dependent assembler features
16
8
Program from Fig 2.5 with object code (cont’d)
17
Machine-dependent assembler features
Calculation for register-toregister instruction 125
1036
RDREC
CLEAR
X
8
4
4
op
r1
r2
10110100
0001
0000
B410
B410 150
1049
COMPR
A,S
8
4
4
op
r1
r2
10100000
0000
0100
A004
A004 Machine-dependent assembler features
18
9
Displacement calculation for programcounter relative addressing z z
TA = (PC)+disp => disp = TA – (PC) The program counter is advanced after each instruction is fetched and before it is executed.
• PC will contain the address of the next instruction. 10 0000 FIRST
STL
95 0030
RETADR
• • •
RETADR ... RESW 1
17202D
After fetching the instruction in line 10, PC becomes 0003. Hence, disp=0030(PC)=0030-0003=002D. p bit is set to 1 to indicate program-counter relative addressing. Bits n and i are both set to 1, indicating neither indirect nor immediate addressing.
6 op 000101
1 n 1
1 1 i x 1 0
1 b 0
1 p 1
1 e 0
12 disp 000000101101
17202D
19
Machine-dependent assembler features
Displacement calculation for programcounter relative addressing (cont’d) 16 0006CLOOP+JSUB 40 0017
RDREC ... CLOOP
J
4B101036 3F2FEC
disp = TA-(PC) = 0006-001A= -0014 = (111111101100)2 using 2’s complement for negative numbers 6 op 001111
1 n 1
1 1 i x 1 0
1 b 0
1 p 1
1 e 0
12 disp 111111101100
3F2FEC Machine-dependent assembler features
20
10
Displacement calculation for base relative addressing z z z
TA=(BASE)+disp=>disp = TA-(BASE) To compute displacements, the assembler must know the content of register B that must be specified by the programmer. The assembler directive “BASE” tells the assembler the content of the base register B.
• • •
13
•
BASE
12
•
LENGTH
This statement informs the assembler that the base register will contain the address of LENGTH.
LDB
#LENGTH
This statement stores the address of LENGTH to register B.
When register B is used for another purpose, the programmer can use the assembler directive “NOBASE” to inform the assembler that the content of the base register can no longer be relied upon for addressing.
Machine-dependent assembler features
21
Displacement calculation for base relative addressing (cont’d) 1050036 BUFFER 160104E
RESB STCH
4096 BUFFER,X
57C003
1. Try indexed and program-counter relative addressing disp = TA – (PC)=0036 – 1050 = -101A16<-2048—out of range 2. Try indexed and base relative addressing disp = TA – (BASE)=0036 – 0033 =000316 6 op 0101,0 1
1 n 1
1 1 i x 1 1
1 b 1
1 p 0
1 e 0
12 disp 0000,0000,0011
57C003 Machine-dependent assembler features
22
11
Calculation for immediate addressing z
Convert the immediate operand to its internal representation and insert it into the instruction.
55
0020 6 op 000000
LDA 1 n 0
1 1 i x 1 0
1 b 0
#3
1 p 0
1 e 0
010003
12 disp 000000000011
010003 133
103C
+LDT
6
1
1
1
1
#4096 75101000 1
1
20
op
n
i
x
b
p
e
address
011101
0
1
0
0
0
1
0000,0001,0000,0000,00 00
75101000
Machine-dependent assembler features
23
Calculation for immediate addressing (cont’d) the value of the symbol LENGTH is its address. 20
0003
100
0033
LDB #LENGTH ... RESW 1
LENGTH
69202D
Line 20 combines the program-counter relative addressing with immediate addressing. disp = TA – (PC)=0033-0006=002D 6 op 011010
1 n 0
1 1 i x 1 0
1 b 0
1 p 1
1 e 0
12 disp 0000,0010,1101
69202D Machine-dependent assembler features
24
12
Calculation for indirect addressing 70
002A
95
0030
J
@RETADR ... RESW 1
RETADR
3E2003
Line 70 combines the program-counter relative addressing with indirect addressing. disp = TA-(PC)=0030-002D=0003 6 1 1 1 1 1 op 001111
n 1
i x 0 0
b 0
p 1
1 e 0
12 disp 0000,0000,0011
3E2003 Machine-dependent assembler features
25
Program relocation z
The object program cannot be assigned to a fixed address.
• To take full advantage of hardware, we must be able to load programs into memory wherever there is room for it. z
The task of adjusting object programs so they may be placed in arbitrary memory locations is called relocation. Machine-dependent assembler features
26
13
Relocation problem No matter where the program is loaded, RDREC is always 1036 bytes past the starting address of the program. Solution must be modified •The assembler insert the address of RDREC relative to the start of the program. •The assembler also produces a command for the loader, instructing it to add the beginning address of the program to the address field in the JSUB instruction at load time. Machine-dependent assembler features
27
What parts of the program need to be modified? z
z
The parts of the object program that require modification at load time are those that specify direct addresses. No modifications are needed for the operands
• that are not memory references, or • specified by • the program-counter addressing, and • base relative addressing.
Relocatable is one advantage of relative addressing. Machine-dependent assembler features
28
14
Relocatable program z
The assembler can identify for the loader those parts of the object program that need modification.
• An object program that contains the information necessary to perform this kind of modification is called a relocatable program.
Machine-dependent assembler features
29
The modification record z
z
The assembler produces a modification record describing the address and length of an address field to be modified. The loader will add the beginning address of the loaded program to the address field specified by a modification record.
Machine-dependent assembler features
30
15
Modification record format Col. 1 Col. 2-7 Col. 8-9
M Starting location of the address field to be modified, relative to the beginning of the program Length of the address field to be modified, in halfbytes If the field contains an odd number of half-bytes, the starting location begins in the middle of the first byte.
15 0006 CLOOP +JSUB M00000705 35 0013 +JSUB M00001405 65 0026 +JSUB M00002705
RDREC
4B101036
WRREC
4B10105D
WRREC
4B10105D
Machine-dependent assembler features
31
Object program with modification record relocatable
same as the absolute program with starting address 0 modification records Machine-dependent assembler features
32
16