Pass 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

LABORATORY PRACTICE – 1

ASSIGNMENT NO.: 2
Problem Statement:
Implement Pass-II of two pass assembler for pseudo-machine in Java/C++ using object
oriented features. The output of assignment-1 (intermediate file and symbol table) should be
input for this assignment.
Objectives:
1. To study the design and implementation of 2nd pass of two pass assembler.
2. To study the data structure used in Pass-2 of assembler implementation.
Theory:

1. Explain the design of Pass- II of assembler with the help of flowchart and example.

Data Structure used by Pass II

1. OPTAB: A table of mnemonic opcodes and related information.


2. SYMTAB: The symbol table
3. POOL_TAB and LITTAB: A table of literals used in the program
4. Intermediate code generated by Pass I
5. Output file containing Target code / error listing.

Algorithms (procedure):

1. code_area_address=address of code area;


Pooltab_ptr:=1;
loc_cntr=0;
2. While next statement is not an END statement

a) clear the machine_code_buffer


b) if an LTORG statement
I) process literals in LITTAB[POOLTAB[pooltab_ptr]]…
LITTAB[POOLTAB[pooltab_ptr+1]]-1 similar to processing of constants in a dc statement.
II) size=size of memory area required for literals
III) pooltab_ptr=pooltab_ptr+1
c) if a START or ORIGIN statement then
I) loc_cntr = value specified in operand field
II) size=0;
d) if a declaration statement
I) if a DC statement then assemble the constant in machine_code_buffer
II) size=size of memory area required by DC or DS:
e) if an imperative statement then
I) get operand address from SYMTAB or LITTAB
II) Assemble instruction in machine code buffer.
III) size=size of instruction;
f) if size # 0 then
I) move contents of machine_code_buffer to the address code_area_address+loc_cntr ;
II) loc_cntr=loc_cntr+size;
3. (Processing of END statement)
Algorithm/Flowchart
Input:
Intermediate code of pass-1.
LC LABEL INSTR. OPERANDS

100 SAMPLE START 100


100 USING *, 15
100 L 1, FOUR
104 A 1, =F’3’
108 ST 1, RESULT
112 SR 1, 2
114 LTORG
124 L 2, FIVE
128 A 2, =F’5’
132 A 2, =F’7’
136 FIVE DC F’5’
140 FOUR DC F’4’
144 RESULT DS 1F
152 5
156 7
160 END

Machine Opcode Table (MOT)

Mnemonic Hex / Binary Length (Bytes) Format


Code
L 5A 4 RX
A 1B 4 RX
ST 50 4 RX
SR 18 2 RR

Pseudo Opcode Table (POT)

Pseudo op Address / Name of Procedure to implement pseudo


operation
START PSTART
USING PUSING
DC PDC
DS PDS
LTORG PLTORG
END PEND
Symbol Table (ST)

Sr. No Symbol name Address Value Length Relocation


1 SAMPLE 100 -- 160 R
2 FIVE 136 5 4 R
3 FOUR 140 4 4 R
4 RESULT 144 4 R

Literal Table (LT)


Sr. No. Literal Address Length
1 3 120 4
2 5 152 4
3 7 156 4

Output:
Base Table (BT)

Register no. Availability Value/ Contents


1 N --
: : :
: : :
: : :
15 Y 100

Object Code

LC OPCODE OPERAND
100 5A 1,40(0,15)
104 1B 1,20(0,15)
108 50 1,44(0,15)
112 18 1,2
124 5A 2,36(0,15)
128 1B 2,52(0,15)
132 1B 2,56(0,15)
Test Cases:

1. Check syntax of instruction (Correct and wrong)


2. Symbol not found
3. Wrong instruction
4. Duplicate symbol declaration
5. Test the output of program by changing value of START & USING pseudo opcode.
Software Requirement:
1. Fedora
2. Eclipse
3. JDK

Hardware Requirement:
4GB RAM, 500GB HDD
Conclusion:
The intermediate data structures generated in Pass-I of assembler are given as input to the Pass-
II of assembler, processed by applying Pass-II algorithm of assembler and machine code is
generated
CODE:
/*
Problem Statement: Implement Pass-II of two pass assembler for pseudo-machine in Java
using object oriented
features. The output of assignment-1 (intermediate file and symbol table) should be
input for this assignment.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

public class Pass2 {


public static void main(String[] Args) throws IOException{
BufferedReader b1 = new BufferedReader(new FileReader("intermediate.txt"));
BufferedReader b2 = new BufferedReader(new FileReader("symtab.txt"));
BufferedReader b3 = new BufferedReader(new FileReader("littab.txt"));
FileWriter f1 = new FileWriter("Pass2.txt");
HashMap<Integer, String> symSymbol = new HashMap<Integer, String>();
HashMap<Integer, String> litSymbol = new HashMap<Integer, String>();
HashMap<Integer, String> litAddr = new HashMap<Integer, String>();
String s;
int symtabPointer=1,littabPointer=1,offset;
while((s=b2.readLine())!=null){
String word[]=s.split("\t\t\t");
symSymbol.put(symtabPointer++,word[1]);
}
while((s=b3.readLine())!=null){
String word[]=s.split("\t\t");
litSymbol.put(littabPointer,word[0]);
litAddr.put(littabPointer++,word[1]);
}
while((s=b1.readLine())!=null){
if(s.substring(1,6).compareToIgnoreCase("IS,00")==0){
f1.write("+ 00 0 000\n");
}
else if(s.substring(1,3).compareToIgnoreCase("IS")==0){
f1.write("+ "+s.substring(4,6)+" ");
if(s.charAt(9)==')'){
f1.write(s.charAt(8)+" ");
offset=3;
}
else{
f1.write("0 ");
offset=0;
}
if(s.charAt(8+offset)=='S')

f1.write(symSymbol.get(Integer.parseInt(s.substring(10+offset,s.length()-1)))+"\n");
else

f1.write(litAddr.get(Integer.parseInt(s.substring(10+offset,s.length()-1)))+"\n");
}
else if(s.substring(1,6).compareToIgnoreCase("DL,01")==0){
String s1=s.substring(10,s.length()-1),s2="";
for(int i=0;i<3-s1.length();i++)
s2+="0";
s2+=s1;
f1.write("+ 00 0 "+s2+"\n");
}
else{
f1.write("\n");
}
}
f1.close();
b1.close();
b2.close();
b3.close();
}
}
OUTPUT:
/*

intermediate code -
(AD,01)(C,200)
(IS,04)(1)(L,1)
(IS,05)(1)(S,1)
(IS,04)(1)(S,1)
(IS,04)(3)(S,3)
(IS,01)(3)(L,2)
(IS,07)(6)(S,4)
(DL,01)(C,5)
(DL,01)(C,1)
(IS,02)(1)(L,3)
(IS,07)(1)(S,5)
(IS,00)
(AD,03)(S,2)+2
(IS,03)(3)(S,3)
(AD,03)(S,6)+1
(DL,02)(C,1)
(DL,02)(C,1)
(AD,02)
(DL,01)(C,1)

Symbol Table --
A 211 1
LOOP 202 1
B 212 1
NEXT 208 1
BACK 202 1
LAST 210 1

literal table --
5 206
1 207
1 213
machine code --

+ 04 1 206
+ 05 1 211
+ 04 1 211
+ 04 3 212
+ 01 3 207
+ 07 6 208
+ 00 0 005
+ 00 0 001
+ 02 1 213
+ 07 1 202
+ 00 0 000
+ 03 3 212

*/

You might also like