1

I'm trying to make a simple "create file with prompt" code from the tutorial I fetched here Assembly - File Management. But everytime I input something, the output strings in the terminal will be mixed and cut together. And the file that would be created is mixed too.

Code is here:

    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

The terminal is like this if I input "Jack"

Masukkan nama Anda
Jack
ck
e telah dibuat

How it should be

Masukkan nama Anda
Jack 
File telah dibuat

And the file name is

Jack e telah dibuat

How it should be

Jack

Sorry, I'm new to the Assembly. Now I'm still trying editing around the eax,ebx things. Will post if I know something. Great thanks!

Update It looks like I was using 32bit code for 64bit assembly. So I changed most of the syntax (But the problem is not this). Final code worked (thanks to that guy on the bottom).

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
  • 1
    You only allocated enough for a single character at Name. You need more. Commented Oct 14, 2017 at 17:33

1 Answer 1

2

Given your memory layout is like this

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

Where Name points to the first ' ' and msg_done points to the 'F'.

Once you store 23 bytes with that read at the address specified by Name, the place where msg_done points to get overwritten with that data as well since Name "has" only 2 bytes.

To correct your issue, you can use this, assuming your max length will stay at 23 chracters - it basically says "define 23 bytes initialized to ' ' at this location that will also be reachable via Name"

Name: times 23 db ' '
Sign up to request clarification or add additional context in comments.

2 Comments

That just worked for the rest problem! But, I don't understand your last line. It still write msg_done into output file (file name merged)
Thanks for pointing that out, that part of answer was incorrect with respect to your code. I have removed it. Your null-terminator added behind Name that never gets overwritten solves the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.