2012-02-16 67 views
6

我讀this文章, 在一個點它給了我這個NASM程序:NASM/gcc的問題

; tiny.asm 
BITS 32 
GLOBAL main 
SECTION .text 
main: 
       mov  eax, 42 
       ret 

,並告訴我運行以下命令:

$ nasm -f elf tiny.asm 
$ gcc -Wall -s tiny.o 

我得到了以下錯誤:

ld: warning: option -s is obsolete and being ignored 
ld: warning: ignoring file tiny.o, file was built for unsupported file format which is not the architecture being linked (x86_64) 
Undefined symbols for architecture x86_64: 
    "_main", referenced from: 
     start in crt1.10.6.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 

我在大膽猜測可能是什麼問題,並改變了BITS行改爲:

BITS 64 

但後來當我運行nasm -f elf tiny.asm我得到:

tiny.asm:2: error: `64' is not a valid segment size; must be 16 or 32 

如何修改代碼以我的機器上運行?

編輯:

我從評論了Alex的建議和下載更新的版本。然而,

./nasm-2.09.10/nasm -f elf tiny.asm 

抱怨

tiny.asm:2: error: elf32 output format does not support 64-bit code 

在另一方面,

./nasm-2.09.10/nasm -f elf64 tiny.asm 
gcc -Wall -s tiny.o 

抱怨

ld: warning: ignoring file tiny.o, file was built for unsupported file format which is not the architecture being linked (x86_64) 
Undefined symbols for architecture x86_64: 
    "_main", referenced from: 
     start in crt1.10.6.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 
+0

檢查您是否擁有最新的nasm並嘗試'-f elf64'。 – 2012-02-16 09:47:32

+0

@Alex elf64給了我「無法識別的輸出格式」。至於該版本,nasm -v給了我「2011年11月3日編譯的NASM 0.98.40版(Apple Computer,Inc. build 11)」0。98看起來可能是一箇舊版本,但它在2011年編譯,因此它不可能是舊的權利?爲什麼蘋果會推出過時的軟件?默認的彙編程序無法在64位平臺上組裝64位代碼? – math4tots 2012-02-16 09:53:04

+0

你的是可怕的過時。我的作品「2011年7月15日編譯的NASM 2.09.10版」。較新的版本是[這裏](http://www.nasm.us/pub/nasm/releasebuilds/2.09.10/)。 – 2012-02-16 09:59:54

回答

13

有OS X專用的調整,你必須爲了使爲你的例子工作: 主要方法被換成了一個_由OS X鏈接:

; tiny.asm 
BITS 32 
GLOBAL _main 
SECTION .text 
_main: 
    mov  eax, 42 
    ret 

第二個是,你必須使用馬赫文件格式:

nasm -f macho tiny.asm 

現在你可以將其鏈接(使用 - m32表示一個32位的目標文件):

gcc -m32 tiny.o 
+1

+1。完全忘了這是「馬赫」。 – 2012-02-16 11:14:21

2

看來你還在使用32位版本。如果你nasm -hf它應該列出macho64。如果不是,則需要再次更新。

你可以在控制檯brew update上試試。如果這執行更新然後brew search nasm它應該帶來nasm。然後簡單地brew install nasm。這應該將nasm安裝到您的計算機上。一定要注意它的安裝位置。礦安裝在/usr/local/cellar/nasm/2.11.02/bin/。然後通過輸入nasm -hf它應該提供一個可用格式列表,您應該看到macho64可用。