2017-10-14 97 views
1

我正在试图在此处提取的教程中提供一个简单的“用提示创建文件”代码Assembly - File Management。但每次输入内容时,终端中的输出字符串都会混合在一起并剪切。而且要创建的文件也是混合的。NASM在输出中混合字符串

代码是在这里:

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 

终端是这样的,如果我输入 “杰克”

Masukkan nama Anda 
Jack 
ck 
e telah dibuat 

应该如何

Masukkan nama Anda 
Jack 
File telah dibuat 

和文件名是

Jack e telah dibuat 

应该如何

Jack 

对不起,我是新来的大会。 现在我仍在尝试编辑eax,ebx的东西。如果我知道什么,会发布。 非常感谢!

更新 它看起来像我用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 
+1

您只能在'Name'处为单个字符分配足够的空间。你需要更多。 – Jester

回答

2

鉴于你的内存布局是这样

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

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

一旦储存23个字节与在读通过Name指定的地址,其中msg_done指向与该数据自Name被改写为好地方“有”只有2个字节。

要纠正你的问题,你可以使用这个,假设你的最大长度将保持在23个chracters - 它基本上是说

Name: times 23 db ' ' 
“在这个位置上,也将通过 Name是可达定义初始化为 ' ' 23个字节”
+0

这只是为其余问题工作!但是,我不明白你的最后一行。它仍然将msg_done写入输出文件(文件名合并) –

+0

感谢您指出,该部分答案在您的代码方面不正确。我已经删除它。在名称后面添加的空终止符可以解决问题。 –