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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4252227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:04:31  来源:igfitidea点击:

Error when trying to run .asm file on NASM on Ubuntu

linuxubuntunasmassembly

提问by rogcg

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.ois producing executable file itself from the object file by invoking linker

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

$ ld -s -o hello hello.o

This will finally build hello executable.

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

I'm following this tutorial http://www.faqs.org/docs/Linux-HOWTO/Assembly-HOWTO.html

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

Error:

错误:

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

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

Code:

代码:

     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

采纳答案by paxdiablo

This looks like it may be a simple mismatch between what's produced by nasmand what ldis trying to make:

这看起来可能是由生产的产品nasmld试图生产的产品之间的简单不匹配:

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

In other words, nasmhas produced a 32-bit object file hello.oand ldwants to take that and make a 64-bit executable file.

换句话说,nasm已经生成了一个 32 位的目标文件,hello.old希望利用它来制作一个 64 位的可执行文件。

The nasm -hfcommand should give you the available output formats:

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

I see that your linked tutorial asks you to run:

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

nasm -f elf hello.asm

Try using:

尝试使用:

nasm -f elf64 hello.asm

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

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

回答by caf

You need to tell the linker to produce an i386 output file, since you're writing i386 assembly:

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

ld -m elf_i386 -s -o hello hello.o

回答by Eric Leschinski

How to compile, link, and run a nasm app on Ubuntu 64 bit.

如何在 64 位 Ubuntu 上编译、链接和运行 nasm 应用程序。

Install nasm:

安装nasm:

sudo apt-get install nasm

Save a file with filename hello.asm:

使用 filename 保存文件hello.asm

section .data
  hello:     db 'Hello world!',10    ; 'Hello world!' plus a linefeed character
  helloLen:  equ $-hello             ; Length of the 'Hello world!' string
                                     ; (I'll explain soon)

section .text
  global _start

_start:
  mov eax,4            ; The system call for write (sys_write)
  mov ebx,1            ; File descriptor 1 - standard output
  mov ecx,hello        ; Put the offset of hello in ecx
  mov edx,helloLen     ; helloLen is a constant, so we don't need to say
                       ;  mov edx,[helloLen] to get it's actual value
  int 80h              ; Call the kernel

  mov eax,1            ; The system call for exit (sys_exit)
  mov ebx,0            ; Exit with return code of 0 (no error)
  int 80h

Compile it:

编译它:

nasm -f elf64 hello.asm

Link it:

链接它:

ld -s -o hello hello.o

Run it

运行

el@apollo:~$ ./hello
Hello world!

It works! What now?Request that your favorite compiler generate the assembly code that it would have been normally passed on to be converted to machine code. Google search: "convert php/java/python/c++ program to assembly"

有用!现在怎么办?请求您最喜欢的编译器生成通常会被传递以转换为机器代码的汇编代码。谷歌搜索:“将php/java/python/c++程序转换为汇编”

Perspective:With all the people today attempting to tear down and get rid of general purpose computing for the general public, it's imperative that we teach the new students the concepts of how to build a general purpose turing machine from core principles, on up through the bare metal, then finally assemblers and programming languages.

观点:当今所有人都试图拆除和摆脱面向公众的通用计算,我们必须向新生教授如何从核心原理构建通用图灵机的概念,直到裸机,然后是汇编程序和编程语言。

How does learning assembly aid in programming?99% of computer programs out there are 10 to 100 times slower than they could optimized to be only because programmers don't know what delays are being forced on them by their favorite high level compiler or interpreter.

学习汇编对编程有什么帮助?99% 的计算机程序比他们可以优化的速度慢 10 到 100 倍,这仅仅是因为程序员不知道他们最喜欢的高级编译器或解释器对他们施加了什么延迟。

A thorough understanding of the full stack here means you can coerce your programs to have that coveted property of only taking nanoseconds to do the job at hand. Time == money. So this knowledge of how to shun anything that takes longer than a few nanoseconds to complete saves time, and therefore money.

对这里的完整堆栈的透彻理解意味着您可以强制您的程序具有仅需要几纳秒来完成手头工作的令人垂涎的特性。时间==金钱。因此,了解如何避开需要超过几纳秒才能完成的任何事情的知识可以节省时间,从而节省资金。

https://softwareengineering.stackexchange.com/questions/156722/how-does-learning-assembly-aid-in-programming

https://softwareengineering.stackexchange.com/questions/156722/how-does-learning-assembly-aid-in-programming