在C函数中插入arm的汇编代码时,如何插入外部变量?
我有一个C文件f1.C,里面有几个函数。我需要在armv4的汇编代码中编写其中一个函数,然后将其放回f1.c中。 因此,我将汇编代码中需要的函数提取到另一个文件(称为test1.c)中,并使用以下代码编译它:在C函数中插入arm的汇编代码时,如何插入外部变量?,c,gcc,assembly,inline-assembly,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(
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"
我不知道这是否是正确的答案,但它按照预期编译和工作您实际上模拟了
main()
的逻辑来设置堆栈帧以调用iowrite32()
。这真的是你想要的吗?我想你更愿意(重新)实现iowrite32()
?请仔细阅读你的问题,确保你在“函数”时说“函数”,在“文件”时说“文件”。我觉得这两个单词有点互换了,这让我很难理解。@mfro我只使用str命令在内存中写入,而不是使用iowrite32