Author Topic: [ARM] Problem with branches  (Read 3128 times)

0 Members and 1 Guest are viewing this topic.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
[ARM] Problem with branches
« 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?

Code: [Select]
#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}
« Last Edit: August 12, 2013, 01:30:13 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: [ARM] Problem with branches
« Reply #1 on: August 12, 2013, 03:35:17 pm »
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).

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: [ARM] Problem with branches
« Reply #2 on: August 13, 2013, 11:10:58 am »
It works now, thanks :D So r0-r3 are literally scratch registers that just get arbitrary values throughout program execution?

Some people need a high five in the face... with a chair.
~EC

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: [ARM] Problem with branches
« Reply #3 on: August 13, 2013, 11:22:10 am »
Quote
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).
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.