【问题标题】:inserting assembly code for arm into a C function, how do I insert extern variables?将arm的汇编代码插入C函数,如何插入外部变量?
【发布时间】:2014-03-26 09:20:51
【问题描述】:

我有一个 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"

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

【问题讨论】:

  • 你实际上是在模仿main() 的逻辑来设置一个堆栈帧来调用iowrite32()。这真的是你想要的吗?我假设您宁愿(重新)实施iowrite32()?
  • 请再次仔细阅读您的问题,并确保在您的意思是“功能”时使用“功能”,在您的意思是“文件”时使用“文件”。我觉得这两个词有点互换,这很难理解。
  • @mfro 我只使用 str 命令写入内存,而不是使用 iowrite32

标签: c gcc assembly inline-assembly


【解决方案1】:

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

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

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

    "ldr    r4, =li2\n\t"

然后我在第一个加载它:

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

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

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 2021-04-23
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多