Professional Documents
Culture Documents
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
UNIT II
ASSEMBLERS
/
k
t
.
e
b
u
t
e
Assemblers1:
(i)Translation mnemonic operation codes to their machine language equivalents and
assigning machine address to symbolic labels used by the programmer.
(ii)There are some features of an assembler language that have no direct relation to machine
architecture.
s
c
/
/
:
p
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
Example:
SIC assambler language program.
The program contains a main routine that reads records from an input device and copies
them to an output device.
This main routine calls subroutine RDREC to read a record into a buffer and subroutine
WRREC to write the record from the buffer to the output device.
Each subroutine must transfer the record one character at a time.
Because the only instructions available are RD and WD.
The buffer is necessary because the I/O rates for the two devices,such as a disk and a slow
printing terminal may be very different.
The end of each record is marked with a null character.If a record is longer than the length
of the buffer (4096 bytes),only the first 4096 bytes are copied.
The program does not deal with error recovery.
The end of the file to be copied is indicated by zero-length record.
When the end of file is detected,the program writes EOF on the output device and terminates
by executing RSUB instruction.
This program was called by the operating system using a JSUB instruction,Thus the RSUB
will return control to the operating system.
/
k
t
.
e
b
u
t
e
s
c
/
PROGRAM
:/
SOURCE STATEMENT
LINE LOCCTR LABEL
p
t
EXPLANATION
OPCODE OPERAND
START
1000
10
ht
FIRST
STL
RETADR
15
CLOOP
JSUB
RDREC
20
LDA
LENGTH
25
COMP
ZERO
30
JEQ
ENDFIL
35
JSUB
WRREC
40
CLOOP
LOOP
LDA
EOF
50
STA
BUFFER
55
LDA
THREE
60
STA
LENGTH
COPY
45
ENDFIL
SET LENGTH=3
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
65
JSUB
WRREC
WRITE EOF
70
LDL
RETADR
75
RSUB
RETURN TO CALLER
80
EOF
BYTE
C'EOF'
85
THREE
WORD
90
ZERO
WORD
100
RETADR RESW
105
LENGTH RESW
110
BUFFER RESB
4096
/
k
t
125
RDREC
130
135
RLOOP
LDX
ZERO
LDA
ZERO
TD
INPUT
140
JEQ
145
RD
u
t
e
s
c
/
RLOOP
.
e
b
CLEAR A TO ZERO
TEST I/P DEVICE
LOOP UNTIL READY
INPUT
READ
CHARACTER
REGISTER A
COMP
ZERO
JEQ
EXIT
STCH
BUFFER,X
165
TIX
MAXLEN
170
JLT
RLOOP
STX
LENGTH
:/
p
t
150
155
160
175
ht
EXIT
180
RSUB
185
INPUT
BYTE
190
MAXLEN WORD
INTO
X'F1'
4096
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
PROGRAM
SOURCE STATEMENT
LINE LOCCTR LABEL
EXPLANATION
OPCODE OPERAND
200
WRREC
LDX
ZERO
210
WLOOP
TD
OUTPUT
215
JEQ
WLOOP
220
LDCH
BUFFER,X
225
WD
OUTPUT
WRITE CHARACTER
230
TIX
LENGTH
235
RSUB
240
OUTPUT
245
RETURN TO CALLER
BYTE
X'05'
END
FIRST
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
The header record constains the program name,starting address and length.
Header record:
col:1
H
col:2-7
program name
col:8-13
Starting address of object program
col:14-19
Length of object program in bytes
The text records contain the translated instructions and data of the program,together with an
indication of the addresses where these are to be loaded.
Text Record:
col:1
col:2-7
col:8-9
col:10-69
/
k
t
.
e
b
T
Starting address for object code in thid record
Length of object code in this record in bytes
Object code,represented in hexadecimal
u
t
e
s
c
/
/
:
p
The end record marks the end of the object program and specifies the address in the program
where execution is to begin.
End record:
col:1
E
col:2-7
Address of first executable instruction in object program.
t
t
h
The scope of the assembler is, to generate object code. But assembler does not know the
address exactly.so that the assembler choose pass1 algorithm and pass 2 algorithm4.
Pass:1
1.Assign addresses to are statements in the program.
2.Save the values assigned to are labels for use in pass 2.
3.Perform some processing of assembler directives.
Pass:2
1.Assemble instructions.
2.Generate data values.
3.Perform processing of assembler directives not done during pass 1.
4.Write the object program and the assembly listing.
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
/
k
t
u
t
e
2 MARKS
4.
5.
6.
7.
8.
.
e
b
Why you go for pass 1 & pass 2 algorithm?. State the reason.
What are the data structures ised in assembler?
Define optab.
Define symtab.
Define LOCCTR.
s
c
/
/
:
p
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
/
k
t
.
e
b
The START statement specifies the starting address of the location where the program is to
be loaded.
Eg:START 0 statement will allow a program to be loaded in the address 0.
SYMTAB would be preloaded with the register names (A,X...etc) and their values(0,1...ets)
Register to memory instruction is assembled using either program counter relative or base
relative addressing.
The assembler must calculate the displacement, which must be added as a part of the object
instruction.
The displacement is calculated so that the correct target address is ngot when content of
program counter(pc) or base register(B) is added with the displacement.
Displacement must be between 0 and 4095(for base relative mode) or between -2048 and
-2047(for program counter relative mode).
If neither program counter rerlative nor base relative addressing can be used then the 4-byte
extended instruction format is used.
Examples for code generation:
u
t
e
s
c
/
/
:
p
t
t
h
10
12
95
0000 FIRST
STL RETADR
17
machine
equivalent
+
first two bits
of register
0003
.
0030 RETADR
LDB #LENGTH
2
0 2 D
last displacement value
four
bits
of
reg
69 2 0 2 D
RESW
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
PRODEDURE:
THERE ARE THREE OPRATIONS TO FIND OBJECT CODE.
OPERATION 1: FIND MACHINE LANGUAGE EQUIVALENT AND SUM WITH
FIRST TWO BITS OF REGISTER.
STEP 1: FIND MACHINE LANGUAGE EQUIVALENT
STEP 2: FIND FIRST TWO BITS OF PC OR BASE REGISTER.
STEP 3: CALCULATE DECIMAL VALUE OF TWO BITS.
E.G: STA - 14
FIRST TWO BITS OF PROGRAM COUNTER ( 11 0010) IS 11.
THE DECIMAL EQUIVALANT IS 1
1
1 X 20= 1
1 X 21= 2
k/
t
.
e
b
u
t
e
s
c
/
/
:
p
t
ht
3 X 160= 3
48
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
48 3 = 45.
16 45
2 - D
HEXADECIMAL 0F 45 IS
2 D.
Example 1:
15
.
.
.
40
45
0006 CLOOP
+JSUB
0017
0014 ENDFIL
J
LDA
RDREC
CLOOP
EOF
1
i
0
x
0
b
1
p
-----03201D
/
k
t
.
e
b
u
t
e
4B101036
0
e
Ist two bits are 1(As n and i=1)
Hexadecimal equivalent of 11 is 3.
find the displacement values,
CLOOP location is 0006 and pc values is 1A.
s
c
/
/
:
p
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
1110
1111
1110
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
Eg:
0006
.
.
1036
t
t
h
CLOOP
+JSUB
RDREC
4B101036
RDREC
CLEAR
B410
11. Difference between program counter addressing and base relative addressing. (2 marks)
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
Case 1:
The statement RDREC is present at the memory location 1036,if the program loaded beginning
at address 0000.
0000
.
0006
.
.
1036
4B101036
B410
<--+JSUB
RDREC
<--RDREC
Case 2:
5000
.
.
5006
4B106036
<--+JSUB
RDREC
.
.
6036
B410
<--RDREC
The address of the instruction JSUB the address of label RDREC.
The assembler does not know the actual location where the program will be loaded.However the
assembler can identify for the loader those parts of the object program that need modification.
An object program that contain the information to perform this kind of modification is called a
relocatable program.
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
Modification Record12:
col:1
col:2-7
M
Starting location of the address field to be modified relative to the beginning of
the program.
Col:8-9
Length of address field to be modified in half bytes.
(ie. 4 bits=1 half byte)
For all the instruction which uses extended format instruction,relocation must be performed,
so modification record must be added.
Other lines in the program do not require modification as they use pc relative or base
relative addressing.
12. Define Modification record.Explain.(2 marks).
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
For example,
Same literal is used more than once,and the literal has different values during the execution of
the program.Here according to the duplication of literals in the pool,the above mentioned literal is
appeared once in the pool and the execution may be a problem.
/
k
t
17
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
Eg:
A
X
L
EQU 0
EQU 1
EQU 2
BASE
COUNT
INDEX
EQU
EQU
EQU
R1
R2
R3
These statements specify a 1 byte literal with the hexadecimal value 05.The notation used
for literal varies from assembler to assembler.
It is important to understand the difference between a literal and an immediate operand with
immediate addressing,the operand value is assembled as part of the machine instruction.
With literal the assembler generates the specified value as a constant at some other memory
location.
BASE
*
LDB
=*
Another assembler directive is called ORG.This is used to indirectly assign the values to
symbols.
When value is a constant or an expression involving constants and previously defined
symbol.
SYMBOL
RESB
6
VALUE
RESB
1
FLAGS
RESB
2
ORG
STAB +1100
The first ORG resets the location counter to the value of STAB.The label on the following
RESB statements defines SYMBOL to have the current value in LOCCTR.
(ie)the same address assigned to SYMTAB LOCCTR.
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
5.3 Expressions:
Our previous examples of assembler language statements have used single terms like
label,literal,etc.,as instruction operands.
Most of the assemblers use expression wherever a single operand is permitted.
Such expression is evaluated by the assembler and the result is used as the normal operand.
Arithmetic expressions are allowed and it must follow the normal rules using the operators
+,-,* and /.
This statement is encountered during assembly of a program,the assembler refers its location
contain(LOCCTR)to the specified value we can define a symbol tabel with all following
structures.
SYMBOL
VALUE
FLAGS
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
*Absolute expression
*Relative expression
The expressions are depending upon the type of value they produce.
Expression that contains only absolute terms are come under absolute expression.
There are some conditions19 to use the relative terms in the expressions,
*Every relative term is paired with another relative term.
*Remaining unpaired term is assigned with a pasitive sign.
*Relative term is not allowed for multiplication and division operation.
Expressions that do not come under absolute or relative are flagged by the assembler an
errors.
Some timer relative terms are paired with opposite signs,in that case the result is an absolute
value.
MAXLEN
EQU
BUFEND-BUFFER
t
t
h
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
20. what are blocks in program. How they classified. Explain.(2 marks)
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
Block Name
Block Number
Address
Length
Default
0000
0066
C DATA
0066
00013
C BLKS
0071
1000
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
DEFINE RECORD:
COL 1
:D
COL 2-7
:Name of the external symbol defined in this Control section.
COL 8-13
:Relative address of symbol.
COL 14-73 : Repeat information in col 2-13 for other external symbol.
21. Define DEFINE record and REFER record.explain.( 2 marks)
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
REFER RECORD:
COL 1
:R
COL 2-7
:Name of external symbol.
COL 8-13
:Name of the other external reference symbols.
MODIFICATION RECORD:
COL 1
:M
COL 2-7
:Starting address of the field to be modified.
COL 8-9
:Length of the field to be modified as half bytes.
COL 10
:Modification flag.
COL 11-16 :External symbol whose value is to be added or subtracted to the
indication field.
6. One pass assemblers and Multipass assemblers:
/
k
t
.
e
b
u
t
e
assembler.
External storage for the intermediate file between two passes is slow or is
s
c
/
inconvenient to use.
/
:
p
tt
Labels on instructions
Solution
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
When the definition for the symbol is encountered, scans the reference list and
inserts the address.
At the end of the program, reports the error if there are still SYMTAB entries
indicated undefined symbols.
6.2 Multi-Pass Assemblers:
For a two pass assembler, forward references in symbol definition are not allowed:
ALPHA
EQU
BETA
BETA
EQU
DELTA
DELTA
RESW
/
k
t
Forward references tend to create difficulty for a person reading the program.
.
e
b
6.2.1 Implementation:
u
t
e
s
c
/
/
:
p
t
t
h
7. IMPLEMENTATION EXAMPLE:
MASAM assembler
SPARC assembler
III CSE
UNIT II
http://csetube.weebly.com/
G.PRABHAKARAN AP/CSE
S.SELVARANI AP/CSE
MASAM assembler
MASAM assembler is written for Pentium and other x 86 systems.
Since x 86 system views memory as a collection of segments, MASAM
assembler language program is written as a collection of segments.
Each segment is defined as belonging to a particular class.
Commonly used classes are CODE, DATA, CONST and STACK.
During program execution, segments are addressed via the x 86 segment
/
k
t
registers.
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
Near jump
Far jump
III CSE
UNIT II
http://csetube.weebly.com/