NASM 输出中的混合字符串
NASM mixed string in output
我正在尝试根据我在此处 Assembly - File Management 获取的教程制作一个简单的 "create file with prompt" 代码。但是每次我输入一些东西时,终端中的输出字符串都会被混合并剪切在一起。并且将创建的文件也是混合的。
代码在这里:
section .data
Msg1: db 'Masukkan nama Anda ',0xa
Msg1ln equ $-Msg1
Name: db ' ', 0xa ; space characters
msg_done: db 'File telah dibuat ', 0xa
;msg_doneln equ $-msg_done
section .text
global _start
_start:
; Output 'Masukkan nama Anda '
mov eax, 4 ; write…
mov ebx, 1 ; to the standard output (screen/console)…
mov ecx, Msg1 ; the information at memory address prompt
mov edx, Msg1ln ; 19 bytes (characters) of that information
int 0x80 ; invoke an interrupt
; Accept input and store the user’s name
mov eax, 3 ; read…
mov ebx, 1 ; from the standard input (keyboard/console)…
mov ecx, Name ; storing at memory location name…
mov edx, 23 ; 23 bytes (characters) is ok for my name
int 0x80
;create the file
mov eax, 8
mov ebx, Name
mov ecx, 0777 ;read, write and execute by all
int 0x80 ;call kernel
mov [fd_out], eax
;write the message indicating end of file write
mov eax, 4
mov ebx, 1
mov ecx, msg_done
mov edx, 18
int 0x80
mov [fd_in], eax
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .bss
fd_out resb 1
fd_in resb 1
输入"Jack"
终端是这样的
Masukkan nama Anda
Jack
ck
e telah dibuat
应该怎样
Masukkan nama Anda
Jack
File telah dibuat
文件名为
Jack e telah dibuat
应该怎样
Jack
抱歉,我是 Assembly 的新手。
现在我仍在尝试围绕 eax、ebx 进行编辑。会 post 如果我知道点什么。
非常感谢!
更新
看起来我正在为 64 位程序集使用 32 位代码。所以我更改了大部分语法(但问题不在于此)。最终代码有效(感谢底部的那个人)。
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
鉴于你的内存布局是这样的
..., ' ', 0xA, 'F', 'i', 'l', 'e', ' ', 't', 'e', ...
其中 Name
指向第一个 ' '
而 msg_done
指向 'F'
.
一旦您在 Name
指定的地址存储了 23 个读取的字节,msg_done
指向的位置也会被该数据覆盖,因为 Name
"has"只有2个字节。
要更正您的问题,您可以使用它,假设您的最大长度将保持在 23 个字符 - 它基本上表示 "define 23 bytes initialized to ' '
at this location that will also be reachable via Name
"
Name: times 23 db ' '
我正在尝试根据我在此处 Assembly - File Management 获取的教程制作一个简单的 "create file with prompt" 代码。但是每次我输入一些东西时,终端中的输出字符串都会被混合并剪切在一起。并且将创建的文件也是混合的。
代码在这里:
section .data
Msg1: db 'Masukkan nama Anda ',0xa
Msg1ln equ $-Msg1
Name: db ' ', 0xa ; space characters
msg_done: db 'File telah dibuat ', 0xa
;msg_doneln equ $-msg_done
section .text
global _start
_start:
; Output 'Masukkan nama Anda '
mov eax, 4 ; write…
mov ebx, 1 ; to the standard output (screen/console)…
mov ecx, Msg1 ; the information at memory address prompt
mov edx, Msg1ln ; 19 bytes (characters) of that information
int 0x80 ; invoke an interrupt
; Accept input and store the user’s name
mov eax, 3 ; read…
mov ebx, 1 ; from the standard input (keyboard/console)…
mov ecx, Name ; storing at memory location name…
mov edx, 23 ; 23 bytes (characters) is ok for my name
int 0x80
;create the file
mov eax, 8
mov ebx, Name
mov ecx, 0777 ;read, write and execute by all
int 0x80 ;call kernel
mov [fd_out], eax
;write the message indicating end of file write
mov eax, 4
mov ebx, 1
mov ecx, msg_done
mov edx, 18
int 0x80
mov [fd_in], eax
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .bss
fd_out resb 1
fd_in resb 1
输入"Jack"
终端是这样的Masukkan nama Anda
Jack
ck
e telah dibuat
应该怎样
Masukkan nama Anda
Jack
File telah dibuat
文件名为
Jack e telah dibuat
应该怎样
Jack
抱歉,我是 Assembly 的新手。 现在我仍在尝试围绕 eax、ebx 进行编辑。会 post 如果我知道点什么。 非常感谢!
更新 看起来我正在为 64 位程序集使用 32 位代码。所以我更改了大部分语法(但问题不在于此)。最终代码有效(感谢底部的那个人)。
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
鉴于你的内存布局是这样的
..., ' ', 0xA, 'F', 'i', 'l', 'e', ' ', 't', 'e', ...
其中 Name
指向第一个 ' '
而 msg_done
指向 'F'
.
一旦您在 Name
指定的地址存储了 23 个读取的字节,msg_done
指向的位置也会被该数据覆盖,因为 Name
"has"只有2个字节。
要更正您的问题,您可以使用它,假设您的最大长度将保持在 23 个字符 - 它基本上表示 "define 23 bytes initialized to ' '
at this location that will also be reachable via Name
"
Name: times 23 db ' '