问题
I'm having trouble getting this subroutine I'm writing to work. Basically, I am trying to have a subroutine where I would set R0
to a value in some array (R3)
with an offset of R1
.
R0_ORIGINAL .FILL 0
R1_ORIGINAL .FILL 0
R2_ORIGINAL .FILL 0
R3_ORIGINAL .FILL 0
LOAD
ST R0, R0_ORIGINAL
ST R1, R1_ORIGINAL
ST R2, R2_ORIGINAL
ST R3, R3_ORIGINAL
AND R0, R0, #0
ADD R0, R0, R2
BRz SKIP
AND R3, R3, #0
LD R3, FIFTY
ADD R1, R1, R3
SKIP AND R3, R3, #0
LEA R3, CIPHER_ARRAY
STR R0, R3, R1
LD R0, R0_ORIGINAL
LD R1, R1_ORIGINAL
LD R2, R2_ORIGINAL
LD R3, R3_ORIGINAL
RET
CIPHER_ARRAY .BLKW #100
FIFTY .FILL #50
This is the line I'm trying to get to work :
STR R0, R3, R1
It gives me the error :
Expected 6 bit signed number, but found 'R1' instead.
Any help would be appreciated!
回答1:
Your usage of STR appears a bit off. It should be used like this:
STR R0, R1, #4
To store the CONTENTS of register R0 at the LOCATION specified by (R1+decimal 4). So get the value of R1 (hopefully an address), add 4 to produce and final address. THAT'S where you will store the contents of R0.
Since you have:
STR R0, R3, R1
The assembler is pointing that out "R1" is wrong.
Assuming you are trying to offset R3 by some small value, you would need that small value to be represented by a number like #4, etc.
From looking at your code, I'm guessing you are trying to store R0 at the address that would be represented by (R3 + R1). One way to do that would be simply:
ADD R3, R3, R1; that is: R3 = R3 + R1 (now R3 has the combined address). STR R0, R3, #0; here we are storing R0 at the address (R3 + #0). Essentially the offset feature of the STR opcode does nothing.
Though you do not need the #0 in this case, recall that it cannot represent an offset outside of this range: -32 <= distance <= 31. That's because STR only reserves 6bits (signed) for the offset address. So if you do use it, be sure it is not out of that range. See lc3tutor.org for opcode reference info, and tools to test your code.
回答2:
STR is used as follows:
STR SR BaseR Offset6
which is equivalent to memory[address stored in BaseR + Offset6] = SR
SR is the source register, BaseR is the memory address you want to offset from, and Offset6 is a 6 bit number. The problem with your code is that you're passing a register, not the contents of the register, as the offset, causing the error.
You would fix it by doing arithmetic on BaseR and using an offset zero:
ADD R3, R3, R1 ; Add R1 to R3 to offset.
STR R0, R3, 0 ; Store R3 in R0 (which you already offset)
You can use a temporary register to do the arithmetic if you need to preserve the value.
When you get cryptic errors like that, it's often useful to read the LC3 instruction set, which you can find here.
来源:https://stackoverflow.com/questions/44056290/lc-3-str-with-r1-as-offset