0% found this document useful (0 votes)
8 views2 pages

Twopass

The document is a C program that simulates the assembly process for a simple instruction set, including the creation of a symbol table and object code generation. It processes instructions from a simulated input, handling 'START', 'END', and various opcodes while generating headers and records for the object file. The program outputs the total length of the program and the corresponding object code for valid instructions.

Uploaded by

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

Twopass

The document is a C program that simulates the assembly process for a simple instruction set, including the creation of a symbol table and object code generation. It processes instructions from a simulated input, handling 'START', 'END', and various opcodes while generating headers and records for the object file. The program outputs the total length of the program and the corresponding object code for valid instructions.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

struct symboltable {
char symbol[10];
int addr;
} s[10];

void main() {
int flag = 0, i, code = 0, addr;
int count = 0, tlen = 0, locctr;
char st[5], a[5], object[6];
char length[6], add[5], symbol[20], operand[20], op[20], label[20];
char mnemonic[10][10] = {"LDA", "STA", "ADD", "LDB", "STCH", "LDCH", "LDB"};
char opcode[10][10] = {"03", "14", "16", "69", "24", "13", "32"};

// Simulating the content of "symtab.txt"


// symbol address
strcpy(s[0].symbol, "X");
s[0].addr = 0x1000;
strcpy(s[1].symbol, "Y");
s[1].addr = 0x2000;
count = 2; // Assuming we have 2 symbols

// Simulating the content of "inner.txt"


// address label opcode operand
char instructions[5][4][20] = {
{"1000", "START", "LDA", "X"},
{"1003", "LOOP", "ADD", "Y"},
{"1006", "END", "", ""},
{"", "", "", ""}
};

// Start processing the "START" instruction


strcpy(label, instructions[0][1]);
strcpy(op, instructions[0][2]);
strcpy(operand, instructions[0][3]);

if (strcmp(op, "START") == 0) {
fprintf(stdout, "H^%s^00^%s\n", label, instructions[0][0]);
strcpy(st, instructions[0][0]);
}

// Calculate program length (using a simple example)


tlen = 3; // Just for this example

// Program length handling


printf("Total Length: %d\n", tlen);

// Write the T record (simulating the opcode.txt output)


fprintf(stdout, "T^00%s^%2X\n", instructions[0][0], tlen);

// Processing instructions
for (i = 1; i < 2; i++) {
strcpy(label, instructions[i][1]);
strcpy(op, instructions[i][2]);
strcpy(operand, instructions[i][3]);
flag = 0;
strcpy(object, "");

// Check for valid mnemonic and get corresponding opcode


for (int j = 0; j < 7; j++) {
if (strcmp(mnemonic[j], op) == 0) {
flag = 1;
strcpy(object, opcode[j]);
break;
}
}

// Look up the address from the symbol table


for (int j = 0; j < count; j++) {
if (strcmp(s[j].symbol, operand) == 0) {
code = s[j].addr;
break;
}
}

// Handle literal WORD or BYTE values (not included in this simple example)
if ((strcmp(op, "WORD") == 0) || (strcmp(op, "BYTE") == 0)) {
code = atoi(operand);
flag = 1;
}

// Write the object code if valid


if (flag == 1) {
printf("^%s%x", object, code);
}
}

// Write E record
fprintf(stdout, "\nE^00%s", st);
}

You might also like