Chapter 2 Assemblers
-- Machine-Dependent Assembler Features
Tzong-Jye Liu
Department of Information Engineering and Computer Science
Feng Chia University
[email protected]
Outline
n Instruction format and addressing mode
n Program relocation
System Programming 2
Instruction format and addressing mode
n PC-relative or Base-relative addressing
n op m
n Indirect addressing
n op @m
n Immediate addressing
n op #c
n Extended format
n +op m
n Index addressing
n op m,x
n Register-to-register instructions
System Programming 3
Example SIC/XE program
(Figure 2.6, pp.58)
Example SIC/XE program
(Figure 2.6, pp.58)
System Programming 5
Example SIC/XE program
(Figure 2.6, pp.58)
System Programming 6
START statement
5 COPY START 0
n COPY: program name
n START directive specifies a beginning
program address
n 0: a relocatable program
n Tread as if the program is loaded starting at address 0
System Programming 7
Register-register instruction
n Convert the mnemonic name to their number
equivalents
n Register name (A, X, L, B, S, T, F, PC, SW) and
their values (0, 1, 2, 3, 4, 5, 6, 8, 9)
n May implement in a separate table or preload the
register names and values to SYMTAB
Format 2 (2 byte) op (8) r1 (4) r2 (4)
X
Mnemonic Format Opcode
CLEAR r1 2 B4 125 CLEAR X B410
COMPR r1,r2 2 A0 150 COMPR A, S A004
A S
System Programming 8
Address translation
n Most register-memory instructions use program
counter relative or base relative addressing
n Format 3: 12-bit address field
n base-relative: 0~4095
n pc-relative: -2048~2047
n Format 4: 20-bit address field
n Addressing mode (Refer to Chapter 1)
Mode Indication Operand value
Immediate addressing n=0, i=1, TA
Indirect addressing n=1, i=0 ((TA))
Simple addressing n=0, i=0 Standard SIC
n=1, i=1 (TA)
Format 3 (3 byte) op (6) n i x b p e disp (12)
Format 4 (4 byte) op (6) n i x b p e address (20) 9
Program counter relative
n Calculate displacement
n Displacement must be small enough to fit in a 12-bit
field (-2048..2047)10
n In SIC, PC is advanced after each instruction is fetched
and before it is executed; i.e., PC contains the address
of the next instruction.
10 0000 FIRST STL RETADR
RETADR is at address (0030)16
After the SIC fetches this instruction, (PC) = (0003)16
TA = (PC) + disp Þ disp = TA – (PC) = 0030 – 0003 = (02D)16
op n i x b p e disp
000101 1 1 0 0 1 0 02D Þ 17202D
System Programming 10
Program counter relative
40 0017 J CLOOP 10000
- 14
CLOOP is at address (0006)16 FFEC
After the SIC fetches this instruction, (PC) = (001A)16
TA = (PC) + disp Þ disp = TA – (PC) = 0006 – 001A = (FFEC)16
op n i x b p e disp 12-bits
001111 1 1 0 0 1 0 FEC Þ 3F2FEC
70 002A J @RETADR
Indirect addressing
RETADR is at address (0030)16
After the SIC fetches this instruction, (PC) = (002D)16
TA = (PC) + disp Þ disp = TA – (PC) = 0030 – 002D = (0003)16
op n i x b p e disp
001111 1 0 0 0 1 0 003 Þ 3E2003
System Programming 11
Base relative
n 12 bits displacement (0 ~ 4095)
n Base register is under the control of the programmer.
n The programmer must tell the assembler what the base register will
contain during execution of program.
n Assembler directive
n BASE: tell the assembler what the base register will contain
n NOBASE: tell the assembler that the contents of the base register
can no longer be used for addressing.
n When based register can be relied upon, the assembler can use base
relative, otherwise only the pc-relative can be used
n The assembler first choose pc-relative, if displacement not enough,
choose base relative
0003 LDB #LENGTH (instruction)
BASE LENGTH (directive)
:
0033 LENGTH RESW 1
NOBASE
System Programming 12
Base relative
12 0003 LDB #LENGTH 69202D
13 BASE LENGTH
: :
100 0033 LENGTH RESW 1
105 0036 BUFFER RESB 4096
: :
160 104E STCH BUFFER,X 57C003
165 1051 TIXR T B850
• PC relative is no longer applicable
• (0036)16 – (1051)16 = (-101B)16 < (-0800)16 = (-2048)10
• LDB loads the address of LENGTH into base register during execution
• BASE directive explicitly informs the assembler that the base register
will contain the address of LENGTH
BUFFER is at address (0036)16
(B) = (0033)16
disp = 0036 – 0033 = (0003)16
op n i x b p e disp
010101 1 1 1 1 0 0 003 Þ 57C003
System Programming 13
Base relative
20 000A LDA LENGTH 032026
: :
100 0033 LENGTH RESW 1
: :
175 1056 EXIT STX LENGTH 134000
n Line 20, using pc-relative
n Consider Line 175
n If we use pc-relative
n LENGTH at address 0033
n Disp = TA – (PC) = 0033 –1059 = EFDA
n PC relative is no longer applicable, try to use
BASE relative addressing
System Programming 14
Choice of Addressing Modes
n Programmer must specify the extended
format (4-byte) by using the prefix +
n If not, assembler first attempts PC-relative
n If the required displacement is out of range,
use base relative addressing
n Otherwise, generate an error message
System Programming 15
Immediate addressing
n No memory reference is involved
n If immediate mode is specified, the target
address becomes the operand
55 0020 LDA #3
Immediate operand
TA = (0003)16
op n i x b p e disp
000000 0 1 0 0 0 0 003 Þ 010003
133 103C +LDT #4096
Extended instruction format
TA = (01000)16
op n i x b p e disp(20 bits)
011101 0 1 0 0 0 1 01000 Þ 75101000
System Programming 16
Immediate & PC-relative addressing
12 0003 LDB #LENGTH
LENGTH is at address 0033
TA = (PC) + disp Þ disp = 0033 – 0006 = (002D)16
op n i x b p e disp
011010 0 1 0 0 1 0 02D Þ 69202D
System Programming 17
Indirect & PC-relative addressing
70 002A J @RETADR
: :
95 0030 RETADR RESW 1
RETADR is at address 0030
TA = (PC) + disp Þ disp = 0030 – 002D = (0003)16
op n i x b p e disp
001111 1 0 0 0 1 0 003 Þ 3E2003
System Programming 18
Program relocation
n Why
n It is desirable to load and run several programs at the
same time
n The system must be able to load programs into memory
wherever there is room
n The exact starting address of the program is not known
until load time
System Programming 19
Example of program relocation
(Figure 2.7, pp.63)
Program relocation
n Absolute Program
n Program with starting address specified at assembly
time
n The address may be invalid if the program is loaded
into some where else.
n Example: (Figure 2.2, pp.47)
55 101B LDA THREE 00102D
Calculate based on the starting address 1000
Reload the program starting at 3000
55 301B LDA THREE 00302D
The absolute address should be modified
System Programming 21
Program relocation
n The only parts of the program that require
modification at load time are those that specify direct
addresses
n The rest of the instructions need not be modified
n Not a memory address (immediate addressing)
n PC-relative, Base-relative
n From the object program, it is not possible to
distinguish the address and constant
n The assembler must keep some information to tell the loader
n The object program that contains the modification record is called
a relocatable program
System Programming 22
The way to solve
the relocation problem
n For an address label, its address is assigned
relative to the start of the program (START 0)
n Produce a modification record to store the
starting location and the length of the address
field to be modified.
n The command for the loader must also be a
part of the object program
System Programming 23
Modification record
Modification record
Col. 1 M
Col. 2-7 Starting location of the address field to be modified,
relative to the beginning of the program (Hex)
Col. 8-9 Length of the address field to be modified, in half-bytes (Hex)
n One modification record for each address to be modified
n The length is stored in half-bytes (4 bits)
n The starting location is the location of the byte
containing the leftmost bits of the address field to be
modified.
n If the field contains an odd number of half-bytes, the
starting location begins in the middle of the first byte.
System Programming 24
Relocatable Object Program
(Figure 2.8, pp.65)
5 half-bytes
System Programming 25