NASM在输出中混合了字符串

时间:2017-10-14 17:29:39

标签: assembly 64-bit nasm

我正在尝试从我在此处获取的教程Assembly - File Management中创建一个简单的“使用提示创建文件”代码。但每当我输入内容时,终端中的输出字符串将被混合并切割在一起。而且要创建的文件也是混合的。

代码在这里:

Masukkan nama Anda
Jack
ck
e telah dibuat

如果输入“Jack”

,终端就是这样
Masukkan nama Anda
Jack 
File telah dibuat

它应该如何

Jack e telah dibuat

文件名是

Jack

它应该如何

section .data
Msg1: db 'Masukkan nama Anda',0xa
Msg1ln equ $-Msg1

Name: times 23 db ' ',0

msg_done: db 'File telah dibuat ', 0xa
;msg_doneln equ $-msg_done

fd dq 0

section .text
global _start         

_start:                  

; Output  'Masukkan nama Anda '
mov rax, 1              ; write…
mov rdi, 0              ; to the standard output (screen/console)…
mov rsi, Msg1           ; the information at memory address prompt
mov rdx, Msg1ln         ; 19 bytes (characters) of that information
syscall                 ; Interrupt buat 64bit Linux adalah syscall, sedangkan 32bit int 0x80

; Accept input and store the user’s name
mov rax, 0              ; read…
mov rdi, 1              ; from the standard input (keyboard/console)…
mov rsi, Name               ; storing at memory location name…
mov rdx, 23                 ; 23 bytes (characters) is ok for my name
syscall

;create the file
mov  rax, 85
mov  rdi, Name
mov rsi,777o                ;Permission tipe data oktal -rwxrwxrwx
syscall

mov [fd], rax

;write the message indicating end of file write
mov rax, 1
mov rdi, 1
mov rsi, msg_done
mov rdx, 18
syscall

mov  [fd], rax


mov    rax, 60
mov    rdi, 0
syscall

对不起,我是大会的新手。 现在我还在尝试围绕eax,ebx编辑。如果我知道的话会发布。 非常感谢!

更新 看起来我使用32位代码进行64位汇编。所以我改变了大部分语法(但问题不是这个)。最终的代码有效(感谢底层的那个人)。

{{1}}

1 个答案:

答案 0 :(得分:2)

鉴于你的内存布局是这样的

..., ' ', 0xA, 'F', 'i', 'l', 'e', ' ', 't', 'e', ... 

Name指向第一个' 'msg_done指向'F'

一旦你在Name指定的地址存储了23个字节,那么msg_done指向被Name"覆盖的地方就有& #34;只有2个字节。

要纠正您的问题,您可以使用此功能,假设您的最大长度将保持在23个字符 - 它基本上说"定义在此位置初始化为' '的23个字节,也可以通过{ {1}}"

Name