Search code examples
assemblyarmdelaycortex-m

ARM assembly 'delay' function not working with unified / thumb-2 syntax


I'm using the following code to do a simple counter-based delay/wait for ARM:

.thumb_func
dowait:
   ldr r7,=0x200000
dowaitloop:
   sub r7,#1
   bne dowaitloop
   bx lr        

I got this function from dwelch's blinker01 mbed_samples, which works fine in other led-blinking-type sample programs. However the program I'm currently working on needs to have .syntax unified at the top because I'm using Thumb-2 instructions (e.g. ITTEE).

I suspect ".syntax unified" is the issue because I took the known-working blinker01 example and added .syntax unified and it no longer worked when I uploaded to my board.

While I don't have all the gdb stuff figured out yet to prove it, the function seems to not be counting / delaying.

Is there a different way to re-write this "delay" function to work with unified / Thumb-2 syntax?


Solution

  • You need to use SUBS when you want the instruction to update the flags.

    .syntax unified
    .thumb_func
    dowait:
       ldr r0,=0x200000
    dowaitloop:
       subs r0,#1
       bne dowaitloop
       bx lr   
    

    Another note: R7 is not a clobber register, so in the case you use the dowait function from "C" code, there will be errors as the compiler does not expect R7 to be modified. This is why I changed it to R0.