簡體   English   中英

在Linux 64位上組合C和匯編(32位代碼)

[英]Combining C and Assembly(32 bit code) on Linux 64 bit

我有一個64位的Ubuntu操作系統,我一直在學習32位匯編。 我正在嘗試編譯這兩個文件:

square.s:

#square.s

.section .text
.globl sqr
.type sqr, @function
sqr:
    pushl %ebp
    movl %esp, %ebp
    movl 8(%ebp), %eax
    imull %eax, %eax
    popl %ebp
    ret

main.c:

//main.c
#include <stdio.h>
extern long sqr(long);
int main(int argc, char* argv[])
{
    long squared = sqr(10);
    printf("%lu\n", squared);
    return 0;
}

在我的32位虛擬機上,我使用此命令編譯它們

  gcc main.c square.s -o test

它起作用了。 我遇到的問題是我想在我的64位機器上編譯這些文件。 我已經嘗試了幾種編譯這些文件的方法,但沒有一種方法可行。 誰能指出我正確的方向? 有沒有選擇這樣做? 我試過-m32但是沒有用。

當我這樣做:

  gcc -m32 -o test main.c square.s

我明白了:

  In file included from /usr/include/stdio.h:28:0,
             from main.c:1:
/usr/include/features.h:323:26: fatal error: bits/predefs.h: No such file or directory
compilation terminated.

在64位ubuntu上編譯/鏈接32位程序需要gcc-multilib,嘗試:

sudo apt-get install gcc-multilib libc6-i386 lib6-dev-i386

但是,當您嘗試鏈接其他庫時,這可能還有其他問題。

使用32位chroot環境 (即在64位ubuntu上運行32位root)會更好。

看起來您的問題通常與32位編譯有關,無論匯編代碼如何。 有些東西可能是配置錯誤的。

另外,您是否考慮使用內聯匯編而不是.s文件? 以這種方式集成C和匯編要容易得多,而且您不必擔心調用約定細節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM