alex quintanilla
alex quintanilla

Reputation: 21

How do I calculate the offset to load R1 with address x3100?

So here's my code I have. The program asks for the user to enter a single digit and stores in R3. Then it checks that digit with 5 other numbers stored in memory locations x3101 - x3105. (The digits are 2, 2, 4, 4, 4)

I believe my issue is with my 2nd line. "LD R1, x0030".
I know x0030 is incorrect I was just playing with it. I'm trying to get it load R1 with x3100 so it can move on to 3101, 3102, and so on later.

Code so far:

.ORIG   x3000       ;0011 0000 0000 0000 ?


    AND R5, R5, #0
    LD R1, x0030
    TRAP x23
    LDR R4, R1, #0
    ADD R2, R4, #-5
    BRz #8
    NOT R4, R4
    ADD R4, R4, #1
    ADD R4, R4, R3
    BRnp #1
    ADD R5, R5, #1
    ADD R1, R1, #1
    LDR R4, R1, #0
    BRnzp #-10
    LD R3, #5
    ADD R3, R3, R5
    TRAP x21
    TRAP x25
.FILL x3100



.END


.ORIG x3100
.FILL  x0005      ; iteration number
.FILL  x0032      ; number 2
.FILL  x0032
.FILL  x0034      ; number 4
.FILL  x0034
.FILL  x0034
.END

Upvotes: 0

Views: 227

Answers (2)

Recifarium
Recifarium

Reputation: 21

Let the assembler do the maths for you:

 LEA R1, mylabel
 ...
 ...
 .ORIG xVVVV    ;(whatever value you want)
mylabel .FILL  x0005 
.FILL  x0032      ; number 2
...

The less you use literal values for addresses the more robust is your code.

Upvotes: 0

Erik Eidt
Erik Eidt

Reputation: 26656

Use a label for your constant:

    LD R1, myLabel
    ...
    ...
myLabel .FILL x3100

If you use this approach, and (run it through the assembler, and then) load it in the simulator, you will be able to see the machine code instruction (x2210), having the PCOffset9 value that is needed to reach the .FILL x3100.  If you want, you can change your code to the proper offset (here x10, aka #16) — but it will be more fragile than using the label, so if you make subsequent changes, the offset can change.

Upvotes: 1

Related Questions