0% found this document useful (0 votes)
24 views3 pages

Pass 1

Uploaded by

Meghna Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Pass 1

Uploaded by

Meghna Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PASS 1

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> // For isdigit()

#define MAX_SYMBOLS 20

typedef struct {
char label[20];
int address;
} Symbol;

// Symbol table and location counter


Symbol symbolTable[MAX_SYMBOLS];
int symbolCount = 0;
int locationCounter = 0;

// Add symbol to the symbol table


void addSymbol(char *label, int address) {
strcpy(symbolTable[symbolCount].label, label);
symbolTable[symbolCount].address = address;
symbolCount++;
}

// Write symbol table to a file


void writeSymbolTableToFile(const char *filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file for writing.\n");
return;
}

for (int i = 0; i < symbolCount; i++) {


fprintf(file, "%s %04X\n", symbolTable[i].label, symbolTable[i].address);
}

fclose(file);
}

// Print symbol table


void printSymbolTable() {
for (int i = 0; i < symbolCount; i++) {
printf("%-10s %04X\n", symbolTable[i].label, symbolTable[i].address);
}
}

int main() {
// Read assembly program from a file
FILE *assemblyFile = fopen("program.sic", "r");
if (assemblyFile == NULL) {
printf("Error opening assembly file.\n");
return 1;
}

// Pass 1: Process the assembly program and generate the symbol table
char line[100];
int startingAddressSet = 0;

while (fgets(line, sizeof(line), assemblyFile)) {


char label[20] = "", opcode[20] = "", operand[20] = "";
int fields = sscanf(line, "%s %s %s", label, opcode, operand);

// Handle START directive to set initial location counter


if (!startingAddressSet && fields >= 2 && strcmp(label, "START") == 0) {
locationCounter = strtol(opcode, NULL, 16);
startingAddressSet = 1;
continue;
}

// Handle lines with labels


if (fields == 3 && !isdigit(label[0])) {
addSymbol(label, locationCounter);
}

// Update location counter based on opcode


if (strcmp(opcode, "RESW") == 0) {
locationCounter += 3 * atoi(operand);
} else if (strcmp(opcode, "BYTE") == 0) {
if (operand[0] == 'C') {
locationCounter += strlen(operand) - 3; // Account for characters
} else if (operand[0] == 'X') {
locationCounter += (strlen(operand) - 3) / 2; // Account for hex pairs
}
} else if (strcmp(opcode, "RESB") == 0) {
locationCounter += atoi(operand);
} else {
locationCounter += 3; // Standard 3-byte instructions
}
}

// Print the symbol table


printSymbolTable();

// Write the symbol table to a file


writeSymbolTableToFile("symbol_table.txt");

fclose(assemblyFile);

return 0;
}
INPUT:

OUTPUT:
user@user-Latitude-E5450:~/Desktop/S5/LAB/MM/sic$ gcc pass1.c
user@user-Latitude-E5450:~/Desktop/S5/LAB/MM/sic$ ./a.out
FIRST 1000
CLOOP 1003
ENDFIL 100C
EOF 1012
RETADR 1015
LENGTH 1018
BUFFER 101B
ZERO 201B

You might also like