lucifer
lucifer

Reputation: 11

Need help in interpreting lines 1,3 and 7 of the machine language instruction to LC3

enter image description here

Here is what i have done so far

.ORIG x30F6
LEA R1, 
ADD R2, R1, #14
ST R2, 
AND R2, R2, #0
ADD R2, R2, #5
STR R2, R1, #14
LDI R3, 
.END

I was hoping that someone could finish one of the three lines mentioned in the question and show the steps taken to get the binary instruction translated to LC3. My college professor for the online course i am taking has only gone over ADD, AND, and NOT.I tried using the principles for the other opcodes in this assignment but cant get the code to assemble. It is only the three lines mentioned in the question that are giving me trouble. He uploaded this assignment, but has not gone over the other opcodes. And won't reply to my emails.

Upvotes: 0

Views: 173

Answers (1)

Erik Eidt
Erik Eidt

Reputation: 26656

The instructions you're referring to all take a pc-relative immediate as their final assembly operand.

An LC3 instruction set manual defines LEA, for example, in Appendix A, on Page 534 as follows:

+--------+------+------------------+
|   op   |  dr  |    PCOffset9     |
+--------+------+------------------+

DR = PC + 1 + SEXT(PCoffset9);

So, for the first one, you should be able to see that the immediate has value

111111101

binary, though we need to point out that this PCOffset9 is a signed field — a signed field whose sign bit is set, meaning it is negative.  Therefore, the decimal value of this immediate is -3 — and by the .ORIG, PC is 0x30F6 for that instruction, so PC + 1 is x30F7, and x30F7 - 3 is x30F4, which matches up with the comments.

How to code that value in assembly depends somewhat on what the assembler will take.

In normal LC-3 assembly usage, you would put a label name for this operand instead of a constant, and the assembler will figure out the immediate value to put in that immediate field (by subtracting PC-1 of the instruction from the address of the label name, both of which are assemble-time constants).

But in this case, a simple constant such as #-3 should work fine (unless your assembler doesn't take it).


.ORIG x30F6
LEA R1, #-3
ADD R2, R1, #14
.END

Works with this simulator: https://wchargin.com/lc3web/


For your simulator, you would put a named label at x30f4, and use that label name in the LEA. Sadly x30f4 is before x30f6, so you'd have to back up the .ORIG by 2 values, and if you do that your simulator will assume that this memory, which is used as data, is the first line of code... So, this is what I might suggest:

.ORIG x30F3
ENTRY BR START
DATA1 .FILL #0
DATA2 .FILL #0
START LEA R1, DATA1
.END

This says that (1) the entry point is at x30f3, but all it does is skip around the (2) data positioned at x30f4 & x30f5, so goes (via unconditional branch) to (3) location x30f6.  The real first line then at x30f6 does the LEA using label for DATA1, and you can see that the instruction's hex value is xE3FD as in your code table, and, it disassembles as LEA R1, DATA1.

The amount of work to get there using this not-so-capable simulator is well beyond what should be taught in an introductory assembly course, so I'll apologize on behalf of your coursework and instructors.

Using a better simulator would address this, so you could simply say LEA R1, #-3.

As an alternative if sticking with that limited simulator, a simple rearrangement choosing to place the data after the code instead of before would have made this quite a bit simpler and much more direct:

.ORIG x30F6
START LEA R1, DATA1
; ...
DATA1 .FILL #0
.END

Upvotes: 1

Related Questions