cancel
Showing results for 
Search instead for 
Did you mean: 

Defining variable in assembly with STM32CubeIDE

Slh
Senior

I want to define a variable in assembly code and write it in R0 whit this code:

__asm__(
		" var1 DD #123 		\n"
		" ldr.w r0, =var1     	\n"
   	   );

but I have this error when compiling:

"Error: bad instruction `var1 DD 123'"

The compiler doesn't know the "DD" for defining a "Double word" as 32-bit variable.

How can I define a variable in my assembly code and write it into e.g R0?

I use STM32Cube IDE and Cortex-M7.

Thanks

10 REPLIES 10
Pavel A.
Evangelist III

Labels should be in column 1. Remove the space before var1.

Slh
Senior

I tried it, the space has no effect.

It's not executable code. Perhaps define the variable outside the __asm and refer to that.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I tried to assign the value of the "var1" to register R0 in the following ways:

int var1 = 30;
 
int main(void)
{
    __asm ("ldr.w r0, =var1"); 
}

In "R0" I have The first Address of RAM (0x20000000).

When I try this Codes:

__asm ("mov r0, var1");
or 
__asm ("mov r0, #var1");
or
__asm ("mov r0, [var1]");
or
__asm ("mov r0, #var1");

I have "0" in R0.

When I try this Code:

__asm("ldr.w r0, =12"); 

I have "12" (desired value) in R0.

Maybe do you have any idea, how can I load a variable in the register?

Thanks

ONadr.1
Senior III

As I know, the second opperand must be constant or register (with optional shift)

https://developer.arm.com/documentation/dui0646/c/The-Cortex-M7-Instruction-Set/About-the-instruction-descriptions/Flexible-second-operand?lang=en

You should use some load instruction instead of mov.

If the variable is a parameter or auto variable in a register, those can be moved directly.

For load/store against memory, the address of the variable (pointer) needs to be in a register first, and then you load data from that register based address into your destination register.

ie

ldr r1,=&var1

ldr r0,[r1, #0]

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Evangelist III

> the address of the variable (pointer) needs to be in a register first

In your example the 32-bit address is loaded from PC-relative location in literal pool, correct?

Then why the linker could not tally the offset from PC to var1 directly? Is this a limitation of GNU C or linker (like, cannot make link-time relocation between code and data sections)?

I think you're right. I can't use the variable as second operand. So how is the way to store a variable into a register and vice versa?

AIBRA.1
Associate II

I have the same problem so did you reach a solution ?