2010-11-23 48 views
21

我正在使用ubuntu 64位并尝试在NASM上运行.asm文件。但是当我尝试运行下面的代码时它会返回这个错误。什么即时试图做的是从源 $ nasm -f elf hello.asm编译(或组装)目标文件生成可执行文件,然后通过调用连接尝试在Ubuntu上的NASM上运行.asm文件时出错

$ ld -s -o hello hello.o 

这创造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 

回答

29

这看起来可能是一个简单的mism什么是由nasm产生什么ld正在努力使之间ATCH:

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

换句话说,nasm已经产生了32位的目标文件hello.old要采取,使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停止抱怨输入文件。

+0

@psyhclo:这是否对你的工作? – 2010-11-23 03:37:17

+0

这不是真正的方法,因为OP写入的程序集也必须被修改(例如,在系统之前需要清除`%rax`,`%rbx`等的高32位呼叫)。 – caf 2010-11-23 07:16:44

+0

是的@caf,你可能是对的。我自己,我更喜欢改变代码的方法,因为我运行64位,但我可以看到创建一个32位可执行文件将在某些情况下更可取。您的答案可能提供了尽可能少的努力来获取工作,所以这可能是一个教程的最佳途径。所以+1。 – paxdiablo 2010-11-23 07:44:42

9

你需要告诉链接器产生I386的输出文件,因为你写的i386装配:

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

如何编译,链接,并运行在Ubuntu 64位A 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 

运行它

[email protected]:~$ ./hello 
Hello world! 

它的工作原理!现在怎么办?请求您最喜爱的编译器生成汇编代码,该汇编代码通常会被转换为机器代码。 Google搜索:“将php/java/python/C++程序转换为程序集”

Perspective:今天所有的人都在试图拆卸并摆脱普通公众的通用计算,所以我们必须教新的学生将从如何从核心原理,到裸机,然后是最终的汇编程序和编程语言,来构建通用图灵机的概念。

学习装配如何帮助编程? 99%的计算机程序比他们可以优化的慢10到100倍,只是因为程序员不知道他们最喜欢的高级编译器或解释器强加给他们什么延迟。

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

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

相关问题