You are on page 1of 8

Name: Sayed Zahra Abadi Course Name: SPCC Lab

Roll No: 19CO07 Class: TE CO


Experiment No: 02 Date: 06/03/2022

Experiment No: 02

AIM : Implementation of Two Pass Assembler.

THEORY :
In two pass assembler the forward reference problem can be resolved easily
because in first pass all the LC processing is done and all the symbols that are used
in the source program has the valid memory addresses associated with them.
So the second pass only use the symbol table and generates the target code by using
the addresses found in symbol tables.
Following are the tasks that are performed during two pass of assembler :
⮚ PASS I :
1. It seperates the labels, mnemonic opcode and operand fields of
statement.
2. Validate the mnemonic opcode through opcode table.
3. Build the symbol and literal table.
4. Perform the LC processing
5. Generate the intermediate code.
⮚ PASS II :
1. Takes the addresses of symbols from symbol table, addresses of literals
from literal table and addresses of opcodes from opcode table.
2. Synthesize the target program.
PASS I performs the analysis of source program while PASS II performs the
synthesis phase of assembler.
◆ DATA STUCTURE USED IN PASS 1 OF PASS 2 ASSEMBLER :
1. OPTAB (Opcode table) :
It is used to see mnemonic operation codes and converts them to their
equivalent machine language. In pass 1 the OPTAB checks theoperation code
in the program and validates it. OPTAB is also called as mnemonic table.
The table contains the three fields :
1. Opcode 2. Class 3. Info
2. SYMTAB (Symbol table) :
Symbol table includes the name and address for each label in the source
program.
In pass 1 all the symbols used in the source program are added in the
symbol table with the corresponding address and length.
In pass 2 addresses of all the symbols used in program are taken from symbol
table and added into the assembly instruction to generate target program.
3. LITTAB (Literal table ) :
Literal table includes the name of the literals and there corresponding
addresses from the source program.
4. POOLTAB (Pool table ) :
Pool table is used to record the pools in the literal table.
PROGRAM :
INPUT_INSTRUCTION.TX
T MOVI R0 5
MOVI R1 2
MOVI R2 3
MOVI R3 2
MOVI R4 2
MOVI R5 0
MOVI R10
2 P1:
CMP R0
R10 JEQ C1
P2:
CMP R1
R10 JEQ C2
P3:
CMP R2
R10 JEQ C3
P4:
CMP R3
R10 JEQ C4
P5:
CMP R4
R10 JEQ C5
JMP
HL C1:
INC R5
JMP P2
C2:
INC R5
JMP P3
C3:
INC R5
JMP P4
C4:
INC R5
JMP P5
C5:
INC
R5 HL:
MOVI R5
3 OUT R5
HLT

INPUT_OPCODE.TXT
ADDI 000000000000 rri
SUBI 000000000001 rri
MULI 000000000010 rri
DIVI 000000000011 rri
MODI 000000000100 rri
ANDI 000000000101 rri
ORI 000000000110 rri
XORI 000000000111 rri
NANDI 000000001000 rri
NORI 000000001001 rri
ADD 00000000101000000 rrr
SUB 00000000101000001 rrr
MUL 00000000101000010 rrr
DIV 00000000101000011 rrr
AND 00000000101000100 rrr
OR 00000000101000101 rrr
XOR 00000000101000110 rrr
NAND 00000000101000111 rrr
MOD 00000000101001000 rrr
NOR 00000000101001001 rrr
ARRI 00000000101001010 rrr
ARRC 00000000101001011 rrr
CMPI 00000000101001100 ri
MOVI 00000000101001101 ri
NOTI 00000000101010000 ri
CMP 0000000010101000100000 rr
MOV 0000000010101000100001 rr
NOT 0000000010101000100010 rr
ARRD 0000000010101000100011 rr
LOAD 0000000010101000100100 rr
STORE 0000000010101000100101 rr
CALL 0000000010101000100110 a
JEQ 0000000010101000100111 a
JLT 0000000010101000101000 a
JGT 0000000010101000101001 a
JMP 0000000010101000101010 a
INC 000000001010100010101100000 r
DEC 000000001010100010101100001 r
CLR 000000001010100010101100010 r
LSHIFT 000000001010100010101100011 r
RSHIFT 000000001010100010101100100 r
PUSH 000000001010100010101100101 r
POP 000000001010100010101100110 r
IN 000000001010100010101100111 r
OUT 000000001010100010101101000 r
RET 00000000101010001010110100100000 z
MSF 00000000101010001010110100100001 z
HLT 00000000101010001010110100100010 z
EXIT 00000000101010001010110100100011 z

CODE :
pass2.c
SYMBOL TABLE :
P1 :: 8
P2 :: 11
P3 :: 14
P4 :: 17
P5 :: 20
C1 :: 24
C2 :: 27
C3 :: 30
C4 :: 33
C5 :: 36
HL :: 38
FINAL OUTPUT :
00000000101001101000000000000101
00000000101001101000010000000010
00000000101001101000100000000011
00000000101001101000110000000010
00000000101001101001000000000010
00000000101001101001010000000000
00000000101001101010100000000010
00000000101010001000000000001010
00000000101010001001110000011000
00000000101010001000000000101010
00000000101010001001110000011011
00000000101010001000000001001010
00000000101010001001110000011110
00000000101010001000000001101010
00000000101010001001110000100001
0000000101010001000000010001010
00000000101010001001110000100100
00000000101010001010100000100110
00000000101010001010110000000101
00000000101010001010100000001011
00000000101010001010110000000101
00000000101010001010100000001110
00000000101010001010110000000101
00000000101010001010100000010001
00000000101010001010110000000101
00000000101010001010100000010100
00000000101010001010110000000101
00000000101001101001010000000011
00000000101010001010110100000101
00000000101010001010110100100010
Conclusion :
In this experiment we learned about the two pass assembler
including it working, flowchart ,data structure with example
implemented it using c language.

You might also like