I'm having a problem trying to open a file and read from it. The user is prompted for the file name.
The program compiles without error but doesn't show anything. When I hardcode the filename in the .data section it runs fine, but when I get the filename from the user it fails to read the file. Where am I doing wrong? I can't find any error.
Output using hardcoded name: welcome
Output when user enters the name: ���
Here is my code:
section .data
promptUsr db 'enter a file name:',0xA,0xD
lenPrompt equ $-promptUsr
info db 1
;file_name db 'data.txt' (NOTE:harcoded name works when used)
section .bss
fd_in resb 1
buffer resb 7
file_name resb 20
section .text
global _start
_start:
;prompt user to enter a file name
mov eax,4 ;sys_write
mov ebx,1 ;stdout
mov ecx,promptUsr
mov edx,lenPrompt
int 0x80
;read filename (NOTE:when user enters the same name 'data.txt',this is the output:���)
mov eax,3
mov ebx,2
mov ecx,file_name ;(NOTE:tried using 'dword[file_name]',doesnt work)
mov edx,20
int 0x80
;open file
mov eax,5
mov ebx,file_name ;(NOTE:also tried using 'dword[file_name]',doesnt work too)
mov ecx,2 ;read n write
mov edx,7777h ;all file permissions
int 0x80
mov [fd_in],eax
;read 7 bytes of the file
mov eax,3
mov ebx,[fd_in]
mov ecx,buffer
mov edx,7
int 0x80
;close the file
mov eax,6
int 0x80
;print out what was read
mov eax,4
mov ebx,1
mov ecx,buffer
mov edx,7
int 0x80
;end program
mov eax,1
int 0x80