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

后端 未结 2 1512
生来不讨喜
生来不讨喜 2021-01-12 18:29

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;
Fun         


        
2条回答
  •  伪装坚强ぢ
    2021-01-12 18:58

    According to GCC docs: 6.45.2.3 Output Operands

    You can pass the values like this:

    #include 
    
    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.

提交回复
热议问题