程序集

时间:2017-01-04 18:50:09

标签: ubuntu assembly nasm

我试图在程序集(NASM 2, Linux)中编写一个简单的代码来创建文本文件并在其中写入内容。但是在运行下面的代码时,我发现文件名不是我在代码中提到的,它与文件的内容连接在一起。即。

它生成一个名为file.txtHello World的文件,其中Hello World是文件的内容。

section .data
    file_name db 'file.txt'

    msg       db 'Hello World', 0xa
    len equ $-msg

section .bss
    fd_out resb 1

section .text
    global _start           ;must be declared for using gcc
_start:                     ;tell linker entry point

    ;Create the file
    mov ebx, file_name
    mov ecx, 777            ;(read write execute = 111 = 7) by all (owner, group, and others)
    mov eax, 8              ;system call number (sys_creat)
    int 0x80                ;call kernel
    mov [fd_out], byte eax

    ;Write into the file
    mov ebx, [fd_out]       ;file descriptor 
    mov ecx, msg            ;message to write
    mov edx, len            ;number of bytes
    mov eax, 4              ;system call number (sys_write)
    int 0x80                ;call kernel

    ;Close the file
    mov eax, 6              ;system call number (sys_close)
    int 0x80                ;call kernel

    mov eax,1               ;System call number (sys_exit)
    int 0x80                ;Call kernel

我不知道,为什么会发生这种情况

1 个答案:

答案 0 :(得分:2)

文件名必须为零:

file_name db 'file.txt',0
相关问题