c - 如何将寄存器的值从 ARM 汇编返回到 C 函数?

标签 c function assembly arm return

例如

extern int addfunction(int,int);
main() {
  int a = 5, b = 6, z;
  z = addfunction(a, b);
  printf("%d", c);
}

addfunction() 在另一个 .s 文件中的定义,例如

.global addfunction
addfunction:
    @ value a will be in R0 & R1 respectively
    add r2, r0, r1    @ r2=r0+r1

现在如何返回存储的值r2,以便C程序中的变量z获得正确的值?我可以使用 C 中的指针并更新 asm 中的地址。我不想这样。帮助!

最佳答案

here

The standard 32-bit ARM calling convention allocates the 16 ARM registers as:

r0 to r3: used to hold argument values passed to a subroutine, and also hold results returned from a subroutine.

所以将r0设置为返回值

.global addfunction
addfunction:
    @ value a will be in R0 & R1 respectively
    add r0, r0, r1    @ r0=r0+r1

关于c - 如何将寄存器的值从 ARM 汇编返回到 C 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44898977/

相关文章:

sql - oracle sql select 与员工

c - 在 C 中作为参数的函数

c++ - 当前的任何C++编译器都发出 “rep movsb/w/d”吗?

delphi - 删除用纯汇编编写的函数的序言

c - Vigenere Cipher - 无法解释的细微差别

r - 在 data.table 的整个列上应用自定义函数?

c - while 循环在它应该 C 之前终止

visual-studio - Visual Studio (MASM) 程序集 - 为什么即使未调用标签,标签中的代码也会自动执行

使用 COM 端口发送和接收串行 (UART) 数据的 C 程序

c - 为 UNIX C 程序获取共享变量锁的最简单方法是什么?