使用ASM代码中的C函数(x64,GAS) [英] Using C functions from ASM code (x64, GAS)
问题描述
我写这段代码只是为了学习如何使用气体汇编程序:
I've written this code just to learn how to use gas assembler:
.code32
.section .data
values:
.int 1,2,3,4,5,6,7,8,9
output:
.asciz "current value %d"
.section .text
.globl _start
_start:
movl $10, %edi
loop:
movl values(,%edi,4),%eax
pushl %eax
pushl $output
call printf
addl $8, %esp
dec %edi
jnz loop
pushl $0
call exit
它使用两个C lib函数(printf和exit)。我在x64上,但是这个代码是32位,所以我使用这个命令组装:
as --32 -o test.o test.asm
看起来它产生了一个有效的obj文件。问题是我无法链接这个文件...
我使用这个命令:
ld -dynamic-linker /lib/ld-linux.so.2 -lc -o test test.o
但它说拱门不兼容,所以我尝试过:
ld -dynamic-linker /lib/ld-linker.so.2 -lc -o test test.o -melf_i386
然后它说:
ld:跳过/usr/lib64/libc.so不兼容-lc
如何链接该obj文件?非常感谢您的帮助!
It uses two C lib functions (printf and exit). I'm on x64, but this code is 32bit, so I assembled using this command:
as --32 -o test.o test.asm
It looks like it produce a valid obj file. The problem is that I'm not able to link this file...
I use this command:
ld -dynamic-linker /lib/ld-linux.so.2 -lc -o test test.o
but it says that the arch is not compatible, so I've tried:
ld -dynamic-linker /lib/ld-linker.so.2 -lc -o test test.o -melf_i386
but then it says:
ld: skipping /usr/lib64/libc.so not compatible -lc
How should I link that obj file ? Many thanks for the help!
推荐答案
10,%edi
循环:
movl values(,%edi,4),%eax
pushl%eax
pushl
10, %edi loop: movl values(,%edi,4),%eax pushl %eax pushl
输出
调用printf
addl
output call printf addl
8,%esp
dec%edi
jnz loop
pushl
8, %esp dec %edi jnz loop pushl
这篇关于使用ASM代码中的C函数(x64,GAS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!