2022-02-24 08:01 AM
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
2022-02-25 01:39 AM
Labels should be in column 1. Remove the space before var1.
2022-02-25 05:27 AM
I tried it, the space has no effect.
2022-02-25 05:50 AM
It's not executable code. Perhaps define the variable outside the __asm and refer to that.
2022-02-28 12:40 AM
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
2022-02-28 06:13 AM
As I know, the second opperand must be constant or register (with optional shift)
You should use some load instruction instead of mov.
2022-02-28 06:25 AM
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]
2022-02-28 09:05 AM
> 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)?
2022-03-01 12:06 AM
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?
2023-01-17 04:57 AM
I have the same problem so did you reach a solution ?