0

I recently started to learn programming in ARM assembly,

today I encountered annoying problem where my program started to crash when I used the r4 register.

With this simple code the program works fine

    .text
    .align  2
    .global arr
    .type   arr, %function
arr:
    mov r3, #8
    mov r0, r3

    bx lr
    .size   arr, .-arr

But changing to r4 it crashes

    .text
    .align  2
    .global arr
    .type   arr, %function
arr:
    mov r4, #8
    mov r0, r4

    bx lr
    .size   arr, .-arr

What the hell am I doing wrong??? It clearly states in ARM docs this:

In all ARM processors, the following registers are available and accessible in any processor mode: 13 general-purpose registers R0-R12 Source

The supposed 'duplicate' question regarding 'answer' does not help at all, since everyone there has categorized r4 (THE ONE THAT I HAVE PROBLEMS) with others (r5, r6, ... that I dont have problems).

I am looking for explanation why using specifically r4 causes this problem, I do not have problems using instead of r4 registers that are supposed to be the same type as r4 (r5, r6, r7, r8).

Community
  • 1
  • 1
VOid
  • 85
  • 2
  • 13
  • 3
    Your code that works fine uses `r0` and `r3` which is certainly not `r5`,`r6`. Also note that not getting a crash with those doesn't mean the code is correct, it just means that your caller doesn't depend on those registers being preserved. But it could, because the calling convention says so. Better question is, if you save and restore `r4`, does that fix your problem? Because if it does, then it **is** a duplicate of that question. – Jester Mar 02 '15 at 15:48
  • 1
    You are correct, the program does not crash if I preserve r4 value after the assembler program exits, this means that I have to preserver r4-r8 register values after my assembler functions? – VOid Mar 02 '15 at 16:01
  • 3
    Read the topmost answer of the linked "duplicate" question! When you use r4-r8, you have to save the contents when you start your function and restore the values after it. When you don't, anything can happen. You have to adhere to the calling conventions. – Juergen Mar 02 '15 at 16:14
  • Itr is dumb luck that you didnt have problems. r4 is more sensitive than r5 and so on since the compiler is going to likely choose them sequentially, you need more code and more nesting of code before hitting your program before you hit registers that you must preserve. It would not be hard to craft a test that your code would break, just play with the compiler and think about what causes the compiler to consume more registers in a function... – old_timer Mar 02 '15 at 21:52

0 Answers0