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:
- Adding a loop to separate the code segments for entering the filename and the content into the file, but it didn’t
- 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```
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.