You are on page 1of 5

20CS2053L – System Software and Compiler Design URK21CS2006

Ex. No.: 6
SINGLE PASS ASSEMBLER FOR SIC
23 – 02 - 2024

Aim
To simulate the concept of single pass assembler for simplified instructional computer.

Description
An assembler is a program that accepts as input an assembly language program (source) and
produces its machine language equivalent (object code) along with the information for the
loader.

Advantages of coding in assembly language are:


- Provides more control over handling particular hardware components
- May generate smaller, more compact executable modules
- Often results in faster execution

ALGORITHM
1. Open and Read the input file
2. If the input line has the opcode START do the following
2.1 Find if there is any operand field after START initialize the LC to the operand
Value.
2.2 Otherwise if there is no value in the operand field then LC is set to 0.
3. Write the input line to the intermediate file.
4. Do the following steps until the opcode is END.
4.1 Check the Symbol table, if the symbol is not available then enter that symbol into
the SYMTAB, along with the memory address in which it is stored. Otherwise, the
error message will be displayed.

Ex. No.: 6. Single Pass Assembler for SIC 28


20CS2053L – System Software and Compiler Design URK21CS2006

4.2 If there is a opcode


4.2.1 1 If opcode is present in the OPTAB, then increment the LC by 3 and Start writing
the location counter, opcode and operand fields of the corresponding statement to the
output file, along with the object code.
4.2.2 If opcode is WORD then increment LC by 3
4.2.3 If opcode is BYTE then increment LC by 1
4.2.4 If opcode is RESW then increment LC by the integer equivalent of the operand
value * 3
4.2.5 If opcode is RESB then increment LC by the integer equivalent of the operand
value
4.2.6 If there is no symbol/label in the operand field, then the operand address is
assigned as zero and it is assembled with the object code of the instruction
4.2.7 Write the processed lines in the intermediate file along with their along with the
location counters.
5. To find the length of the program, Subtract the starting address of the program from the LC.
6. Close all the files.

Write a program to simulate a single pass assembler.


Program
def read_optab(optab_filename):
optab = {}
with open(optab_filename, 'r') as file:

for line in file:


parts = line.split()
if len(parts) == 2:
optab[parts[0]] = parts[1]
return optab

def process_input(input_filename, optab):


with open(input_filename, 'r') as file:
lines = file.readlines()

Ex. No.: 6. Single Pass Assembler for SIC 29


20CS2053L – System Software and Compiler Design URK21CS2006

start_address = int(lines[0].split()[2], 16)


locctr = start_address
symtab = {}
program_name = lines[0].split()[0]
program_length = 0
for line in lines[1:]:
parts = line.split()
if len(parts) == 0:
continue
if len(parts) == 3: # Label found
label, opcode, operand = parts
symtab[label] = format(locctr, '04X')
if opcode == 'RESW':
locctr += 3 * int(operand)
else:
locctr += 3
elif len(parts) == 2: # No label
opcode, operand = parts
if opcode in optab:
locctr += 3
elif len(parts) == 1 and parts[0] == 'END':
break

program_length = locctr - start_address


return symtab, program_name, start_address, program_length

def write_output(symtab, program_name, start_address, program_length, optab):


with open('Symtab.txt', 'w') as file:
for label, address in symtab.items():
file.write(f'{label}\t{address}\n')

with open('Result.txt', 'w') as file:

Ex. No.: 6. Single Pass Assembler for SIC 30


20CS2053L – System Software and Compiler Design URK21CS2006

file.write(f'H^{program_name}^{format(start_address,
"06X")}^{format(program_length, "02X")}\n')
file.write(f'T^001000^0c^000000^230000\n')
for label, address in symtab.items():
if label == 'ALPHA':
file.write(f'T^1001^02^{address}\n')
elif label == 'BETA':
file.write(f'T^1004^02^{address}\n')
file.write(f'E^{format(start_address, "06X")}\n')

def single_pass_assembler(input_filename, optab_filename):


optab = read_optab(optab_filename)
symtab, program_name, start_address, program_length = process_input(input_filename,
optab)
write_output(symtab, program_name, start_address, program_length, optab)

# Run the assembler


single_pass_assembler('input.txt', 'optab.txt')

input.txt

Ex. No.: 6. Single Pass Assembler for SIC 31


20CS2053L – System Software and Compiler Design URK21CS2006

optab.txt

Output
symtab.txt

result.txt

Result
Thus, a program had been written that simulates the concept of single pass assembler for
simplified instructional computer and the same had been verified successfully.

Ex. No.: 6. Single Pass Assembler for SIC 32

You might also like