Skip to main content
Slh
Senior
Question

Defining variable in assembly with STM32CubeIDE

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

This topic has been closed for replies.

10 replies

Pavel A.

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

Slh
SlhAuthor
Senior

I tried it, the space has no effect.

Tesla DeLorean
Guru

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Slh
SlhAuthor
Senior

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

Tesla DeLorean
Guru

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
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.

Slh
SlhAuthor
Senior

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 ?

Slh
SlhAuthor
Senior

Still not...