在Linux中从C代码调用汇编函数

4

我希望能在我的C程序中调用打印函数。
汇编程序:

 #test.s
.text

    .global _start
    .global print
    .type print, @function

_start:

        call print

        # and exit.

        movl    $0,%ebx         # first argument: exit code.
        movl    $1,%eax         # system call number (sys_exit).
        int     $0x80           # call kernel.

print:
    # write our string to stdout.

        movl    $len,%edx       # third argument: message length.
        movl    $msg,%ecx       # second argument: pointer to message to write.
        movl    $1,%ebx         # first argument: file handle (stdout).
        movl    $4,%eax         # system call number (sys_write).
        int     $0x80           # call kernel.

        mov $0,   %eax
        ret
.data

msg:
        .ascii  "Hello, world!\n"      # the string to print.
        len = . - msg                  # length of the string.

我可以使用以下方法组装和链接它:
$as test.s -o test.o
$ld test.o -o test

我可以执行它作为一个程序,然后它输出"Hello, world!"。但是当我试图像这样从C代码中调用打印函数时:
#include <stdio.h>
extern int print();

int main(){
    int g;
    g = print();
    printf("Hello from c!, %d\n", g);

}

这个程序是用以下工具编译的:

$gcc -c main.c test

它只打印“Hello from c,13”,这意味着函数被调用并返回字符数,但不会打印任何内容!

我做错了什么?

P.S. 当我尝试编译这样的程序时:

$as test.s -o test.o
$gcc -c main.c -o main.o
$gcc main.c test.o 

我有一个错误:
/usr/bin/ld: test.o: in function `_start':
(.text+0x0): multiple definition of `_start'; /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../lib/Scrt1.o:(.text+0x0): first defined here
/usr/bin/ld: test.o: relocation R_X86_64_32 against `.data' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status

1
总体做得很好,但我怀疑sys_write()或write()是否实际上使用printf()样式的格式化或查看其他参数。通常在汇编语言层面上,需要使用多个写操作构建格式化输出的各个部分,或者将格式化文本构建到数据区域中,然后一次性写出完整的格式化缓冲区。https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm - clearlight
2
这能帮助您解决-fPIE问题吗?https://dev59.com/nKXja4cB1Zd3GeqPKwby - clearlight
1
使用 gcc -no-pie -c test.s -o test.ogcc -no-pie -o main main.c test.o 进行编译似乎可以解决问题... - dragosht
2
这是32位代码。使用gcc -m32 main.c test.s -o main进行构建。您可以选择使用-fno-pie -no-pie,因为对于32位来说这是一个很好的提高效率的想法。 - Peter Cordes
1个回答

0

好的,完成了!谢谢 clearlight

我可以编译所有使用

$as test.s -o test.o
$gcc -c main.c -o main.o
$gcc -no-pie main.c test.o 

一切都会正常工作!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接