Question: //main.c #include #include #include phase2.h void breakup(char * in, char * p1, char * p2, char * p3) { int i = 0; int j
//main.c
#include
#include
#include "phase2.h"
void breakup(char * in, char * p1, char * p2, char * p3)
{
int i = 0;
int j = 0;
while (in[i] != ' ' && i < strlen(in) - 1)
{
p1[j] = in[i];
i++;
j++;
}
j = 0;
i++;
while (in[i] != ' ' && i < strlen(in) - 1)
{
p2[j] = in[i];
i++;
j++;
}
j = 0;
i++;
while (in[i] != ' ' && i < strlen(in) - 1)
{
p3[j] = in[i];
i++;
j++;
}
}
char * inFile;
int main()
{
char input[100] = "";
char part1[10] = "";
char part2[10] = "";
char part3[10] = "";
printf("%s ", "Welcome to the coding platform. Type \"help\" for assistance.");
do
{
printf("%s", ":: ");
fgets(input, 60, stdin);
breakup(input, part1, part2, part3);
if (strcmp(part1, "help") == 0)
{
printf("-------------------------- HELP COMMANDS: load
}
else if (strcmp(part1, "directory") == 0)
{
system("ls");
}
else if(strcmp(part1, "load") == 0)
{
if(strcmp(part2,"\0") == 0)
{
printf("LOAD takes in 1 parameter. ");
}
else
{
if( access( part2, F_OK ) != -1 )
{
inFile = part2;
printf("'%s' has been loaded. ", inFile);
}
else
{
printf("File does not exist. ");
}
}
}
else if(strcmp(part1, "assemble") == 0)
{
if(strcmp(part2,"\0") == 0)
{
printf("ASSEMBLE takes in 1 parameter. ");
}
else
{
if( access( part2, F_OK ) != -1 )
{
assemble(part2);
}
else
{
printf("File does not exist. ");
}
}
}
else if (strcmp(part1, "execute") == 0)
{
printf("Under Construction. ");
}
else if (strcmp(part1, "debug") == 0)
{
printf("Under Construction. ");
}
else if (strcmp(part1, "dump") == 0)
{
if(strcmp(part2, "start") == 0)
{
if (strcmp(part3, "end") == 0)
printf("Under Construction. ");
else
printf("Takes 2 parameters. ");
}
else
printf("Takes 2 parameters. ");
}
else if (strcmp("exit", part1) != 0)
{
printf("Invalid command. ");
}
if (strcmp(part1, "exit") != 0)
{
memset(input, 0, 10);
memset(part1, 0, 10);
memset(part2, 0, 10);
memset(part3, 0, 10);
}
}
while (strcmp(part1, "exit") != 0);
printf("%s ", "Exited...");
}
-------------------------------------------------------------------------------------- // Phase 2.h
#ifndef phase2_h
#define phase2_h
#include
#include
void read_line(void);
void assemble(char *inFile);
struct optab{
char code[10],objcode[10];
}
myoptab[25] = {
{"ADD", "18"},
{"AND", "58"},
{"COMP", "28"},
{"DIV", "24"},
{"J", "3C"},
{"JEQ", "30"},
{"JGT", "34"},
{"JLT", "38"},
{"JSUB", "48"},
{"LDA", "00"},
{"LDCH", "50"},
{"LDL", "08"},
{"LDX", "20"},
{"MUL", "20"},
{"OR", "44"},
{"RD", "D8"},
{"RSUB", "4C"},
{"STA", "0C"},
{"STCH", "54"},
{"STL", "14"},
{"STX", "10"},
{"SUB", "1C"},
{"TD","E0"},
{"TIX", "2C"},
{"WD", "DC"},
};
struct SYMTAB
{
char symbol[15];
int addr;
}
mysymtab[20];
int startaddr;
int locctr;
int symcount=0;
int length;
char line[20],label[8],opcode[8],operand[8],programname[10];
int dupsym=0;
int nostart=0;
int noend=0;
int manybyte=0;
int noop=0;
int validop=0;
int memover=0;
void pass_one(char *inFile)
{
int sym_there = 0;
int sym_tracker = 0;
int op_there = 0;
FILE *input,*inter;
input=fopen(inFile,"r");
inter=fopen("inter.txt","w");
fgets(line,20,input);
read_line();
if (strcmp(opcode, "START") == 0)
{
startaddr=atoi(operand);
locctr=startaddr;
strcpy(programname,label);
fprintf(inter,"%d %s",locctr, line);
fgets(line,20,input);
read_line();
}
else
{
programname[0]='\0';
startaddr=0;
locctr=0;
nostart = 1;
}
while (strcmp(opcode,"END") != 0 )
{
if (strcmp(&line[0],".") != 0)
{
if (strcmp(label,"/0") != 0)
{
if (strcmp(mysymtab[0].symbol, "\0") == 0)
{
//DO NOTHING
}
if (strcmp(mysymtab[0].symbol, label) == 0)
{
sym_there = 1;
}
else
{
for (int i = 0; strcmp(mysymtab[i].symbol, "\0") != 0; i++)
{
if (strcmp(label,mysymtab[i+1].symbol) == 0)
{
sym_there = 1;
break;
}
}
}
if (sym_there == 1)
{
dupsym = 1;
sym_there = 0;
}
else
{
for (int i = 0; i < 20; i++)
{
if (strcmp(mysymtab[i].symbol, "\0") != 0)
{
sym_tracker++;
}
}
memcpy(mysymtab[sym_tracker].symbol, label, 10);
mysymtab[sym_tracker].addr = locctr;
sym_tracker = 0;
}
for (int i = 1; i < 25; i++)
{
if (strcmp(myoptab[i].code, opcode) == 0)
{
op_there = 1;
break;
}
}
if(op_there == 1)
{
locctr += 3;
op_there = 0;
}
else if (strcmp(opcode, "WORD") == 0)
{
locctr += 3;
}
else if (strcmp(opcode, "RESW") == 0)
{
locctr += atoi(operand)*3;
}
else if (strcmp(opcode, "RESB") == 0)
{
locctr += atoi(operand);
}
else if (strcmp(opcode, "BYTE") == 0)
{
int j = 0;
for (int i = 2; strcmp(&operand[i], "\0") != 0; i++)
{
if (strcmp(&operand[i], "\'") != 0)
{
j++;
}
}
locctr += j;
}
else
{
noop = 1;
}
}
}
fprintf(inter, "%s %d ", line, locctr);
fgets(line,20,input);
read_line();
}
fprintf(inter, "%s %d ", line, locctr);
length = locctr - startaddr;
fprintf(inter," Symbol Table ");
fprintf(inter,"_____________________________________ ");
for (int i = 0; i < 10; i++)
{
if (strcmp(mysymtab[i].symbol, "\0") != 0)
fprintf(inter, "%s %d", mysymtab[i].symbol, mysymtab[i].addr);
fprintf(inter, " ");
}
/*
fprintf(inter, " Op Table ");
fprintf(inter, "_____________________________________ ");
for (int i = 0; strcmp(myoptab[i].code,"\0") != 0; i++)
{
fprintf(inter, "%s", myoptab[i].code);
fprintf(inter, " ");
}
*/
fclose(inter);
fclose(input);
}
void read_line()
{
char buff[8],word1[8],word2[8],word3[8];
int i,j=0,count=0;
label[0]=opcode[0]=operand[0]=word1[0]=word2[0]=word3[0]='\0';
for(i=0;line[i]!='\0';i++)
{
if(line[i]!=' ')
buff[j++]=line[i];
else
{
buff[j]='\0';
strcpy(word3,word2);
strcpy(word2,word1);
strcpy(word1,buff);
j=0;
count++;
}
}
buff[j-1]='\0';
strcpy(word3,word2);
strcpy(word2,word1);
strcpy(word1,buff);
switch(count)
{
case 0:
strcpy(opcode,word1);
break;
case 1:
{
strcpy(opcode,word2);
strcpy(operand,word1);
}
break;
case 2:{
strcpy(label,word3);
strcpy(opcode,word2);
strcpy(operand,word1);
}
break;
}
}
void assemble(char *inFile)
{
pass_one(inFile);
}
#endif /* phase2_h */
BUGS:
Infinite loop when there is no END
For some reason, the FIRST is read by the program and counted but its treated like a comment as well.
Registers address need to change to HEX. Help from an experienced coder prefered. Will rate thank you.
I'm following the following algorithm: Assembler Pass 1: begin read first input line if OPCODE ='START' then begin save #[OPERAND] as starting address initialize LOCCTR to starting address write line to intermediate file read next input line end {if START} else initialize LOCCTR to 0 while OCODE != 'END' do begin if this is not a comment line then begin if there is a symbol in the LABEL field then begin search SYMTAB for LABEL if found then set error flag (duplicate symbol) else insert (LABEL,LOCCTR) into SYMTAB end {if symbol} search OPTAB for OPCODE if found then add 3 {instruction length} to LOCCTR else if OPCODE='WORD' then add 3 to LOCCTR else if OPCODE = 'RESW' then add 3 * #[OPERAND] to LOCCTR else if OPCODE = 'RESB' then add #[OPERAND] to LOCCTR else if OPCODE = 'BYTE' then begin find length of constant in bytes add length to LOCCTR end {if BYTE} else set error flag (invalid operation code) end {if not a comment} write line to intermediate file read next input line end {while not END} write last line to intermediate file save (LOCCTR starting address) as program length end {Pass 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
