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

查看:39
本文介绍了如何在 arm 内联汇编中访问局部 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;
}

在你的汇编代码之后,你把 ':' 字符和你想要传递的值像这样:"(=|+)(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.

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

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