How do I declare a variable in inline ARM Assembly?

How do I declare a variable in ARM inline Assembly?

And how do I move the value from register to variable and vice versa?

Looking for something like this,

__asm(

    variable: Word #22222222

    mov r0, variable

    mov variable, r0

)

  • What compiler and hardware platform are you using?

  • In general whatever the compiler one would normally declare any statics using C rather than in an asm. In gcc one would have to say the static is an input or output of the asm as appropriate.

  • Which complier version are you using?
    If your using Arm Compiler 5, you can use "Use MOV Rn, expression" which generates a load from a literal pool as stated in arminfo.emea.arm.com/.../chr1359124248868.html.

  • if you're using inline assembler in a C or C++ program, you should just define the variable in the C part of the code:

       static uint32_t variable = 0x22222222;

    For a separate module using the gnu assembler, data and program space get relocated at link time as appropriate for the chip, so you have to explicitly tell the assembler which "section" to put things in using ".data", ".text", or ".section" directives.  It would look something like:

    .data
    
    variable: .long 0x22222222   // global variable
    
    .text
    
    myfunc:
    
       ldr r1, =variable  ;; get addr of variable from pc-relative constant
       ldr r2, [r1]       ;; get the actual value of variable
       str r2, [r1]         ;; store it back.


    That syntax may be a little off...
    If you have a lot of global variables, you'd probably set up the base address for all of them in some register, and access them with the offset form of ldr.
    If the variable is "local", you can put it on the stack and use [sp] as the base register.