Program 6: Sticky Keyboard
Are you tired of adding one all the time to increment memory addresses? What if I told you there was a better way? A way where you didn’t need the ADD opcode. A way that allows the processor to work in less cycles?!?!?!? Now for $19.95 I will show you the secret to be more efficient. Or you could just read below.
Incrementing LDR/STR instructions #
Offset address #
The first method is to alter the address before the LDR or STR operation happens. When you use this syntax, the change in address does not write a new value to the address register. There are some use cases for this such as looking ahead in memory.
ldr r0, =address @ pretend the address loaded is 0x1000 ldrb r1, [r0, #2] @ this would load the byte at 0x1002 to r1 @ r0 still equals 0x1000
Pre-index address #
By using this method, the address with be adjusted before the LDR/STR operation
(as above) however it will then write this address to the address register. The only
difference is the addition of an
! after the offset.
ldr r0, =address @ pretend the address loaded is 0x1000 ldrb r1, [r0, #2]! @ this would load the byte at 0x1002 to r1 @ r0 would then be set to 0x1002
Post-index address #
This is the form that you will see most often. This will allow you to read an address and then offset the address value after the LDR/STR.
ldr r0, =address @ pretend the address loaded is 0x1000 ldrb r1, [r0], #2 @ this would load the byte at 0x1000 to r1 @ r0 is then set to 0x1002