汇编语言链接器错误

时间:2015-11-23 16:50:17

标签: macos assembly x86 nasm

我在Mac上运行OS / X 10.8和Xcode 5.1.1,似乎无法编译任何基本的汇编语言。从命令终端编译时,我收到以下错误:

ld: warning: ignoring file proj2.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): proj2.o
        Undefined symbols for architecture x86_64:
          "_main", referenced from:
             implicit entry/start for main executable
        ld: symbol(s) not found for architecture x86_64
        clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人可以向我解释这个错误,以及如何解决它?

我使用这些命令编译,链接和运行:

nasm -f elf -l proj2.lst proj2.asm    
gcc -o proj2 proj2.o
./proj2.o

我的代码:

SECTION .data
msg: db "Hello world", 10,0 ; this is the message
yes db '6', 10, 0
no db '-1', 10, 0

SECTION .text
extern printf
global main

compare:    
    mov edi, [esp+4]
    mov ecx, [esp+8]
    mov eax, [esp+12]

    repne scasb;
    mov eax, 0
    setz al
    ret

found:
    jmp displayYes

displayYes:
    push yes    
    call printf 
    mov eax, 0
    leave 
    ret

displayNo: 
    push no
    call printf 
    mov eax, 0
    leave 
    ret

main:
    push ebp
    mov ebp, esp
    push dword 'w'
    push dword 10
    push msg
    call compare

    test eax, eax ; if letter equals 'w' set condition to zero 
    jnz found 

    push no 
    jmp displayNo

更新

如果我用编译

nasm -f macho proj2.asm

我收到了这个新错误:

ld: warning: ignoring file proj2.o, file was built for i386 which is not the architecture being linked (x86_64): proj2.o    Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

0 个答案:

没有答案