INPUT: Assembly code
Pass 1 Code: to generate symbol table and IR file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINES 100
#define MAX_LABELS 50
#define MAX_LINE_LENGTH 100
typedef struct {
char label[50];
int address;
} Symbol;
Symbol symbolTable[MAX_LABELS];
int symbolCount = 0;
typedef struct {
int address;
char instruction[MAX_LINE_LENGTH];
} IR;
IR irTable[MAX_LINES];
int irCount = 0;
int currentAddress = 0;
void addSymbol(const char *label, int address) {
strcpy(symbolTable[symbolCount].label, label);
symbolTable[symbolCount].address = address;
symbolCount++;
}
int isLabel(const char *line) {
int len = strlen(line);
if (len > 1 && line[len - 1] == ':') {
return 1;
}
return 0;
}
int isThumbInstruction(const char *instruction) {
// Check for "Thumb" instructions by looking for specific patterns or keywords
return (strstr(instruction, "MOVS") || strstr(instruction, "ADDS") || strstr(instruction, "SUBS") ||
strstr(instruction, "CMP") || strstr(instruction, "BEQ") || strstr(instruction, "B") ||
strstr(instruction, "ANDS") || strstr(instruction, "ORRS") || strstr(instruction, "EORS"));
}
int isIgnoredLine(const char *line) {
// Skip lines containing specific keywords
return (strstr(line, "AREA") || strstr(line, "ENTRY") || strstr(line, "CODE") || strstr(line, "READONLY") || strstr(line, "END"));
}
void processLine(const char *line) {
char trimmedLine[MAX_LINE_LENGTH];
strcpy(trimmedLine, line);
// Remove comments
char *comment = strchr(trimmedLine, ';');
if (comment) {
*comment = '\0';
}
// Trim leading and trailing whitespace
char *start = trimmedLine;
while (*start == ' ' || *start == '\t') start++;
char *end = trimmedLine + strlen(trimmedLine) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) *end-- = '\0';
// Skip empty lines or ignored lines
if (*start == '\0' || isIgnoredLine(start)) return;
// Check if line is a label
if (isLabel(start)) {
char label[50];
strncpy(label, start, strlen(start) - 1);
label[strlen(start) - 1] = '\0';
addSymbol(label, currentAddress);
return;
}
// Add to IR table
irTable[irCount].address = currentAddress;
strcpy(irTable[irCount].instruction, start);
irCount++;
// Increment address based on instruction type
if (isThumbInstruction(start)) {
currentAddress += 2; // Thumb instructions are 2 bytes
} else {
currentAddress += 4; // Non-Thumb instructions are 4 bytes
}
}
void printSymbolTable() {
printf("Symbol Table:\n");
for (int i = 0; i < symbolCount; i++) {
printf("%06X\t%s\n", symbolTable[i].address, symbolTable[i].label);
}
}
void printIRTable() {
printf("\nIntermediate Representation (IR):\n");
for (int i = 0; i < irCount; i++) {
printf("%06X\t%s\n", irTable[i].address, irTable[i].instruction);
}
}
int main() {
FILE *file = fopen("input.txt", "r");
if (!file) {
printf("Error: Could not open file.\n");
return 1;
}
char line[MAX_LINE_LENGTH];
currentAddress = 0; // Start address from 000000
while (fgets(line, sizeof(line), file)) {
processLine(line);
}
fclose(file);
printSymbolTable();
printIRTable();
return 0;
}
Output: Symbol table and IR file