so I am in the process of creating a lc-3 compiler that is written in C and I am stuck on how to convert the BR command into C language, the assignment asks that i get a file and then spit it out in zeros and ones , but with the BR command there are different types of it an example BRn, BRz etc.. and I am not sure on how to write this portion of the assignment, any help would be grateful. Thanks in advance

Compiler.. In C... Spew out 1s and 0s? You mean disassembler in C?

Compiler.. In C... Spew out 1s and 0s? You mean disassembler in C?

yes, sorry thats what I was meaning to say , the output should be 0's and 1's

So actually it is an assembler. How about for of addressing. Labels? Give example code and what you do now with it and were you having difficulties? So you do not need to actually do simulator?

so BR has three formats BRn Brz Brp and the encoding for Br goes like this the first 15-12 bits are 0000 11-9 are the n, z, p and 8-0 are the Pcoffset9 the total is 16 bits. so Br will branch down code making it difficult to translate this to binary when I have a file that I am trying to convert line by line, and BR dosent allow me to do this. and example of Br is

TEST ADD R4,R1,# -4;test 
         BRz   output      ; if done prepare the output


         NOT R1,R1
         ADD R1,R1,R0


OUTPUT LD R0,AscII
              ADD

so this just random code just to example on how BR would jump down.
sorry if this is confusing but thanks for replying to my post

> when I have a file that I am trying to convert line by line

You need two passes. At pass 1, when a forward jump is encountered, create a relocation record of branch location and a target symbol. Also during the first pass build a symbol table (a map of labels to addresses). In the second pass, go over the relocation records and resolve them.

There are other techniques, but all of them require two passes.

PS: please use punctuation and capitalization. Your posts are almost impossible to read.

> when I have a file that I am trying to convert line by line

You need two passes. At pass 1, when a forward jump is encountered, create a relocation record of branch location and a target symbol. Also during the first pass build a symbol table (a map of labels to addresses). In the second pass, go over the relocation records and resolve them.

There are other techniques, but all of them require two passes.

PS: please use punctuation and capitalization. Your posts are almost impossible to read.

Instead of going line by line, it instead goes word by word. If the current word is ";", then the program will move to the next line reading the next word. And the ADD, AND, .ORIG, and BR functions work as well.

but I am not sure if the second pass is working properly and also I am confused on how to code the LD LDI and LDR commands of the LC-3.
Below is my code there might be some tests within them that are commented out. also this is just part of my code

unsigned short int brFunction(FILE *infile, char *fileword, int counter, int n, int z, int p)
{
short int offset = -1;
unsigned short int operation = 0;
fscanf(infile, "%s", fileword);
int a = 0;
while (symbolTable[a].name != NULL)
{
if (strcmp(symbolTable[a].name, fileword) == 0)
{
offset = symbolTable[a].address;
break;
}
a++;
}
if (offset == -1)
{
//ERROR
}
offset = offset - (counter + 1);
operation += offset;
if (offset < 0)
{
operation += (1 << 9);
}
operation += (n << 11) + (z << 10) + (p << 9);
return operation;
}

short int jmpFunction(char *fileline, FILE *infile)
{

return 0;
}


int checkOperand(FILE *infile, int counter, char *fileword)
{
short int operation;

if (strcmp(fileword, ".ORIG") == 0)
{
operation = origFunction(infile, fileword);

}else if (strcmp(fileword, "ADD") == 0)
{
operation = addFunction(infile, fileword);
//printf("%d: %d\n", counter, operation);
counter++;

}else if (strcmp(fileword, "AND") == 0)
{
operation = andFunction(infile, fileword);
//printf("%d: %d\n", counter, operation);
counter++;

}else if (strcmp(fileword, "BR") == 0)
{
operation = brFunction(infile, fileword, counter, 1, 1, 1);
counter++;

}else if (strcmp(fileword, "BRn") == 0)
{
operation = brFunction(infile, fileword, counter, 1, 0, 0);
counter++;

}else if (strcmp(fileword, "BRz") == 0)
{
operation = brFunction(infile, fileword, counter, 0, 1, 0);
counter++;

}else if (strcmp(fileword, "BRp") == 0)
{
operation = brFunction(infile, fileword, counter, 0, 0, 1);
counter++;

}else if (strcmp(fileword, "BRnz") == 0)
{
operation = brFunction(infile, fileword, counter, 1, 1, 0);
counter++;

}else if (strcmp(fileword, "BRnp") == 0)
{
operation = brFunction(infile, fileword, counter, 1, 0, 1);
counter++;

}else if (strcmp(fileword, "BRzp") == 0)
{
operation = brFunction(infile, fileword, counter, 0, 1, 1);
counter++;

}else if (strcmp(fileword, "BRnzp") == 0)
{
operation = brFunction(infile, fileword, counter, 1, 1, 1);
counter++;

}else if (strcmp(fileword, "JMP") == 0)
{
//operation = jmpFunction(infile, fileword, counter);
counter++;

}else if (strcmp(fileword, "RET") == 0)
{
operation = 1100000111 << 6;
counter++;

}else if (strcmp(fileword, "JSR") == 0)
{
counter++;

}else if (strcmp(fileword, "JSRR") == 0)
{
counter++;

}else if (strcmp(fileword, "LD") == 0)
{
counter++;

}else if (strcmp(fileword, "LDI") == 0)
{
counter++;

}else if (strcmp(fileword, "LDR") == 0)
{
counter++;

}else if (strcmp(fileword, "LEA") == 0)
{
counter++;

}else if (strcmp(fileword, "NOT") == 0)
{
counter++;

}else if (strcmp(fileword, "RET") == 0)
{
counter++;

}else if (strcmp(fileword, "RTI") == 0)
{
counter++;

}else if (strcmp(fileword, "ST") == 0)
{
counter++;

}else if (strcmp(fileword, "STI") == 0)
{
counter++;

}else if (strcmp(fileword, "STR") == 0)
{
counter++;

}else if (strcmp(fileword, "MUL") == 0)
{
counter++;

}else if (strcmp(fileword, "PUSH") == 0)
{
counter++;

}else if (strcmp(fileword, "POP") == 0)
{
counter++;

}else if (strcmp(fileword, "NEG") == 0)
{
counter++;

}else if (strcmp(fileword, "TRAP") == 0)
{
counter++;

}else if (strcmp(fileword, "OUT") == 0)
{
counter++;

}else if (strcmp(fileword, "HALT") == 0)
{
counter++;

}else if (strcmp(fileword, "GETC") == 0)
{
counter++;

}else if (strcmp(fileword, "IN") == 0)
{
counter++;

}else if (strcmp(fileword, "PUTSP") == 0)
{
counter++;

}else if (strcmp(fileword, ".FILL") == 0)
{
counter++;

}else if (strcmp(fileword, ".BLKW") == 0)
{
counter++;

}else if (strcmp(fileword, ".STRINGZ") == 0)
{
counter++;

}else if (strcmp(fileword, ".END") == 0)
{
counter++;

}else
{
int i;
for (i = 0; i < tableLength; i++)
{
if (strcmp(fileword, symbolTable[i].name) == 0)
{
fscanf(infile, "%s", fileword);
counter = checkOperand(infile, counter, fileword);
}
}
//printf("Line: %d Misnamed Label %s", counter + origin, fileword);
//ERROR//
}
return counter;
}

Why you do not have opword array, you are doing zillion identical counter increments (ie NOP), and you are ignoring all errors in file?

Why you do not have opword array, you are doing zillion identical counter increments (ie NOP), and you are ignoring all errors in file?

this is just a part of my code. And that could be why there are errors. And I am not sure what you mean by opword array? Could you explain please

Might be interesting to check the Python language code snippets for inspiration.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.