I want to access local variable declared in C in inline arm Assembly. How do I do that?
Global variables can be accessed like this,
int temp = 0;
Function(){
__asm(
".global temp\n\t"
"LDR R2, =temp\n\t"
"LDR R2, [R2, #0]\n\t"
);
}
But how do I access local variables? I tried changing ".global" to ".local" for local variables, but it generated error (undefined reference to `temp'). The IDE I am using is KEIL.
Any thoughts? Thanks in Advance.
According to GCC docs: 6.45.2.3 Output Operands
You can pass the values like this:
#include <stdio.h>
int main(int argc, char *argv[]) {
int src = 1;
int dst;
asm ("mov %1, %0\n\t add $1, %0" : "=r" (dst) : "r" (src));
printf("0x%X\n", dst);
return 0;
}
After your asm code you put the ':'
character and the values you want to pass like this: "(=|+)(r|m)" (variable)
. Use '='
when overriding the value and '+'
when reading or overriding the value, then use the 'r'
letter if the value resides in a register or 'm'
if it resides in memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With