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

标签 linux ubuntu nasm assembly

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

$ ld -s -o hello hello.o

这最终将构建 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

最佳答案

这看起来可能是 nasm 生成的内容与 ld 试图生成的内容之间的简单不匹配:

i386 architecture of input file 'hello.o' is incompatible with i386:x86-64 output

换句话说,nasm 已经生成了一个 32 位的目标文件 hello.o 并且 ld 想要使用它并制作一个 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

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

nasm -f elf hello.asm

尝试使用:

nasm -f elf64 hello.asm

相反,您可能会发现 ld 停止提示输入文件。

关于linux - 尝试在 Ubuntu 上的 NASM 上运行 .asm 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4252227/

相关文章:

windows - 如何处理在 Linux 上使用无效 Windows 文件名提交给 Git 的文件?

linux - 程序集:printf 不打印新行

linux - 每个进程的页表是否包含映射到内核地址空间的条目?

c++ - QProcess 多平台命令

python - Lubuntu 终端 Python 桌面快捷方式

linux - 如何将变量赋值的耗时存储到bash脚本中的另一个变量?

git克隆 "does not appear to be a git repository"

java - 如何在Ubuntu 18.04上安装openjdk-9-jdk?

arrays - NASM x86 16 位中的索引 float 组

assembly - 为什么我们在引导加载程序的开头需要 ORG 0x7c00?