如何在手臂内联汇编中访问局部C变量? [英] How do I access local C variable in arm inline assembly?

查看:131
本文介绍了如何在手臂内联汇编中访问局部C变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问在内联臂汇编中用C声明的局部变量.我该怎么办?

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"
    );
}       

但是如何访问局部变量?我尝试将局部变量的".global"更改为".local",但是会产生错误(未定义对"temp"的引用). 我正在使用的IDE是KEIL.

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.

推荐答案

根据GCC文档: 6.45.2.3输出操作数

您可以像这样传递值:

#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;
}

在您的asm代码之后,您输入':'字符和要传递的值,如下所示:"(=|+)(r|m)" (variable).覆盖值时使用'=',读取或覆盖值时使用'+',如果值位于寄存器中,则使用'r'字母;如果值位于存储器中,则使用'm'字母.

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.

这篇关于如何在手臂内联汇编中访问局部C变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆