c - 将 arm 的汇编代码插入到 C 函数中,如何插入外部变量?

标签 c gcc assembly inline-assembly

我有一个 C 文件 f1.c,里面有几个函数。我需要在 armv4 的汇编代码中编写其中一个函数,然后将其放回 f1.c 中。 所以我将我想要的汇编代码中的函数提取到另一个文件(称为 test1.c)中并编译它:

arm-linux-gnueabi-gcc -S -march=armv4 test1.c 

在 test1.c 中我有:

extern long int li1, li2, li3;
int main()
{
    iowrite32(li1, li2);
    iowrite32(li3, li1);
    return 0;
}

之后,我得到以下代码 test1.s:

.arch armv4
.eabi_attribute 27, 3
.fpu vfpv3-d16
.eabi_attribute 20, 1
.eabi_attribute 21, 1
.eabi_attribute 23, 3
.eabi_attribute 24, 1
.eabi_attribute 25, 1
.eabi_attribute 26, 2
.eabi_attribute 30, 6
.eabi_attribute 34, 0
.eabi_attribute 18, 4
.file   "test1.c"
.text
.align  2
.global main
.type   main, %function
main:
    @ Function supports interworking.
    @ args = 0, pretend = 0, frame = 0
    @ frame_needed = 1, uses_anonymous_args = 0
    stmfd   sp!, {fp, lr}
    add fp, sp, #4
    ldr r3, .L2
    ldr r2, [r3, #0]
    ldr r3, .L2+4
    ldr r3, [r3, #0]
    mov r0, r2
    mov r1, r3
    bl  iowrite32
    ldr r3, .L2+8
    ldr r2, [r3, #0]
    ldr r3, .L2
    ldr r3, [r3, #0]
    mov r0, r2
    mov r1, r3
    bl  iowrite32
    mov r3, #0
    mov r0, r3
    sub sp, fp, #4
    ldmfd   sp!, {fp, lr}
    bx  lr
.L3:
    .align  2
.L2:
    .word   li1
    .word   li2
    .word   li3
    .size   main, .-main
    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section    .note.GNU-stack,"",%progbits

问题是我需要将该汇编代码 (t1.s) 放回文件 f1.C,因此我使用:

asm volatile( "stmfd    sp!, {fp, lr}\n\t"
              "add  fp, sp, #4\n\t"
              [...]);

但我不知道如何放回指令,例如: ldr r3, .L2

因为它们引用了标签 L2 下的外部变量,如果我尝试,f1.c 将无法编译

    "ldr r3, .L2\n\t"

谁能告诉我该怎么做? 谢谢。

最佳答案

最后,我是这样做的: 而不是:

    "ldr    r3, .L2+4\n\t"

我将变量的方向保存在另一个寄存器中:

    "ldr    r4, =li2\n\t"

然后我在第一个加载它:

    "ldr    r3, [r4]\n\t"

我不知道这是否是正确答案,但它按预期编译和工作

关于c - 将 arm 的汇编代码插入到 C 函数中,如何插入外部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22656370/

相关文章:

c++ - 负数的strtoul

macos - "Illegal Instruction: 4"错误是什么?为什么 "-mmacosx-version-min=10.x"可以修复它?

c - 警告 : format ‘%s’ expects type ‘char *’ , 但参数 2 的类型为 ‘int’

c++ - GCC 无法解析带有默认参数和以下参数包的方法调用

assembly - 如何同时在两个不同的DOSBox上播放声音?

c - 字符串函数: insert - self implemented via a copy function

c - 奇怪的 malloc 问题

c - 为什么以下不将数组内容写入文件

linux - 了解 libc.a 中的简单 Linux 系统调用

assembly - 在汇编 8086 中输出数字的 ASCII 字符