¾ PC-relative or Base-relative addressing: op m
¾ Indirect addressing: op @m
¾ Immediate addressing: op #c
¾ Extended format: +op m
¾ Extended format: +op m
¾ Index addressing: op m,x
¾ register-to-register instructions
¾ larger memory -> multi-programming (program allocation)
Translation
Register translation
¾ Register name (A, X, L, B, S, T, F, PC, SW) and their values (0,1, 2, 3, 4, 5, 6, 8, 9)
¾ Preloaded in SYMTAB
Address translation
Address translation
¾ Most register-memory instructions use program counter relative or base relative addressing
¾ Format 3: 12-bit address field
Base-relative: 0~4095
PC-relative: -2048~2047
¾ Format 4: 20-bit address field
PC-Relative Addressing Mode
PC-relative
¾ 10 0000 FIRST STL RETADR 17202D
OPCODE n i x b p e Address
0001 01 1 1 0 0 1 0 (02D)
Displacement= RETADR - PC = 30-3 = 2D
¾ 40 0017 J CLOOP 3F2FEC
Displacement= CLOOP-PC= 6 - 1A= -14= FEC 0001 01 1 1 0 0 1 0 (02D)16
OPCODE n i x b p e Address
0011 11 1 1 0 0 1 0 (FEC)16
Base-Relative Addressing Modes
Base-relative
¾ Base register is under the control of the programmer
¾ 12 LDB #LENGTH
¾ 13 BASE LENGTH
¾ 160 104E STCH BUFFER, X 57C003
Displacement= BUFFER - B = 0036 - 0033 = 3
¾ NOBASE is used to inform the assembler that the contents of the base register no longer be relied upon for addressing
OPCODE n i x b p e Address
0101 01 1 1 1 1 0 0 (003)16
Immediate Address Translation
Immediate addressing
¾ 55 0020 LDA #3 010003
OPCODE n i x b p e Address
0000 00 0 1 0 0 0 0 (003)
¾ 133 103C +LDT #4096 75101000
0000 00 0 1 0 0 0 0 (003)16
OPCODE n i x b p e Address
0111 01 0 1 0 0 0 1 (01000)16
Immediate Address Translation
Immediate addressing
¾ 12 0003 LDB #LENGTH 69202D
¾ 12 0003 LDB #LENGTH 690033
OPCODE n i x b p e Address
0110 10 0 1 0 0 1 0 (02D)16
¾ 12 0003 LDB #LENGTH 690033
The immediate operand is the symbol LENGTH
The address of this symbol LENGTH is loaded into register B
LENGTH=0033=PC+displacement=0006+02D
If immediate mode is specified, the target address becomes the operand
OPCODE n i x b p e Address
0110 10 0 1 0 0 0 0 (033)16
Indirect Address Translation
Indirect addressing
¾ Target addressing is computed as usual (PC-relative or BASE-relative)
¾ Only the n bit is set to 1
¾ 70 002A J @RETADR 3E2003
TA=RETADR=0030
TA=(PC)+disp=002D+0003
OPCODE n i x b p e Address
0011 11 1 0 0 0 1 0 (003)16
Functions of a Basic Assembler
Object File
Address Translation
Program Relocation
Design & Implementation of Assembler
Program Block
Control Section & Program Linking
Other Issues
¾ One-pass Assembler
¾ Multi-pass Assembler
Program Relocation
Examples of Program Relocation
Absolute program, starting address 1000
5 2000 1000 COPY START 1000
10 2000 1000 FIRST STL RETADR 141033 142033
15 2003 1003 CLOOP JSUB RDREC 482039 483039
20 2006 1006 LDA LENGTH 001036 002036
25 2009 1009 COMP ZERO 281030 282030
30 200C 100C JEQ ENDFIL 301015 302015
35 200F 100F JSUB WREC 482061 483061
40 2012 1012 J CLOOP 3C1003 3C2003
45 2015 1015 ENDFIL LDA EOF 00102A 00202A
50 2018 1018 STA BUFFER 0C1039 0C2039
55 201B 101B LDA THREE 00102D 00202D
60 201E 101E STA LENGTH 0C1036 0C2036
65 2021 1021 JSUB WREC 482061 483061
70 2024 1024 LDL RETADR 081033 082033
75 2027 1027 RSUB 4C0000 4C0000
80 202A 102A EOF BYTE C'EOF' 454E46 454E46
85 202D 102D THREE WORD 3 000003 000003
90 2030 1030 ZERO WORD 0 000000 000000
95 2033 1033 RETADR RESW 1
100 2036 1036 LENGTH RESW 1
105 2039 1039 BUFFER RESB 4096
Problems of Program Relocation
Except for absolute address, the rest of the instructions need not be modified
¾ not a memory address (immediate addressing)
¾ PC-relative, Base-relative
Th l t f th th t i difi ti
The only parts of the program that require modification
at load time are those that specify direct addresses
Examples of Program Relocation
5 1000 0000 COPY START 0
10 1000 0000 FIRST STL RETADR 17202D 17202D
12 1003 0003 LDB #LENGTH 69202D 69202D
13 BASE LENGTH
15 1006 0006 CLOOP +JSUB RDREC 4B101036 4B102036
20 100A 000A LDA LENGTH 032026 032026
25 100D 000D COMP #0 290000 290000
== Æ 1000
30 1010 0010 JEQ ENDFIL 332007 332007
35 1013 0013 +JSUB WRREC 4B10105D 4B10205D
40 1017 0017 J CLOOP 3F2FEC 3F2FEC
45 101A 001A ENDFIL LDA EOF 032010 032010
50 101D 001D STA BUFFER 0F2016 0F2016
55 1020 0020 LDA #3 010003 010003
60 1023 0023 STA LENGTH 0F200D 0F200D
65 1026 0026 +JSUB WRREC 4B10105D 4B10205D
70 102A 002A J @RETADR 3E2003 3E2003
80 102D 002D EOF BYTE C'EOF' 454F46 454F46
95 1030 0030 RETADR RESW 1
100 1036 0036 BUFFER RESB 4096
How to Make Program Relocation Easier
Use program-counter (PC) relative addresses
¾ Did you notice that we didn’t modify the addresses for JEQ, JLT and J instructions?
¾ We didn’t modify the addresses for RETADR, LENGTH, and BUFFER
BUFFER.
Virtual memory
¾ Every program pretends that it has all of memory. Therefore, Text segment always starts at a fixed address; Stack segment always resides a some huge high address.
Relocatable Program
Modification record
¾ Col 1 M
¾ Col 2-7 Starting location of the address field to be
modified, relative to the beginning of the program
Col 8 9 l th f th dd fi ld t b difi d i h lf
¾ Col 8-9 length of the address field to be modified, in half-bytes
Object Code with Modification Record
Functions of a Basic Assembler
Object File
Address Translation
Program Relocation
Design & Implementation of Assembler
Program Block
Control Section & Program Linking
Other Issues
¾ One-pass Assembler
¾ Multi-pass Assembler
Program Blocks
Refer to segments of code that are rearranged within a single object program unit
USE [blockname]
At the beginning, statements are assumed to be part f th d (d f lt) bl k
of the unnamed (default) block
If no USE statements are included, the entire program belongs to this single block
Each program block may actually contain several
separate segments of the source program
Program Blocks - Implementation
Pass 1
¾ Each program block has a separate location counter
¾ Each label is assigned an address that is relative to the start of the block that contains it
¾ At the end of Pass 1, the latest value of the location ,
counter for each block indicates the length of that block
¾ The assembler can then assign to each block a starting address in the object program
Pass 2
¾ The address of each symbol can be computed by adding the assigned block starting address and the relative
address of the symbol to that block
Program Blocks - Implementation
Each source line is given a relative address assigned and a block number
For absolute symbol, there is no block number
¾ line 107
Example
¾ 20 0006 0 LDA LENGTH 032060
¾ LENGTH = (Block 1) + 0003 = 0066 + 0003 = 0069
¾ LOCCTR = (Block 0) + 0009 = 0009
Object Code
Loading Program Blocks
Functions of a Basic Assembler
Object File
Address Translation
Program Relocation
Design & Implementation of Assembler
Program Block
Control Section & Program Linking
Other Issues
¾ One-pass Assembler
¾ Multi-pass Assembler
Control Section
Are most often used for subroutines or other logical subdivisions of a program
The programmer can assemble, load, and manipulate each of these control sections separately
I t ti i t l ti d t f t
Instruction in one control section may need to refer to instructions or data located in another section
Because of this, there should be some means for linking
control sections together
External Definition and References
External definition
¾ EXTDEF name [, name]
¾ EXTDEF names symbols that are defined in this control section and may be used by other sections
External reference
¾ EXTREF name [,name]
¾ EXTREF name [,name]
¾ EXTREF names symbols that are used in this control section and are defined elsewhere
Example
¾ 15 0003 CLOOP +JSUB RDREC 4B100000
160 0017 +STCH BUFFER,X 57900000
190 0028 MAXLEN WORD BUFEND-BUFFER 000000
Implementation
The assembler must include information in the object program that will cause the loader to insert proper values where they are required
Define record
¾ Col. 1 D
¾ Col. 2-7 Name of external symbol defined in this control section
¾ Col. 8-13 Relative address within this control section (hexadeccimal)
¾ Col.14-73 Repeat information in Col. 2-13 for other external symbols
Refer record
¾ Col. 1 D
¾ Col. 2-7 Name of external symbol referred to in this control section
¾ Col. 8-73 Name of other external reference symbols
Modification Record
Modification record
¾ Col. 1 M
¾ Col. 2-7 Starting address of the field to be modified (hexiadecimal)
¾ Col. 8-9 Length of the field to be modified, in half-bytes (hexadeccimal)
¾ Col.11-16 External symbol whose value is to be added to or subtracted from the indicated field
N l i i i ll l b l i i i
¾ Note: control section name is automatically an external symbol, i.e. it is available for use in Modification records.
Object Code
Functions of a Basic Assembler
Object File
Address Translation
Program Relocation
Design & Implementation of Assembler
Program Block
Control Section & Program Linking
Other Issues
¾ One-pass Assembler
¾ Multi-pass Assembler
One-Pass Assemblers
Main problem
¾ Forward references
Data items & labels on instructions
Solution
¾ Require all such areas be defined before they are referencedequ e suc e s be de ed be o e ey e e e e ced
¾ Labels on instructions: no good solution
Two types of one-pass assemblers
¾ Load-and-go
Produces object code directly in memory for immediate execution
¾ The other
Produces usual kind of object code for later execution
Load-and-go Assembler
Characteristics
¾ Useful for program development and testing
¾ Avoids the overhead of writing the object program out and reading it back
¾ Both one-pass and two-pass assemblers can be designed as p p g load-and-go.
¾ However one-pass also avoids the over head of an additional pass over the source program
¾ For a load-and-go assembler, the actual address must be known at assembly time, we can use an absolute program
Load-and-go Assembler
Forward references handling
1. Omit the address translation
2. Insert the symbol into SYMTAB, and mark this symbol undefined
3. The address that refers to the undefined symbol is added to a 3. The address that refers to the undefined symbol is added to a
list of forward references associated with the symbol table entry
4. When the definition for a symbol is encountered, the proper address for the symbol is then inserted into any instructions previous generated according to the forward reference list
Load-and-go Assembler
At the end of the program
¾ Any SYMTAB entries that are still marked with * indicate undefined symbols
¾ Search SYMTAB for the symbol named in the END statement and jump to this location to begin executionj p g
The actual starting address must be specified at
assembly time
After Scanning Line 40
After Scanning Line 160
Object Code
Multi-Pass Assemblers
Restriction on EQU
¾ No forward reference, since symbols’ value can’t be defined during the first pass
Example
Use link list to keep track of hose al e depend on an
¾ Use link list to keep track of whose value depend on an undefined symbol