SPCC EXP 1 Saif
SPCC EXP 1 Saif
EXPERIMENT NO. 1
AIM: To Design and implement two pass assembler for X86 machine.
THEORY:
An assembler is a language translator that takes as input assembly language program &
generates its machine language equivalent along with the information required by the
loader.
The rules in ALP state that the symbol should be defined somewhere in the course of
the program .Hence there may be some cases in which the reference to the symbol is
made prior to its definition & such a reference is called as Forward Reference . Due to
Forward Reference the assembler cannot assemble the instructions & such a problem
is called as Forward Reference Problem.
To resolve Forward Reference Problem we make two passes over the input program.
The purpose of Pass1 is to define the symbols & literals encountered in the program.
The purpose of Pass2 is to assemble the instructions & assemble the data.
The database consists of the following tables:
MOT is a fixed length table i.e. no entries are made in it in either of the passes.
In Pass1 it is consulted to obtain instruction length [update LC]. In Pass2 it is consulted
to obtain binary opcode, instruction length & instruction format.
POT is a fixed length table ie no entries are made in it in either of the passes. In both the
passes it is consulted to process pseudo opcodes like START, USING, DROP, DC, DS, etc
ST [Symbol Table]
Symbol Value Length Relocation [R/A]
ST is used for keeping track of symbols that are defined in the program.
221230 Saif Madre SPCC B batch
LT [Literal Table]
Literal Value Length Relocation [R/A]
LT is used for keeping track of symbols that are defined in the program. In Pass1
whenever a literal is defined an entry is made in the LT. In Pass2 LT is used for generating
the address of the literals.
BT [Base Table]
Availability indicator Contents of base register
1 ‘n’
:
15 ‘n’
BT is used to keep track on the contents of the base register.
In Pass1 BT is not required.
In Pass2 BT is used for processing USING & DROP statements.
PROGRAM:
SIMPLE LOOP THAT INCREMENTS A REGISTER AND STORES THE RESULT IN MEMORY.
LINE BY LINE
1. Program Start:
o PROG1 START 0: The program starts at memory location 0.
2. Base Register Setup:
o USING *,15: Register 15 is used as the base register.
3. Load Address into Register:
o LA 15,SETUP: Loads the address of SETUP into register 15.
221230 Saif Madre SPCC B batch
4. Clear Register:
o SR R1,R1: Clears register R1 by subtracting it from itself.
5. Load Literal into Register:
o L R2,=F'10': Loads the literal value 10 into register R2.
6. Loop Start:
o LOOP: Marks the start of a loop.
7. Increment Register:
o A R1,=F'1': Adds 1 to register R1.
8. Store Register Value:
o ST R1,COUNT: Stores the value in R1 into the memory location COUNT.
9. Compare Registers:
o C R1,R2: Compares the value in R1 with the value in R2.
10. Branch if Not Equal:
o BNE LOOP: Branches back to LOOP if R1 is not equal to R2.
11. Load Register:
o LR 3,R1: Loads the value in R1 into register 3.
12. Branch to Register 14:
o BR 14: Branches to the address in register 14.
13. Literal Pool:
o LTORG: Generates a literal pool for literals like =F'1' and =F'10'.
14. Define Storage:
o COUNT DS F: Defines a fullword (4 bytes) of storage labeled COUNT.
15. End of Program:
o END: Marks the end of the program.
Code
TwoPassAssembler.java :
StringTokenizer st = new
LitTuple(String s1, int i1, int i2, String s2) { StringTokenizer(s, " ", false);
literal = s1; String s_arr[] = new
value = i1; String[st.countTokens()];
length = i2; for (int i = 0; i < s_arr.length; i++) {
ra = s2; s_arr[i] = st.nextToken();
} }
} if (searchPot1(s_arr) == false) {
searchMot1(s_arr);
class TwoPassAssembler { out_pass1.println(s);
static int lc; }
static List<Tuple> mot; lclist.add(lc);
static List<String> pot; }
static List<SymTuple> symtable; int j;
static List<LitTuple> littable; String output = new String();
static List<Integer> lclist; System.out.println("Symbol Table:");
static Map<Integer, Integer> basetable; System.out.println("Symbol Value
static PrintWriter out_pass2; Length R/A");
static PrintWriter out_pass1; for (SymTuple i : symtable) {
static int line_no; output = String.format("%-10s %-8d %-
8d %s", i.symbol, i.value, i.length, i.ra);
public static void main(String args[]) System.out.println(output);
throws Exception { out_symtable.println(output);
initializeTables(); }
System.out.println("====== PASS 1 System.out.println("\nLiteral Table:");
======\n"); System.out.println("Literal Value
pass1(); Length R/A");
System.out.println("\n====== PASS 2 for (LitTuple i : littable) {
======\n"); output = String.format("%-10s %-8d %-
pass2(); 8d %s", i.literal, i.value, i.length, i.ra);
} System.out.println(output);
out_littable.println(output);
static void pass1() throws Exception { }
BufferedReader input = new }
BufferedReader(new
InputStreamReader(new static void pass2() throws Exception {
FileInputStream("input.txt"))); line_no = 0;
out_pass1 = new PrintWriter(new out_pass2 = new PrintWriter(new
FileWriter("output_pass1.txt"), true); FileWriter("output_pass2.txt"), true);
PrintWriter out_symtable = new BufferedReader input = new
PrintWriter(new BufferedReader(new
FileWriter("out_symtable.txt"), true); InputStreamReader(new
PrintWriter out_littable = new FileInputStream("output_pass1.txt")));
PrintWriter(new String s;
FileWriter("out_littable.txt"), true); System.out.println("Pass 2 input:");
String s; while((s = input.readLine()) != null) {
while ((s = input.readLine()) != null) { System.out.println(s);
221230 Saif Madre SPCC B batch
input.txt : mot.txt:
pot.txt:
221230 Saif Madre SPCC B batch
OUTPUT
Conclusion:
Thus, we have successfully implemented two pass Assembler.