0% found this document useful (0 votes)
11 views4 pages

Pass 2

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)
11 views4 pages

Pass 2

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/ 4

PASS 2

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE_LEN 50
#define MAX_SYMBOLS 20

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

Symbol symbolTable[MAX_SYMBOLS];
int symbolCount = 0;

// Opcode table with format "opcode -> machine code"


typedef struct {
char opcode[10];
char code[3];
} Opcode;

Opcode opcodeTable[] = {
{"LDA", "00"}, {"STA", "0C"}, {"STL", "14"},
{"COMP", "28"}, {"JEQ", "30"}, {"J", "3C"},
{"JSUB", "48"}
};
int opcodeCount = 7;

// Function to load symbol table from file


void loadSymbolTable() {
FILE *file = fopen("symbol_table.txt", "r");
if (!file) {
printf("Error: Could not open symbol table file.\n");
exit(1);
}

while (fscanf(file, "%s %x", symbolTable[symbolCount].label,


&symbolTable[symbolCount].address) != EOF) {
symbolCount++;
}

fclose(file);
}

// Function to lookup symbol in symbol table


int getAddress(char *label) {
for (int i = 0; i < symbolCount; i++) {
if (strcmp(symbolTable[i].label, label) == 0) {
return symbolTable[i].address;
}
}
return -1;
}

// Function to lookup opcode in opcode table


char *getOpcode(char *mnemonic) {
for (int i = 0; i < opcodeCount; i++) {
if (strcmp(opcodeTable[i].opcode, mnemonic) == 0) {
return opcodeTable[i].code;
}
}
return NULL;
}

// Function to generate object code for an instruction


void generateObjectCode(char *opcode, char *operand, char *objCode) {
char *opCode = getOpcode(opcode);
if (opCode) {
int address = getAddress(operand);
if (address != -1) {
sprintf(objCode, "%s%04X", opCode, address);
} else {
sprintf(objCode, "%s0000", opCode);
}
} else if (strcmp(opcode, "BYTE") == 0) {
if (operand[0] == 'C') {
int i;
for (i = 2; operand[i] != '\''; i++) {
sprintf(&objCode[(i - 2) * 2], "%02X", operand[i]);
}
}
} else if (strcmp(opcode, "WORD") == 0) {
sprintf(objCode, "%06X", atoi(operand));
} else {
strcpy(objCode, "");
}
}

int main() {
FILE *intermediateFile = fopen("program.sic", "r");
FILE *objectFile = fopen("object.txt", "w");

if (!intermediateFile || !objectFile) {
printf("Error: Could not open one of the files.\n");
return 1;
}

// Load symbol table from file


loadSymbolTable();

// Variables for object file header and text records


char programName[20];
int startAddress = 0;
int programLength = 0x1C; // Example program length

// Read first line for header record


fscanf(intermediateFile, "%s %*s %x", programName, &startAddress);
fprintf(objectFile, "H%-6s%06X%06X\n", programName, startAddress,
programLength);

// Text record setup


int textStart = startAddress;
char textRecord[70] = "";
int textLength = 0;

// Process each line in the intermediate file


char line[MAX_LINE_LEN];
while (fgets(line, sizeof(line), intermediateFile)) {
int address;
char label[20], opcode[10], operand[20];
char objCode[10] = "";

sscanf(line, "%x %s %s %s", &address, label, opcode, operand);

// Generate object code for the instruction


generateObjectCode(opcode, operand, objCode);

if (strlen(objCode) > 0) {
if (textLength + strlen(objCode) / 2 > 30) {
fprintf(objectFile, "T%06X%02X%s\n", textStart, textLength, textRecord);
textStart = address;
strcpy(textRecord, objCode);
textLength = strlen(objCode) / 2;
} else {
strcat(textRecord, objCode);
textLength += strlen(objCode) / 2;
}
}
}

// Write last text record


if (textLength > 0) {
fprintf(objectFile, "T%06X%02X%s\n", textStart, textLength, textRecord);
}

// End record
fprintf(objectFile, "E%06X\n", startAddress);

fclose(intermediateFile);
fclose(objectFile);

printf("Object file generated successfully.\n");

return 0;
}
INPUT:

OUTPUT:
user@user-Latitude-E5450:~/Desktop/S5/LAB/MM/sic$ gcc pass2.c
user@user-Latitude-E5450:~/Desktop/S5/LAB/MM/sic$ ./a.out
Object file generated successfully.

You might also like