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

时间:2010-11-23 02:32:38

标签: linux ubuntu nasm assembly

我正在使用ubuntu 64位并尝试在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

3 个答案:

答案 0 :(得分:37)

这似乎可能是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停止抱怨输入文件。

答案 1 :(得分:12)

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

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

答案 2 :(得分:4)

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

安装nasm:

sudo apt-get install nasm

保存文件名为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

编译:

nasm -f elf64 hello.asm

关联它:

ld -s -o hello hello.o

运行

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

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

观点:今天所有的人都试图拆除并为公众摆脱通用计算,我们必须教新学生如何建立一个通用的概念图灵机从核心原理,直到裸机,最后是汇编程序和编程语言。

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

这里对完整堆栈的透彻理解意味着你可以强迫你的程序拥有只需几纳秒就能完成手头工作的令人垂涎的属性。时间==钱。所以关于如何避免任何花费超过几纳秒的任何事情的知识可以节省时间,从而节省资金。

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