繁体   English   中英

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

[英]Error when trying to run .asm file on NASM on Ubuntu

我正在使用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

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

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

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

如何在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM