The main goal of Pass 2 in an assembler is to create the machine code and write the final object program. This includes:
Converting symbolic machine opcodes into their binary form.
Storing all opcodes in the Machine Opcode Table (MOT) with their symbolic code and binary representation.
Processing pseudo-operations and storing them in the Pseudo Opcode Table (POT).
Replacing labels with memory addresses and converting instructions into binary code.
Writing the final object code and generating a listing of the source code.
Note: In Pass 1, the assembler checks for errors, assigns memory locations to instructions and symbols, and creates an intermediate file.
Symbol Table:
Stores symbols (labels) and their corresponding memory addresses.
Used to replace symbolic addresses with actual memory addresses in the object code.
Literal Table:
Stores literal values (constants) and their memory addresses.
Ensures literals are correctly included in the object code.
Location Counter (LC):
Tracks the memory address of each instruction and data item.
Helps assign the correct addresses in the object code.
Opcode Table (OPTAB):
Contains machine opcodes and instruction formats.
Used to look up the opcode and format for each instruction in the source code to generate the correct machine code.
begin
read first input line (from intermediate file)
if OPCODE ='START' then
begin
write listing line
read next input line
end {if START}
write Header record to object program
initialize first Text record
while OPCODE != 'END' do
begin
if this is not a comment line then
begin
search OPTAB for OPCODE
if found then
begin
if there is a symbol in OPERAND field then
begin
search SYMTAB for OPERAND
if found then
store symbol value as operand address
else
begin
store 0 as operand address
set error flag (undefined symbol)
end
end {if symbol}
else
store 0 as operand address
assemble the object code instruction
end {if opcode found}
else if OPCODE ='BYTE' or 'WORD' then
convert constant to object code
if object code will not fit into the current Text record then
begin
write Text record to object program
initialize new Text record
end
add object code to Text record
end {if not comment}
write listing line
read next input line
end(while not END)
write last Text record to object program
write End record to object program
write last listing line
end{Pass 2}
Intermediate:
- COPY START 1000
1000 - +LDA ALPHA
1004 - ADD #1
1007 - DIV #2
100A - STA BETA
100D - SUB #4
1010 - STA GAMMA
1013 - +LDA GAMMA
1017 - ADD #2
101A - +LDA BETA
101E ALPHA BYTE C'COF'
1021 BETA RESW 2
1027 GAMMA RESW 2
102D - END 1000
Optab:
DIV 06
SUB 05
CMP 03
LDA 00
STA 23
ADD 01
JNC 08
Symbol Tab:
ALPHA 101E
BETA 1021
GAMMA 1027
#iinclude<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
char *decimalToHex(int decimal) {
static char hex_string[20];
sprintf(hex_string, "%X", decimal);
return hex_string;
}
void main() {
printf("\nPASS 2.\n");
char loc[20], label[20], opcode[20], operand[20];
char optab_opcode[10], optab_machine_code[10];
char symtab_label[10], symtab_address[10];
int count = 0, start, end, length = 0;
bool opcode_found = false;
bool label_found = false;
FILE *input, *output, *symtab, *optab;
input = fopen("intermediate.txt", "r");
output = fopen("object.txt", "w");
symtab = fopen("symtab.txt", "r");
optab = fopen("optab.txt", "r");
if (input == NULL || output == NULL || symtab == NULL || optab == NULL){
printf("Error! File not found!");
exit(0);
} else {
printf("All files opened successfully!\n");
}
fscanf(input, "%s %s %s %s", loc, label, opcode, operand);
start = strtol(operand, NULL, 16);
while (!feof(input)) {
if(strcmp(opcode,"BYTE")==0){
count+=strlen(operand)-3;
}
if (!(strcmp(opcode, "START") == 0 || strcmp(opcode, "END") == 0 || strcmp(opcode, "RESW") == 0 || strcmp(opcode, "RESB") == 0 || strcmp(opcode,"BYTE")==0)) {
count+=3;
}
fscanf(input, "%s %s %s %s", loc, label, opcode, operand);
}
end = strtol(loc, NULL, 16);
printf("END: %d/ START: %d", end, start);
length = end - start;
rewind(input);
fscanf(input, "%s %s %s %s", loc, label, opcode, operand);
fprintf(output, "H^%s^00%s^0000%d\n", label, operand, length);
fprintf(output, "T^00%s^%d^", operand,count);
while (!feof(input)) {
if (strcmp(opcode, "START") == 0 || strcmp(opcode, "END") == 0 || strcmp(opcode, "RESW") == 0 || strcmp(opcode, "RESB") == 0) {
fscanf(input, "%s %s %s %s", loc, label, opcode, operand);
continue;
} else {
rewind(optab);
opcode_found = false;
fscanf(optab, "%s %s", optab_opcode, optab_machine_code);
while (!feof(optab)) {
if (strcmp(opcode, optab_opcode) == 0) {
opcode_found = true;
break;
}
fscanf(optab, "%s %s", optab_opcode, optab_machine_code);
}
if (opcode_found) {
rewind(symtab);
label_found = false;
fscanf(symtab, "%s %s", symtab_label, symtab_address);
while (!feof(symtab)) {
if (strcmp(symtab_label, operand) == 0) {
label_found = true;
break;
}
fscanf(symtab, "%s %s", symtab_label, symtab_address);
}
if (label_found) {
fprintf(output, "%s%s^", optab_machine_code, symtab_address);
} else {
printf("Error! Label not found in symtab!\n");
exit(0);
}
} else if (strcmp(opcode, "WORD") == 0) {
fprintf(output, "00000%s ", operand);
}
else if (strcmp(opcode, "BYTE") == 0) {
for (int i = 2; i < strlen(operand) - 1; i++) {
fprintf(output, "%s", decimalToHex(operand[i]));
}
fprintf(output, "^");
} else {
printf("Error!\n %s", opcode);
exit(0);
}
}
fscanf(input, "%s %s %s %s", loc, label, opcode, operand);
}
fprintf(output, "\nE^00%s\n", decimalToHex(start));
fclose(input);
fclose(output);
fclose(symtab);
fclose(optab);
output = fopen("object.txt", "r");
char str = fgetc(output);
printf("\n\nObject program file/ Output file : \n\n");
while (str != EOF) {
printf("%c", str);
str = fgetc(output);
}
fclose(output);
}