尝试在Ubuntu上运行NASM上的.asm文件时出错

rog*_*gcg 25 linux ubuntu assembly nasm

我正在使用ubuntu 64位并尝试在NASM上运行.asm文件.但是当我尝试运行以下代码时它会返回此错误.我想要做的是通过从源代码编译(或汇编)目标文件来构建可执行文件 $ nasm -f elf hello.asm,然后在创建文件后hello.o通过调用链接器从目标文件生成可执行文件本身

$ ld -s -o hello hello.o
Run Code Online (Sandbox Code Playgroud)

这将最终构建hello可执行文件.

我正在关注这个教程http://www.faqs.org/docs/Linux-HOWTO/Assembly-HOWTO.html

错误:

输入文件`hello.o'的i386体系结构与i386:x86-64输出不兼容

码:

     section .data              ;section declaration

 msg     db      "Hello, world!",0xa    ;our dear string
 len     equ     $ - msg                 ;length of our dear string

 section .text              ;section declaration

             ;we must export the entry point to the ELF linker or
     global _start       ;loader. They conventionally recognize _start as their
             ;entry point. Use ld -e foo to override the default.

 _start:

 ;write our string to stdout

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

  ;and exit

     mov    ebx,0   ;first syscall argument: exit code
         mov     eax,1   ;system call number (sys_exit)
         int     0x80   ;call kernel
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 38

这看起来可能是产生的nasm和产生的东西之间的简单不匹配ld:

i386 architecture of input file 'hello.o' is incompatible with i386:x86-64 output
Run Code Online (Sandbox Code Playgroud)

换句话说,nasm已生成一个32位目标文件,hello.old希望获取该文件并生成64位可执行文件.

nasm -hf命令应该为您提供可用的输出格式:

valid output formats for -f are (`*' denotes default):
  * bin       flat-form binary files (e.g. DOS .COM, .SYS)
    ith       Intel hex
    srec      Motorola S-records
    aout      Linux a.out object files
    aoutb     NetBSD/FreeBSD a.out object files
    coff      COFF (i386) object files (e.g. DJGPP for DOS)
    elf32     ELF32 (i386) object files (e.g. Linux)
    elf       ELF (short name for ELF32) 
    elf64     ELF64 (x86_64) object files (e.g. Linux)
    as86      Linux as86 (bin86 version 0.3) object files
    obj       MS-DOS 16-bit/32-bit OMF object files
    win32     Microsoft Win32 (i386) object files
    win64     Microsoft Win64 (x86-64) object files
    rdf       Relocatable Dynamic Object File Format v2.0
    ieee      IEEE-695 (LADsoft variant) object file format
    macho32   NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files
    macho     MACHO (short name for MACHO32)
    macho64   NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files
    dbg       Trace of all info passed to output stage
Run Code Online (Sandbox Code Playgroud)

我看到你的链接教程要求你运行:

nasm -f elf hello.asm
Run Code Online (Sandbox Code Playgroud)

尝试使用:

nasm -f elf64 hello.asm
Run Code Online (Sandbox Code Playgroud)

相反,你可能会发现ld停止抱怨输入文件.


caf*_*caf 13

您需要告诉链接器生成i386输出文件,因为您正在编写i386程序集:

ld -m elf_i386 -s -o hello hello.o
Run Code Online (Sandbox Code Playgroud)

  • @psyhclo:不,这是*不*相同的错误,这是相反的错误,因为您还告诉 `nasm` 创建 x86-64 输出。做一个*或*另一个,而不是两个!(我强烈建议创建 `i386` 输出,这意味着像你最初所做的那样调用 `nasm`,并以这种方式调用 `ld`,因为你实际上是在编写 i386 汇编程序而不是 x86-64)。 (2认同)