This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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

)

Parents
  • 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.

Reply
  • 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.

Children
No data