1

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

Community
  • 1
  • 1
  • Look at your reference: it's the `imm5` bits you are interested in. (Presumably, one of its bits is the sign bit; probably the leftmost -- highest --, as usual.) – Jongware Oct 20 '14 at 00:11
  • @Jongware Sorry I tired I few things and am still having difficulty Could you explain what I need to do in more detail – Bilbo Baggins Oct 20 '14 at 00:42
  • In answer to your question, `two's compliment` for a number is arrived at by negating the number `~number` and adding `1`. – David C. Rankin Oct 20 '14 at 01:34

1 Answers1

4

The problem is use of logical NOT in place of bitwise NOT.

Try this instead:

sr2 = (0x10 & instruction) ? ((~(instruction & 0x1F)) + 1) : (instruction & 0x1F);

I'm taking advantage of a property of two's complement: if the most-significant bit is set, the number is negative, and we can make it positive by flipping all the bits (~, bitwise NOT), and adding 1.

It looked like you tried to do this, but you need to be aware that ! (logical NOT) is going to likely return a 1 (if val == 0) or a 0 (if val != 0).

example illustrating the difference:

uint8_t value = 0xAA;
uint8_t logical_not = !value;
uint8_t bitwise_not = ~value;

printf("initial value: 0x%02X, logical not: 0x%02X, bitwise not: 0x%02X\n", value, logical_not, bitwise_not);

What you expect to see is:

initial value: 0xAA, logical not: 0x00, bitwise not: 0x55

Adding everything you need to verify this for yourself:

Test program:

#include <stdio.h>

void main(void)
{
    uint8_t value = 0xAA;   // Test value: 0b10101010
    uint8_t lnot = !value;  // logical not of test value
    uint8_t bnot = ~value;  // bitwise not of test value

    printf("v: 0x%02X, L: 0x%02X, B: 0x%02X\n", value, lnot, bnot);
}

Program output:

$ gcc test.c
$ ./a.exe
v: 0xAA, L: 0x00, B: 0x55
$
  • Thanks i'm new to C and was not aware of the ~ bitwise NOT, I figured how to make it work. – Bilbo Baggins Oct 20 '14 at 01:33
  • 1
    glad it was helpful. If this resolves your issue please mark the answer so that others can benefit, or feel free to ask for further clarification if your program still isn't working. –  Oct 20 '14 at 01:35