Hey everyone,
I am writing a basic program titled, "LMC.c" that takes the contents from a file "LMC.s", and outputs them into another file "LMC.o"
Here is the input file, LMC.s
Code:
INP 00
STO 90
INP 00
ADD 90
OUT 00
STOP 00
Then here is the program that reads the input file, LMC.c
Code:
#include<stdio.h>
#include<string.h>
int main()
{
FILE *input,*output;
char opcode[5],address[5];
char binopcode[10], binaddress[10];
int i;
/* the following codes open the file for reading and the file for output */
input = fopen("LMC.s","r");
output = fopen("LMC.o","w");
while(fscanf(input,"%s %s",opcode,address)!=EOF)
{
printf("Opcode: %s \t\tAddress: %s\n",opcode,address);
if(strcmp(opcode, "INP") == 0)
{
strcpy(binopcode,"10000000");
strcpy(binaddress," ");
}
else if(strcmp(opcode,"STO") == 0)
{
strcpy(binopcode,"00000011");
strcpy(binaddress,"01011010");
}
else if(strcmp(opcode,"ADD") == 0)
{
strcpy(binopcode,"00000001");
strcpy(binaddress,"01011010");
}
else if(strcmp(opcode,"OUT") == 0)
{
strcpy(binopcode,"01000000");
strcpy(binaddress,"");
}
else if(strcmp(opcode,"STOP") == 0)
{
strcpy(binopcode,"00000000");
strcpy(binaddress,"");
}
/* now put the results to the output file */
fprintf(output,"%s %s\n",binopcode,binaddress);
printf( "%s %s\n",binopcode,binaddress);
} /*end while*/
fclose(output);
fclose(input);
return 0;
}
I cannot get it to output the file into "LMC.o" though. I keep getting a "Bus error (core dumped)" when I compile it. Any suggestions why this might be?
Thanks,
MATT