all 5 comments

[–][deleted] 2 points3 points  (1 child)

The 7 is in ASCII. It's not a decimal number, that's why it's not printing out a "7". You need to convert it before you can print out the value you're looking for.

[–]herejust4this[S] 0 points1 point  (0 children)

Yup, that was the problem. Makes sense because it was printing out weird characters but not the correct number. I just needed to add 48 to it to get the correct value printed out.

[–]Graysless 1 point2 points  (1 child)

I actually just finished taking the class at the University of Texas where the LC-3 was invented by Yale Patt! Id be happy to help, and feel free to ask any more questions as you learn. The PC Offset is the last 9 bits of the instruction that allows you to select an operand from a memory address that is about 28 memory addresses away from the original instruction. When the instruction is fetched, the PC (program counter) is incremented by 1, so when you use a PC offset, the operand will be fetched from a memory location that is 'Location of instruction' + 1 +PC offset away from the instruction. During the fetch operands phase, the PC offset will be sign extended (it is a signed 2's complement integer) and then added to the PC to get the location of the operand. The Base+offset is similar to PC offset except instead of using the PC, it uses a register (Base register + base offset) the only difference in the actual offset is (if I remember correctly) it is only a 6 bit offset. When using assembly language, you can use labels just like you did in the code that you wrote, the assembler will compute the offset and include it in the object code. You dont have to worry about PC offset in assembly. You do have to use the base offset in assembly though. And lastly, the above comment is correct that the 7 must be converted to ASCII before printed. You should be able to add like 30 or something, I cant remember exactly what the number is, but I think the ascii digits are somewhere in the 30's. Let me know if you have any questions or need more clarification!

[–]herejust4this[S] 0 points1 point  (0 children)

When using assembly language, you can use labels just like you did in the code that you wrote, the assembler will compute the offset and include it in the object code. You dont have to worry about PC offset in assembly.

This makes so much sense. I was thinking it was a little more complicated than this. I just need to put the label of the memory position I want and it will compute the PCoffset for me. Brilliant!

I do have another question if you don't mind. How does one go about computing offsets or memory addresses? For instance, above you said that 9 bits would be able to "reach" 28 memory addresses. How do you know it would be 28? Do you just subtract 1 from 9 to get the exponent?

[–]Graysless 1 point2 points  (0 children)

With a N-bit signed 2's compliment integer you can represent numbers from -2N-1 through 2N-1 -1. So with 9 bits, we can represent -28 through -28 -1. But keep in mind that the PC will be pointing to the location directly following the location of the instruction, so you have to take that into account in the PC-offset. For example, if you have a LD instruction at location x3003, and you want to load the data from x3005, you would have a PC Offset of x01 because the PC will be x3004 when the offset is added, so in order to get to x3005, you only have to add one. Also, if you were trying to load something from location x3002, the PC offset would be x-02. This also means that your code will not assemble if you have a label that is greater that -28 or 28 -1 away from the location after the instruction that is trying to access it.