gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > 3 lines

#50561 - anoneem - Wed Aug 10, 2005 7:47 pm

Hye!

Here's a small piece of code:

ldr r0, =#0x403
ldrb r1, [r0]
ldr r2, [$403]

My interpretation of it:

1. load an immediate value $403
2. load byte from adress holden by r0
3. load value from adress $00000403

VBA's opinion...

1. Yes, r0 contains 0x403 but...
Instruction was assembled as...
ldr r0, [$08000010]

2. Everything looks nice, but r1 = $e0.
It should (?) be 0 (byte @ 0x403 is)

3. This is the worst.
Assembled as:
ldr r2, [$08000403] (=$03020202)
But r2 is... 01020002

I am sure that goldroad1_7 is OK. Problem is propably in my head - what am i understanding bad in this code?

#50566 - DekuTree64 - Wed Aug 10, 2005 8:05 pm

anoneem wrote:
ldr r0, =#0x403

In C-ish, this would be "r0 = 0x403".
It stores the value 0x403 somewhere near the PC (0x8000010 in your case), and replaces the instruction with ldr r0, [pc, #offset].

Quote:
ldrb r1, [r0]

Then this loads a byte from the address in r0. Since r0 is 0x403, you're loading from address 0x403, which is random data from the protected BIOS...

Quote:
ldr r2, [$403]

This is not even a valid instruction. You have to specify a base register to load from, and the offset from that register must fit in 8 shifted bits (0x403 spans 11 bits).
It's probably actually loading from r0 though, since r0 is 0x403.

The reason you get a weird value is because it's a) also in the BIOS, and b) not a word-aligned address. I don't remember the exact behavior of unaligned reads, but obviously it's pretty squirrely :)
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku