Professional Documents
Culture Documents
It is often convenient for the programmer to be able to write the value of a constant operand as a part of the instruction that uses it This avoids having to define the constant elsewhere in the program and makeup a label for it
1
05/11/13
Contd..
A literal is identified with the prefix = which is followed by a specification of the literal value
Example: LDA
=CEOF
Statement specifies a 3-byte operand whose value is the character string EOF
2
05/11/13
Contd..
There is a difference between a literal and an immediate operand With immediate addressing, the operand value is assembled as part of the machine instruction With a literal, the assembler generates the specified value as a constant at some other memory location The address of the generated constant is used as the target address for the machine instruction
3
05/11/13
Example
LOC 001A SOURCE STATEMENT : : ENDFIL LDA =CEOF . . J @RETADR LTORG OBJECT CODE 032010
002A 002D
3E2003 454F46
*
WD
106B 1073
END 1076
DF2008 4F0000 05
05/11/13
Contd..
All of the literal operands used in a program are gathered together into one or more Literal Pools Normally literals are placed into a pool at the end of the program In some cases, however, it is desirable to place literals into a pool at some other location in the object program To allow this, the assembler directive LTORG is used
5
05/11/13
Contd..
When the assembler encounters a LTORG statement, it creates a literal pool that contains all of the literal operands used since the previous LTORG Of course, literals placed in a pool by LTORG will not be repeated in the pool at the end of the program
05/11/13
Contd..
The basic data structure needed is a Literal Table LITTAB For each Literal used, this table contains the Literal Name, the operand value and length and the address assigned to the operand when it is placed in a literal pool LITTAB is often organized as a hash table, using the literal name or value as the key
7
05/11/13
Contd..
During Pass 1, as each literal operand is recognized, the assembler searches LITTAB for the specified literal name If the literal is already present in the table, no action is needed; if it is not present, the literal is added to LITTAB (leaving the address unassigned) When pass 1 encounters a LTORG statement or the end of the program, the assembler makes a scan of the literal table
8
05/11/13
Contd..
At this time each literal currently in the table is assigned an address During Pass 2, the operand address for use in generating object code is obtained by searching LITTAB for each literal operand encountered
05/11/13
Symbol-Defining Statements
EQU (EQUATE)
This assembler directive allows the programmer to define symbols and specify their values Symbol EQU Value
This statement defines the symbol and assigns to it the value specified
10
05/11/13
Contd..
05/11/13
Contd..
When the assembler encounters the EQU statement, it enters MAXLEN into SYMTAB with value 4096 During the assembly of the LDT instruction, the assembler searches SYMTAB for the symbol MAXLEN, using its value as the operand in the instruction
12
05/11/13
Contd..
ORG
ORG stands for Origin Its form is : ORG Value where Value is a constant or an expression involving constants and previously defined symbols
13
05/11/13
Contd..
When ORG statement is encountered during assembly of a program, the assembler resets its location counter (LOCCTR) to the specified value Since the values of symbols used as labels are taken from LOCCTR, the ORG statement will affect the values of all labels defined until the next ORG
14
05/11/13
Example..
SYMBOL STAB (100 entries) . . .
Let : SYMBOL: 6bytes VALUE: 1word FLAGS: 2bytes
VALUE FLAGS
. . .
. . .
05/11/13
15
Contd..
We want to be able to refer to the fields SYMBOL, VALUE and FLAGS individually, so we must also define these labels We can make the structure of the table as clear as it might be using ORG statement
16
05/11/13
Contd..
STAB SYMBOL VALUE FLAGS RESB ORG RESB RESW RESB ORG 1100 STAB 6 1 2 STAB+1100
05/11/13
17
Expressions
Most assemblers allow the use of expressions Each such expression must be evaluated by the assembler to produce a single operand address or value Individual terms in the expression may be constants, user defined symbols or special terms The most common special term is the current value of the location counter (often designated by * )
18
05/11/13
Example..
:
0036 1036 1000 BUFFER RESB BUFEND EQU MAXLEN EQU : : 4096 * BUFEND - BUFFER
05/11/13
19
Contd..
Expressions are classified as either Absolute Expressions or Relative Expressions depending upon the type of value they produce An expression that contains only absolute terms is an Absolute Expression Absolute expression may contain relative terms provided the relative terms occur in pairs and the terms in each such pair have opposite signs
20
05/11/13
Contd..
Both BUFEND and BUFFER are relative terms, each representing an address within the program However, the expression represents an absolute value: the difference between the two addresses, which is the length of the buffer area in bytes
21
05/11/13
Program Blocks
So far all the programs we come across were handled by the assembler as one entity resulting in a single block of object code Within this object program the generated machine instructions and data appeared in the same order as they were written in the source program Program Blocks refer to segments of code that are rearranged within a single object program unit
22
05/11/13
Loc / Block 0000 0000 0003 0006 0009 000C 000F 0012 0015 0018 001B 001E 0021 0024 0000 0000 0003 0000 0000 1000 1000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 2 2 2 2
Source statement COPY FIRST CLOOP START STL JSUB LDA COMP JEQ JSUB J ENDFIL LDA STA LDA STA JSUB J USE RETADR LENGTH BUFFER BUFEND MAXLEN RESW RESW USE RESB EQU EQU 0 RETADR RDREC LENGTH #0 ENDFIL WRREC CLOOP =CEOF BUFFER #3 LENGTH WRREC @RETADR CDATA 1 1 CBLKS 4096 * BUFEND BUFFER
Object code 172063 4B2021 032060 290000 332006 4B203B 3F2FEE 032055 0F2056 010003 0F2048 4B2029 3E203F
23
Source statement
Object code
SUBROUTINE TO READ RECORD INTO BUFFER USE CLEAR CLEAR CLEAR +LDT TD JEQ RD COMPR JEQ STCH TIXR JLT STX RSUB USE BYTE CDATA XF1 F1 X A S #MAXLEN INPUT RLOOP INPUT A, S EXIT BUFFER, X T RLOOP LENGTH B410 B400 B440 75101000 E32038 332FFA DB2032 A004 332008 57A02F B850 3B2FEA 13201F 4F0000
125 127 130 135 140 145 150 155 160 165 170 175 180 181 185
0027 0029 002B 002D 0031 0034 0037 003A 003C 003F 0042 0044 0047 004A 0006 0006
05/11/13
24
Line 200
Loc / Block
Source statement
Object code
. SUBROUTINE TO WRITE RECORD FROM BUFFER . 004D 0 0 0 0 0 0 0 0 0 0 1 WLOOP WRREC USE CLEAR LDT TD JEQ LDCH WD TIXR JLT RSUB USE LTORG CDATA X LENGTH =X05 WLOOP BUFFER,X =X05 T WLOOP B410 772017 E3201B 332FFA 53A016 DF2012 B850 3B2FEF 4F0000
004D 004F 0052 0055 0058 005B 005E 0060 0063 0007
250 255
0007 000A
1 1
* *
454F46 05
05/11/13
25
Contd..
The assembler directive USE indicates which portions of the source program belong to the various blocks At the beginning, statements are assumed to be part 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
05/11/13
26
Contd..
During Pass 1, a separate location counter for each program block is assigned The location counter for a block is initialized to 0 when the block is first begun The current value of this location counter is saved when switching to another block, and the saved value is restored when resuming a previous block Each label in the program is assigned an address that is relative to the start of the block that contains it
05/11/13
27
Contd..
When labels are entered into the symbol table, the Block Name or Number is stored along with the assigned relative address At the end of the 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
28
05/11/13
Contd..
For code generation during Pass 2, the assembler needs the address for each symbol relative to the start of the object program This is easily found from the information in SYMTAB The assembler simply adds the location of the symbol, relative to the start of its block, to the assigned block starting address
05/11/13
29
Contd..
At the end of Pass 1 the assembler constructs a table that contains the starting addresses and lengths for all blocks For our sample program, this table looks like:
Source Program
Object Program
Default (1)
0000 0027
Default (1)
CDATA (1) CBLCKS (1) Default (2) CDATA (2) Default (3)
0066
0071
CBLCKS (1) CDATA (3)
1070
05/11/13 31