Seeking Help: Instruction Drift in Assembly Similar to Java’s Input Stream Issues

Hello everyone, I am very happy that you are reading this. It’s okay if you can’t help me, but if possible, please take a little time to assist me.

My assembly program needs to ask the following:

%define BUFFER_READ_WRITE 3000

section .bss
    buffer_str resb BUFFER_READ_WRITE          ; reserve buffer for string input
    buffer_filename resb BUFFER_READ_WRITE     ; reserve buffer for filename input
    function_choice resb 1                     ; contains one digit for function selection

section .data
    str_prompt db 'Enter string: ', 0          ; prompt for entering a string
    str_prompt_len equ $-str_prompt            ; length of the prompt

    filename_prompt db 'Enter filename: ', 0   ; prompt for entering a filename
    filename_prompt_len equ $-filename_prompt  ; length of the filename prompt

    function_menu db '1. Enter and edit file', 10, '2. Exit', 10, 0   ; function menu
    function_menu_len equ $-function_menu      ; length of the function menu

    error_open_msg db 'Failed to open file', 10, 0     ; error message for file open failure
    error_open_len equ $-error_open_msg        ; length of the open file error message

    error_write_msg db 'Failed to write to file', 10, 0   ; error message for file write failure
    error_write_len equ $-error_write_msg      ; length of the write file error message

section .text
    global _start

_start:
    loop:
        ; Print function menu
        mov rsi, function_menu
        mov rdx, function_menu_len
        call print

        ; Get function selection from user
        mov rsi, function_choice
        mov rdx, 1
        call get_input

        ; Compare and branch based on function selection
        cmp byte [function_choice], '1'
        je write_file_function

        cmp byte [function_choice], '2'
        je exit_program
        jmp loop

; Function implementations
exit_program:
    ; Exit program
    mov rax, 60
    xor rdi, rdi
    syscall

print:
    ; Print string
    mov rax, 1
    mov rdi, 1
    syscall
    ret

get_input:
    ; Get string input
    mov rax, 0
    mov rdi, 0
    syscall
    ret

write_file_function:
    ; Get filename from user
    mov rsi, filename_prompt
    mov rdx, filename_prompt_len
    call print
    mov rsi, buffer_filename
    mov rdx, BUFFER_READ_WRITE
    call get_input

    ; Get string to write to file
    mov rsi, str_prompt
    mov rdx, str_prompt_len
    call print
    mov rsi, buffer_str
    mov rdx, BUFFER_READ_WRITE
    call get_input

    ; Write string to file
    mov rsi, buffer_str
    mov rdx, BUFFER_READ_WRITE
    mov rdi, buffer_filename
    call write_file
    jmp loop

write_file:
    ; Open -> write -> close file
    ; Open file with write permission
    mov rax, 2                ; syscall number for sys_open
    mov rsi, 577              ; flags (O_WRONLY | O_CREAT | O_TRUNC)
    mov rdx, 0644             ; mode (rwxr--r--)
    syscall
    test rax, rax
    js error_open
    mov r8, rax               ; file descriptor

    ; Write to file
    mov rax, 1                ; syscall number for sys_write
    mov rdi, r8               ; file descriptor
    syscall
    test rax, rax
    js error_write

    ; Close file
    mov rax, 3                ; syscall number for sys_close
    mov rdi, r8               ; file descriptor
    syscall
    ret

error_open:
    ; Print file open error message
    mov rsi, error_open_msg
    mov rdx, error_open_len
    call print
    jmp exit_program

error_write:
    ; Print file write error message
    mov rsi, error_write_msg
    mov rdx, error_write_len
    call print
    jmp exit_program

The program above prints a menu and asks the user to press 1 to enter a filename and the content to write to it, or press 2 or any other key to exit. But there is an issue with the write_file_function (Image enter image description here) and the output (quick read, no need to see the image):

Note: The autoasm.sh script is just a file for compiling, linking, and running the binary. It does not affect the output.

oen@oen-Latitude-E7440:~/oEn_file/asm/x64-roadmap/do_tasks/file$ autoasm.sh chuong_trinh.asm 
1. Enter and edit file
2. Exit
1
Enter filename: Enter string: 

Description: If, as required, when the user presses 1, the program should print “Enter filename:” and wait for the user to input, and then proceed to “Enter string:”, but somehow it prints both prompts immediately. I don’t know how to handle this. It seems similar to an instruction drift error in Java, but I’m not sure if that’s the case. I hope you can help or provide some suggestions. Thank you for reading.

I have tried:

  1. Adding a loop to separate the code segments for entering the filename and the content into the file, but it didn’t
  2. I have modified it as follows, but it didn’t work:
write_file_function:
    .loop:
        ; Get filename from user
        mov rsi, filename_prompt
        mov rdx, filename_prompt_len
        call print
        mov rsi, buffer_filename
        mov rdx, BUFFER_READ_WRITE
        call get_input

        mov al, byte [buffer_filename]
        cmp al, 0
        je .loop
        jne .write_to_file

    .write_to_file:
        ; Get string to write to file
        mov rsi, str_prompt
        mov rdx, str_prompt_len
        call print
        mov rsi, buffer_str
        mov rdx, BUFFER_READ_WRITE
        call get_input

    ; Write string to file
    mov rsi, buffer_str
    mov rdx, BUFFER_READ_WRITE
    mov rdi, buffer_filename
    call write_file
    jmp loop```

New contributor

Lê Minh Hoàng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật