尝试在 Ubuntu 上的 NASM 上运行 .asm 文件时出错 [英] Error when trying to run .asm file on NASM on Ubuntu

查看:52
本文介绍了尝试在 Ubuntu 上的 NASM 上运行 .asm 文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using ubuntu 64-bit and trying to run a .asm file on NASM. But it returns this error when I try to run the following code. What Iḿ trying to do is build an executable by compiling (or assembling) object file from the source $ nasm -f elf hello.asm, and then after created the file hello.o is producing executable file itself from the object file by invoking linker

$ ld -s -o hello hello.o

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

This will finally build hello executable.

我正在学习本教程 http://www.faqs.org/docs/Linux-HOWTO/Assembly-HOWTO.html

错误:

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

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

代码:

     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 正在尝试的内容之间的简单不匹配制作:

This looks like it may be a simple mismatch between what's produced by nasm and what ld is trying to make:

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

换句话说,nasm 生成了一个 32 位的目标文件 hello.o 并且 ld 想要获取它并制作一个 64-bit 可执行文件.

In other words, nasm has produced a 32-bit object file hello.o and ld wants to take that and make a 64-bit executable file.

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

The nasm -hf command should give you the available output formats:

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

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

I see that your linked tutorial asks you to run:

nasm -f elf hello.asm

尝试使用:

nasm -f elf64 hello.asm

相反,您可能会发现 ld 不再抱怨输入文件.

instead, and you may find ld stops complaining about the input file.

这篇关于尝试在 Ubuntu 上的 NASM 上运行 .asm 文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆