Omnimaga
Calculator Community => TI Calculators => ASM => Topic started by: ElementCoder on August 12, 2013, 01:29:30 pm
-
I'm playing around a bit with keypresses and wanted to put something together with it. It's a very simple idea: detect a keypress, branch to the appropriate label, do your thing and branch back to the keypress detection loop. In my attempt below the ctrl key exits the program and the enter key branches to another part. My problem is that this branch stays, when it goes back to the keypress detection loop (at least I think it does), it keeps branching to the part where the enter key points to. Have I misunderstood something about ARM or am I doing something wrong codewise?
#include <os.h>
main: .global main
push {r4-r11, lr}
bl lcd_ingray
bl clrscr
ldr r1, =0x900E0010
keycheck:
ldrh r2, [r1, #0]
tst r2, #1 << 1
bne enterkey
ldrh r2, [r1, #14]
tst r2, #1 << 9
bne ctrlkey
b keycheck
@ handle various keypresses
enterkey:
b stuff
ctrlkey:
b end
stuff:
b keycheck
end:
mov r0, #0
pop {r4-r11, pc}
-
Just the code you posted works. But if you call any function in stuff, r1 (where you stored the keypad address) may change and you read from a wrong address. This happens because r0 to r3 aren't preserved when a function is called, only r4 to r11.
So if you move line 6 of your code after the "keycheck" label everything should work (or if you save the address to r4 instead of r1).
-
It works now, thanks :D So r0-r3 are literally scratch registers that just get arbitrary values throughout program execution?
-
So r0-r3 are literally scratch registers that just get arbitrary values throughout program execution?
The standard ARM calling convention is that r0-r3 are used for passing the 4 first arguments to a function (the rest on the stack), and that the same set of registers is assumed to be destroyed by any called function (even though it does not necessarily do so, it's a severe bug to assume that after any function call, r0, r1, r2 or r3 has some value).