Hi,
I am having trouble with a program I am working on. The program is a disassembler that reads through a hex file and converts it to assembly code. I am using a while loop to loop through the file but it is terminating randomly in the file before it reaches the end.
Here is my code:
Code:
void printAssembly(char fileName[])
{
int inst = 0;
int pc = 0;
FILE *f;
char line[2048];
f = fopen(fileName, "r");
fscanf(f, "%x", &pc);
// while((read = getline(&line, &buf, f)) != -1)
// while((fscanf(f, "%x", &inst) != EOF))
while(fgets(line, sizeof(line), f))
{
// if(index == 0)
// {
// pc = inst;
// index++;
// }
// else
// {
fscanf(f, "%x", &inst);
pc ++;
if(inst >> 12 == 1)
{
printAdd(inst);
}
if(inst >> 12 == 5)
{
printAnd(inst);
}
if(inst >> 12 == 0)
{
printBr(inst, pc);
}
}
}
The while loop is terminating when it reaches an instruction that starts with with a 'zero'. I am wondering if this could be the issue or if this is completely unrelated.This happens when it reaches the Branch (printBr). The opcode for branch on the LC3 begins with x0. The commented lines are other things I have tried (the variables used for these have been removed but I did have them when I attempted it.)
I also attempted while(fgets(line, sizeof(line), f) != NULL) and I received the exact same result.
Please help, I am stuck! Thank you in advance for your responses!