In continuation from my previous question Having trouble reading strings from a file in C and manipluating them as an lc3 disassmbler
I need to now implement immediate addressing mode in an lc3 disassembler using ADD & AND
For example if a file contained:
1283
5105
1df7
506f
I would like to print out:
add r1,r2,r3
and r0,r4,r5
add r6,r7,-9
and r0,r1,15
How would I be able to print out the -9 and 15 I understand I need to convert it twos-complement but am not sure how.
Here is my code for If statement referring to the ADD instruction ie. line 3 and 1 of output
while (fscanf(file, "%s", hexString) != EOF){
long int instruction = strtol(hexString, NULL, 16);
if (instruction >> 12 == 0b0001){ //op code is ADD
if ((instruction >> 5) & 0b001){ //is using “immediate” addressing mode
dr = (instruction >> 9) & 0b111;
sr1 = (instruction >> 6) & 0b111;
sr2 = (!instruction) & 0b11111 + 1; // this needs to convert to twos complement
printf("and r%d,r%d,%d \n", dr, sr1,sr2);
} else {
dr = (instruction >> 9) & 0b111; // turns other bits to zero
sr1 = (instruction >> 6) & 0b111;
sr2 = (instruction) & 0b111;
printf("add r%d,r%d,r%d \n", dr, sr1, sr2);
}
} else if ....
Heres a copy of lc3 instruction set for reference http://ece224web.groups.et.byu.net/reference/LC3_Instructions.gif