2015-09-13 95 views
0

我只是在学习大会。当我使用在线汇编程序时,它会打印出“Hello,world!”如预期。但是,当我使用我刚安装的nasm时,我只会打个招呼。为什么会发生?印刷“你好”而不是“你好,世界!”

section .text 
    global _start  ;must be declared for using gcc 
_start:      ;tell linker entry point 
    mov edx, len ;message length 
    mov ecx, msg ;message to write 
    mov ebx, 1  ;file descriptor (stdout) 
    mov eax, 4  ;system call number (sys_write) 
    int 0x80  ;call kernel 
    mov eax, 1  ;system call number (sys_exit) 
    int 0x80  ;call kernel 

    section .data 

    msg db 'Hello, world!',0xa ;our dear string 
    len equ $ - msg   ;length of our dear string 
+0

不能重现我的机器(X86-64的Linux)上。 – Downvoter

+0

你是如何组装这段代码的? – SirPython

+0

我正在使用nasm文件名,我的机器也是x86-64 linux(vm),并且正在运行./a.out – Erk

回答

0

正如你在评论中解释,你组装这样的代码:

nasm helloWorld.asm 

这样做的问题是,

  1. 您还没有组装它像一个可执行文件

  2. 如果对于可执行文件进行汇编,则没有将其链接

在Linux下,您将将此代码组装到一个ELF文件中。为了做到这一点,运行这些命令:

nasm -f elf helloWorld.asm 
ld -m elf_i386 -s -o helloWorld helloWorld.o 

第一个命令的代码装配成一个对象(.o)文件被内置到ELF文件。第二个命令采用该目标文件并将其转换为ELF(可执行)文件。

要运行它,输入:

./helloWorld 
+0

好吧,我写的最后一个程序必须打印出hello, 。 – Erk