justinmeiners/lc3-vm

Question about using uint16

markus-zhang opened this issue · 2 comments

Hi experts,

I'm reading the tutorial and wondering why you use unsigned 16-bit integer for PC offset?
For example, in the example code below, what happens if oc_offset should be a negative (thus 1 extended) number? I think the sign gets extended but still oc_offset is intepreted as a positive nunmber as it's an uint16_t. Where is my misunderstanding? Thanks!

{
    uint16_t pc_offset = sign_extend(instr & 0x1FF, 9);
    uint16_t cond_flag = (instr >> 9) & 0x7;
    if (cond_flag & reg[R_COND])
    {
        reg[R_PC] += pc_offset;
    }
}

Whether a number is negative or positive is just a convention of how you read the bits.
We use unsigned so that all the bit manipulation operations are clear.

Someone recently asked a similar question and you can read more here: #37

If that does not answer it for you, let me know and ill provide more detail.

Hi thanks a lot, I got the idea!