2017-10-14 1 views
1

나는 여기 Assembly - File Management을 가져온 튜토리얼의 간단한 "create prompt with file"코드를 작성하려고합니다. 하지만 매번 입력 할 때마다 터미널의 출력 문자열이 혼합되어 함께 잘립니다. 그리고 생성 될 파일도 섞여 있습니다.출력에 NASM 혼합 문자열

코드는 여기에 있습니다 : I 입력 "잭"

Masukkan nama Anda 
Jack 
ck 
e telah dibuat 

Masukkan nama Anda 
Jack 
File telah dibuat 

되어야하는지 그리고 파일 이름이

경우 터미널이 같다

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 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', ... 

처럼 어디 'F'에 대한 첫 번째 ' 'msg_doneName 점. 당신이 Name에 의해 지정된 주소로 그 읽기 23 바이트를 저장하면

msg_doneName 이후뿐만 아니라 데이터로 덮어 얻을 수있는 곳은 2 바이트 "가".

문제를 해결하려면, 당신은 23 chracters에 남아있을 것입니다 귀하의 최대 길이를 가정하고, 이것을 사용할 수 있습니다 - 그것은 기본적으로

Name: times 23 db ' ' 
"또한 Name를 통해 도달 할 수있을 것이다이 위치에 ' '으로 초기화 23 바이트를 정의합니다"라고
+0

그건 그냥 나머지 문제를 위해 일했습니다! 그러나 나는 당신의 마지막 줄을 이해하지 못합니다. 여전히 msg_done을 출력 파일에 씁니다. (파일 이름 병합) –

+0

그 점을 지적 해 주셔서 감사합니다. 그 부분은 코드와 관련하여 올바르지 않습니다. 나는 그것을 제거했다. 덮어 쓰지 않는'Name' 뒤에 널 터미네이터를 추가하면 문제가 해결됩니다. –