Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access local C variable in arm inline assembly?

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.

like image 655
Muzahir Hussain Avatar asked Nov 27 '17 16:11

Muzahir Hussain


1 Answers

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.

like image 69
ProNOOB Avatar answered Oct 03 '22 01:10

ProNOOB