Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ASSIGNMENT4
WriteaprogramtoimplementII
passassembler.(For
hypotheticalinstructionsetfrom
Dhamdhere)
Considerfollowingcasesonly
(Literalprocessingnotexpected):
a.Forwardreferences
b.DSandDCstatement
c.START,EQU,END
d.Errorhandling:symbolused
butnotdefined,invalid
instruction/registeretc.
Objective:
1.Tostudybasictranslation
processofassemblylanguageto
machinelanguage.
2.Tostudytwopassassembly
process.
Tasksperformedbythepasses
oftwopassassemblerareas
follows:
PassI:
1.Separatethesymbol,
mnemonicopcodeandoperand
fields.
2.Determinethestorage
requiredforeveryassembly
languagestatementandupdate
the
locationcounter.
3.Buildthesymboltableandthe
literaltable.
4.Constructtheintermediate
codeforeveryassembly
languagestatement.
PassII:
1.Synthesizethetargetcodeby
processingtheintermediatecode
generatedduringPassI.
DesignofaTwo
PassAssembler:
Datastructuresrequired
forpassI:
1.Sourcefilecontaining
assemblyprogram.
2.MOT:Atableofmnemonic
opcodesandrelated
information.
Ithasthefollowingfields
Mnemonic,TYPE,OPcode
,Length
HashtableImplementationof
MOTtominimizethesearch
timerequiredforsearching
theinstruction.
3.SYMTAB:Thesymbol
table.
FieldsareSymbolname,
Address(LCValue).
4.Intermediateformused
Variant1/Variant2
3.SYMTAB:Thesymboltable.
FieldsareSymbolname,
Address(LCValue).
4.IntermediateformusedVariant
1/Variant2
DataStructureusedby
PassII:
1.OPTAB:Atableofmnemonic
opcodesandrelatedinformation.
2.SYMTAB:Thesymboltable.
3.LITTAB:Atableofliteralsused
intheprogram.
4.Intermediatecodegenerated
byPassI.
5.OutputfilescontainingTarget
code/errorlisting.
PASSI
INPUT
Textfileinassemblycode.
MOTtable(Hypothetical
Instructionset).
OUTPUT
Intermediatecodefilein
variant1/2.
SymbolTable.
PASSII
INPUT
MOTtable(Hypothetical
Instructionset).
Intermediatecodefilein
variant1/2.
SymbolTable.
OUTPUT
MachineCode
AlgorithmPASSI
1.Openthesourcefileininput
mode.
2.ifendoffileofsourcefilegoto
step8.
3.Readthenextlineofthe
sourceprogram
4.Separatethelineintowords.
Thesewordscouldbestoredin
arrayofstrings.
5.Searchforfirstwordis
mnemonicopcodetable,ifnot
presentitisalabel,addthisasa
symbolinsymboltablewith
currentLC.Andthensearchfor
secondwordinmnemonic
opcodetable.
6.Ifinstructionisfound
case1:imperativestatement
case2:Declarativestatement
case3:AssemblerDirective
GenerateIntermediatecodeand
writetoIntermediatecodefile.
7.gotostep2.
8.Closesourcefile.
AlgorithmPASSII
1.openintermediatecodefile
2.Ifendoffile(Intermediate
code),gotostep2
3.Readnextlinefrom
intermediatecodefile.
4.Writeopcode,registercode,
andaddressofmemory(tobe
fetchedfromsymboltable)onto
targetfile.Thisistobedoneonly
forImperativestatement.
5.gotostep2.
6.Closeallfiles.
7.Displaysymboltableandtarget
file.
11
AlgorithmFortwopassassemblerPassI
1
Initialization
SetLC=0
STP=1//SymbolTablePointer
RC=0//RegisterCodeAREG=1,BREG=2,CREG=3,DREG=4
Whilenextstmt.isnotanENDstmt
a
IfLabelispresentthen
this_label=symbolinlabelfield
if(checkSYMTAB(this_label))
Address=LCLength=operand1
else
Enter(this_label,LC)inSYMTAB
ifnextMnemonicthengotostep2f
12
b
IfaSTARTorORIGINthen
LC=valuespecifiedinoperandfield.
IfanEQUstmtthen
1. this_addr=valueof<addr_spec>
//IfReqdfetchfromSYMTAB
2. CorrecttheSYMTABentryforthis_labelto
(this_label,this_addr)
d
IfaDeclarationstmtthen
IfDCthen
1. Generatecode(DL,01)
2. size=sizeofconstant(1)
3. LC=LC+size
IfDSthen
4. Generatecode(DL,02)
5. size=sizespecifiedasarg.ofDSstmt.
6. ReflectthesizeinSYMTAB
7. LC=LC+size
13
e
IfanImperativestmtthen
3. code=opcodeformOPTAB
4. IfOperandisLiteralthen
this_literal=literalinoperandfield
LITTAB[LTP]=this_literal
GenerateICas(IS,code)RC(L,LTP)
LTP=LTP+1
IfoperandisRegisterthen
RC=Appropriateregistercode
IfOperandisSymbolthen
CheckSYMTAB(operand)
If(!EntryFound)then
Enter(SYMTAB[STP]=symbol)
STP=STP+1
this_entry=SYMTABentryno.ofoperand
GenerateICas(IS,code)(RC)(S,this_entry)
LC=LC+instructionlengthfromOPTAB
3
(ProcessingOfENDStatement)
a
b
GenerateIC(AD,02)
GotoPassII
ErrorHandling
Symbolusedbutnotdefined,
TobehandledinPASSII.Whenwereplace(s01)with
address,soifaddressisnotfoundinSymTab,
Generateanerror.
Invalidinstruction/registeretc.
TobehandledinPASSI.Whenwesearchtokenin
MOTtableifnotfound,Generateanerror.
LineNo
Label
MnemonicCode
Operand1
Operand2
1
START
200
2
MOVER
AREG,
C
3
MOVEM
AREG,
A
4
LOOP
MOVER
AREG,
A
5
MOVER
CREG
B
6
ADD
CREG
D
7
NEXT1
SUB
AREG
D
8
MULT
CREG
B
9
STOP
10
A
DS
2
11
B
DC
3
12
C
DC
5
13
D
DC
1
14
NEXT1
EQU
LOOP
15
END
Example
MnemonicCodeTable
(MOTtable)
Mnemonic
Code
Machine
Code
Class
LengthInsructio
STOP
00
IS
1
ADD
01
IS
2
SUB
02
IS
2
MULT
03
IS
2
MOVER
04
IS
2
MOVEM
05
IS
2
COMP
06
IS
2
BC
07
IS
2
DIV
08
IS
2
READ
09
IS
2
PRINT
10
IS
2
DC
01
DL
1
DS
02
DL
SIZE
PseudoOpCodeTable
(POTtable)
Pseudoopcode
Code
IntermediateMachine
Code
Class
LengthInsructio
START
01
AD
END
02
AD
LTONG
03
AD
ORIGIN
04
AD
EQU
05
AD
PASSI
Intermediatecode,Symbol
Table
19
MnemonicCode
Operand1
Operand2
LC
IntermediatecodepassI
1
START
200
(AD01)(200)
2
MOVER
AREG
C
200
(IS4)(01)(S01)
3
MOVEM
AREG
A
201
(IS5)(01)(S02)
4
LOOP
MOVER
AREG
A
202
(IS4)(01)(S02)
5
MOVER
CREG
B
203
(IS4)(03)(S04)
6
ADD
CREG
D
204
(IS01)(03)(S05)
8
NEXT1
SUB
AREG
D
207
(IS02)(01)(S05)
20
9
MULT
CREG
B
208
(IS03)(03)(S4)
10
STOP
209
(IS00)
11
A
DS
2
210211
(DL02)(C2)
12
B
DC
3
212
(DL01)(C3)
13
C
DC
5
213
(DL01)(C5)
D
14
DC
1
214
(DL01)(C1)
15
NEXT1
EQU
LOOP
(AD05)
(AD02)
SRNo
Symbolname
address
size
1
C
213
1
2
A
210
2
3
LOOP
202
16
END
B
212
3
5
D
214
1
6
NEXT1
202
PASSII
MachineCode
22
AlgorithmFortwopassassemblerPassII
1
Initialization
SetLC=0
RC=0//RegisterCodeAREG=1,BREG=2,CREG=3,DREG=4
Whilenextstmt.isnotanENDstmt
a
Clearmachinecodebuffer
23
b
IfaSTARTorORIGINthen
LC=valuespecifiedinoperandfield.
Size=0
c
IfaDeclarationstmtthen
IfDCthen
8. Assembletheconstantinmachine_code_buffer
9. size=sizeofmemoryarearequiredbyDC/DS
IfanImperativestmtthen
5. GetoperandaddressfromSYMTAB/LITTAB
6. Assembleinstructioninmachine_code_buffer
7. Size=sizeofinstruction.
Ifsize!=0
movecontentsofmachine_code_buffertocode_area
1. Lc=Lc+size
(ProcessingOfENDStatement)
Performsteps2(e)
Writecode_areaintooutputfile.
24
MnemonicCode
Operand1
Operand2
LC
MachineCOde
1
START
200
2
MOVER
AREG
C
+200
401213
3
MOVEM
AREG
A
+201
501210
4
LOOP
MOVER
AREG
A
+202
401210
5
MOVER
CREG
B
+203
403212
6
ADD
CREG
D
+204
103214
8
NEXT1
SUB
AREG
D
+207
201214
25
MULT
CREG
B
+208
303212
10
STOP
+209
00
11
A
DS
2
+210211
12
B
DC
3
+212
13
C
DC
5
+213
14
D
DC
1
+214
15
NEXT1
EQU
LOOP
16
END
Example2
START100
READN
MOVERBREG,ONE
AGAINMULTBREG,TERM
MOVERCREG,TERM
BCLE,AGAIN
MOVEMBREG,RESULT
PRINTRESULT
STOP
NDS1
RESULTDS1
ONEDC1
TERMDS1
END