Download - Machine Independent Assember Features
-
7/30/2019 Machine Independent Assember Features
1/31
05/11/13 1
LITERALS
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 alabel for it
-
7/30/2019 Machine Independent Assember Features
2/31
05/11/13 2
Contd..
A literal is identified with the prefix = which isfollowed by a specification of the literal value
Example:
LDA =CEOF
Statement specifies a 3-byte operand whosevalue is the character string EOF
-
7/30/2019 Machine Independent Assember Features
3/31
05/11/13 3
Contd..
There is a difference between a literal and animmediate operand
With immediate addressing, the operand value is
assembled as part of the machine instruction
With a literal, the assembler generates the specifiedvalue as a constant at some other memory location
The address of the generated constant is used asthe target address for the machine instruction
-
7/30/2019 Machine Independent Assember Features
4/31
05/11/13 4
ExampleLOC SOURCE STATEMENT OBJECT CODE
:
:001A ENDFIL LDA =CEOF 032010
.
.
002A J @RETADR 3E2003
LTORG
002D * =CEOF 454F46:
:
106B WD =X05 DF2008
:
1073 RSUB 4F0000END FIRST
1076 * =X05 05
-
7/30/2019 Machine Independent Assember Features
5/31
05/11/13 5
Contd..
All of the literal operands used in a program aregathered 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 placeliterals into a pool at some other location in theobject program
To allow this, the assembler directive LTORG isused
-
7/30/2019 Machine Independent Assember Features
6/31
05/11/13 6
Contd..
When the assembler encounters a LTORGstatement, 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 ofthe program
-
7/30/2019 Machine Independent Assember Features
7/31
05/11/13 7
Contd..
The basic data structure needed is a LiteralTableLITTAB
For each Literal used, this table contains theLiteral Name, the operand value and lengthand the address assigned to the operandwhen 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/30/2019 Machine Independent Assember Features
8/31
05/11/13 8
Contd..
During Pass 1, as each literal operand isrecognized, the assembler searches LITTAB for thespecified literal name
If the literal is already present in the table, no actionis needed; if it is not present, the literal is added toLITTAB (leaving the address unassigned)
When pass 1 encounters a LTORG statement or theend of the program, the assembler makes a scan ofthe literal table
-
7/30/2019 Machine Independent Assember Features
9/31
05/11/13 9
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
-
7/30/2019 Machine Independent Assember Features
10/31
05/11/13 10
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
-
7/30/2019 Machine Independent Assember Features
11/31
05/11/13 11
Contd..
Consider the statement
+LDT #4096 to load value 4096 into T
If we include the statement
MAXLEN EQU 4096
We can write above line as
+LDT #MAXLEN
-
7/30/2019 Machine Independent Assember Features
12/31
05/11/13 12
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 theoperand in the instruction
-
7/30/2019 Machine Independent Assember Features
13/31
05/11/13 13
Contd..
ORG
ORG stands forOrigin
Its form is : ORG Value
where Value is a constant or an expressioninvolving constants and previously defined
symbols
-
7/30/2019 Machine Independent Assember Features
14/31
05/11/13 14
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 statementwill affect the values of all labels defined until
the next ORG
-
7/30/2019 Machine Independent Assember Features
15/31
05/11/13 15
Example..
SYMBOL VALUE FLAGSSTAB
(100 entries)
. . .
. . .
. . .
Let :
SYMBOL: 6bytes
VALUE: 1wordFLAGS: 2bytes
-
7/30/2019 Machine Independent Assember Features
16/31
05/11/13 16
Contd..
We could reserve space for this table with thestatement
STAB RESB 1100
We want to be able to refer to the fields SYMBOL,VALUE and FLAGS individually, so we must alsodefine these labels
We can make the structure of the table as clear as itmight be using ORG statement
-
7/30/2019 Machine Independent Assember Features
17/31
05/11/13 17
Contd..
STAB RESB 1100
ORG STAB
SYMBOL RESB 6
VALUE RESW 1
FLAGS RESB 2
ORG STAB+1100
-
7/30/2019 Machine Independent Assember Features
18/31
05/11/13 18
Expressions
Most assemblers allow the use of expressions
Each such expression must be evaluated by theassembler 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 valueof the location counter (often designated by * )
-
7/30/2019 Machine Independent Assember Features
19/31
05/11/13 19
Example..
:
0036 BUFFER RESB 4096
1036 BUFEND EQU *
1000 MAXLEN EQU BUFEND - BUFFER
:
:
-
7/30/2019 Machine Independent Assember Features
20/31
05/11/13 20
Contd..
Expressions are classified as either AbsoluteExpressions or Relative Expressions depending
upon the type of value they produce
An expression that contains only absolute terms is
anAbsolute Expression
Absolute expression may contain relative termsprovided the relative terms occur in pairs and the
terms in each such pair have opposite signs
-
7/30/2019 Machine Independent Assember Features
21/31
05/11/13 21
Contd..
In the statement
MAXLEN EQU BUFEND - BUFFER
Both BUFEND and BUFFER are relative terms,
each representing an address within the program
However, the expression represents an absolutevalue: the difference between the two addresses,
which is the length of the buffer area in bytes
-
7/30/2019 Machine Independent Assember Features
22/31
05/11/13 22
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
Line Loc / Block Source statement Object code
-
7/30/2019 Machine Independent Assember Features
23/31
05/11/13 23
Line Loc / Block Source statement Object code
5 0000 0 COPY START 0
10 0000 0 FIRST STL RETADR 172063
15 0003 0 CLOOP JSUB RDREC 4B2021
20 0006 0 LDA LENGTH 032060
25 0009 0 COMP #0 290000
30 000C 0 JEQ ENDFIL 332006
35 000F 0 JSUB WRREC 4B203B
40 0012 0 J CLOOP 3F2FEE
45 0015 0 ENDFIL LDA =CEOF 032055
50 0018 0 STA BUFFER 0F2056
55 001B 0 LDA #3 010003
60 001E 0 STA LENGTH 0F2048
65 0021 0 JSUB WRREC 4B2029
70 0024 0 J @RETADR 3E203F
80 0000 1 USE CDATA
95 0000 1 RETADR RESW 1
100 0003 1 LENGTH RESW 1
105 0000 2 USE CBLKS
106 0000 2 BUFFER RESB 4096
107 1000 2 BUFEND EQU *
108 1000 2 MAXLEN EQU BUFEND -BUFFER
-
7/30/2019 Machine Independent Assember Features
24/31
05/11/13 24
Line Loc / Block Source statement Object code
115 . SUBROUTINE TO READ RECORD INTO BUFFER
120 .
0027 0 USE
125 0027 0 RDREC CLEAR X B410
127 0029 0 CLEAR A B400
130 002B 0 CLEAR S B440
002D 0 +LDT #MAXLEN 75101000
135 0031 0 RLOOP TD INPUT E32038
140 0034 0 JEQ RLOOP 332FFA
145 0037 0 RD INPUT DB2032
150 003A 0 COMPR A, S A004
155 003C 0 JEQ EXIT 332008
160 003F 0 STCH BUFFER, X 57A02F
165 0042 0 TIXR T B850
170 0044 0 JLT RLOOP 3B2FEA
175 0047 0 EXIT STX LENGTH 13201F
180 004A 0 RSUB 4F0000
181 0006 1 USE CDATA
185 0006 1 INPUT BYTE XF1 F1
-
7/30/2019 Machine Independent Assember Features
25/31
05/11/13 25
Line Loc / Block Source statement Object code200 . SUBROUTINE TO WRITE RECORD FROM BUFFER
.
004D 0 USE
205 004D 0 WRREC CLEAR X B410
210 004F 0 LDT LENGTH 772017
215 0052 0 WLOOP TD =X05 E3201B
220 0055 0 JEQ WLOOP 332FFA
225 0058 0 LDCH BUFFER,X 53A016230 005B 0 WD =X05 DF2012
235 005E 0 TIXR T B850
240 0060 0 JLT WLOOP 3B2FEF
245 0063 0 RSUB 4F0000
0007 1 USE CDATA
LTORG
250 0007 1 * =CEOF 454F46
000A 1 * =X05 05
255 END FIRST
-
7/30/2019 Machine Independent Assember Features
26/31
05/11/13 26
Contd..
The assembler directive USE indicates which portions ofthe 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
-
7/30/2019 Machine Independent Assember Features
27/31
05/11/13 27
Contd..
During Pass 1, a separate location counter for each programblock is assigned
The location counter for a block is initialized to 0 when the blockis first begun
The current value of this location counter is saved whenswitching to another block, and the saved value is restored whenresuming a previous block
Each label in the program is assigned an address that is relativeto the start of the block that contains it
-
7/30/2019 Machine Independent Assember Features
28/31
05/11/13 28
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
-
7/30/2019 Machine Independent Assember Features
29/31
05/11/13 29
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 theassigned block starting address
-
7/30/2019 Machine Independent Assember Features
30/31
05/11/13 30
Contd..
At the end of Pass 1 the assembler constructs a tablethat contains the starting addresses and lengths for allblocks
For our sample program, this table looks like:
Block name Block number Address Length
(default) 0 0000 0066
CDATA 1 0066 000B
CBLKS 2 0071 1000
-
7/30/2019 Machine Independent Assember Features
31/31
05/11/13 31
Default (1)
CDATA (1)
CBLCKS (1)
Default (2)
CDATA (2)
Default (3)
CDATA (3)
Default (1)
Default (2)
CDATA (2)
Default (3)
CDATA (3)
Default (1)
Default (2)
Default (3)
CDATA (1)
CDATA (2)
CDATA (3)
CBLCKS (1)
Source Program Object Program Program Loaded in Memory
0000
0027
004D
0066
006C
006D
0071
1070