unable to read from file when user provides filename (x86 assembly program using nasm) - linux

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

To add to what Michael says... 1) fd_in is too small - make it resd 1. 2) sys_read doesn't return a zero-terminated string, and sys_open wants one.
mov byte [ecx + eax - 1], 0
after the sys_read of the filename should zero-terminate it.

Looks like you're trying to read from STDERR:
mov eax,3
mov ebx,2
mov ecx,file_name ;(NOTE:tried using 'dword[file_name]',doesnt work)
mov edx,20
That mov ebx,2 ought to be mov ebx,0 if you want to read from STDIN.
would also love to know how to check first if the file exists before opening it
If you use sys_open without the O_CREAT flag set, it should fail and return -1 if the file doesn't already exist. You could also use access (syscall 33) or stat (syscall 18) if you want to.

Related

Assembly on Linux: unexpected behaviour from Assembly [duplicate]

This question already has answers here:
In NASM labels next to each other in memory are printing both strings instead of first one
(1 answer)
How does $ work in NASM, exactly?
(2 answers)
Closed 4 years ago.
running the code below generates a file with Welcome to jj Shashwat as content. what i didn't get is why does it writes Shashwat at the end of the file, Shashwat is in a totally different variable. Any idea why does this happen?
section .text
global _start ;must be declared for using gcc
_start:
;create the file
mov eax, 8
mov ebx, file_name
mov ecx, 0777 ;read, write and execute by all
int 0x80 ;call kernel
mov [fd_out], eax
; close the file
mov eax, 6
mov ebx, [fd_out]
;open the file for reading
mov eax, 5
mov ebx, file_name
mov ecx, 2 ;for read/write access
mov edx, 0777 ;read, write and execute by all
int 0x80
mov [fd_out], eax
; write into the file
mov edx,len ;number of bytes
mov ecx, msg ;message to write
mov ebx, [fd_out] ;file descriptor
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
; close the file
mov eax, 6
mov ebx, [fd_out]
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
file_name db 'myfile.txt', 0
msg db 'Welcome to jj', 0
mgafter db ' Shashwat', 0
lntwo equ $-mgafter
len equ $-msg
section .bss
fd_out resb 1
fd_in resb 1
info resb 26
That's because you said len equ $-msg after defining both msg and msgafter, so len is set to the length of both msg and msgafter, making your write call write both strings. This is because len equ $-msg means “set len to be the difference between the current location ($) and the location of msg.”
To fix this, move the len equ $-msg line right after the definition of msg.

Read file in a loop in assembly

I am supposed to write a small assembly "program" that simply reads a file using a loop and outputs the contents to STDOUT.
Now, I made a program that can read the file and unfortunately only outputs the first line. I also can't figure out how to use the loop.
This is my very first time working with assembly and I do it because I have to, not because I want to.
Here's my code:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
;open the file for reading
mov eax, 5
mov ebx, file_name
mov ecx, 0 ;for read only access
mov edx, 0777 ;read, write and execute by all
int 0x80
mov [fd_in], eax
;read from file
mov eax, 3
mov ebx, [fd_in]
mov ecx, info
mov edx, 26
int 0x80
; close the file
mov eax, 6
mov ebx, [fd_in]
; print the info
mov eax, 4
mov ebx, 1
mov ecx, info
mov edx, 26
int 0x80
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
file_name db './passwd'
section .bss
fd_out resb 1
fd_in resb 1
info resb 26

How to read and display the contents of a text file in nasm?

I want to read and display the contents of a text file using nasm and Linux system calls. My text file is named "new.txt". I wrote the following code and am receiving no output on the terminal.
section .data
line db "This is George,",
db " and his first line.", 0xa, 0
len equ $ - line
line2 db "This is line number 2.", 0xa, 0
len2 equ $ - line2
filename: db 'ThisIsATestFile.txt', 0
section .bss
bssbuf: resb len ;any int will do here, even 0,
file: resb 4 ;since pointer is allocated anyway
global _start
section .text
_start:
; open file in read-only mode
mov eax, 5 ;sys_open file with fd in ebx
mov ebx, filename ;file to be opened
mov ecx, 0 ;O_RDONLY
int 80h
cmp eax, 0 ;check if fd in eax > 0 (ok)
jbe error ;can not open file
mov ebx, eax ;store new (!) fd of the same file
; read from file into bss data buffer
mov eax, 3 ;sys_read
mov ecx, bssbuf ;pointer to destination buffer
mov edx, len ;length of data to be read
int 80h
js error ;file is open but cannot be read
cmp eax, len ;check number of bytes read
jb close ;must close file first
; write bss data buffer to stderr
mov eax, 4 ;sys_write
push ebx ;save fd on stack for sys_close
mov ebx, 2 ;fd of stderr which is unbuffered
mov ecx, bssbuf ;pointer to buffer with data
mov edx, len ;length of data to be written
int 80h
pop ebx ;restore fd in ebx from stack
close:
mov eax, 6 ;sys_close file
int 80h
mov eax, 1 ;sys_exit
mov ebx, 0 ;ok
int 80h
error:
mov ebx, eax ;exit code
mov eax, 1 ;sys_exit
int 80h
When you write
jb close ;must close file first
you, in fact, are jumping to close, not calling it.
Once you reach close, you just exit after you close the file:
close:
mov eax, 6 ;sys_close file
int 80h
mov eax, 1 ;sys_exit
mov ebx, 0 ;ok
int 80h
Perhaps you want to either call close in some way (and then ret from close) or jump back to where you were to continue what you were doing?
Also, keep in mind that jbe is an unsigned comparison, so when you write:
cmp eax, 0 ;check if fd in eax > 0 (ok)
jbe error ;can not open file
You in fact will not detect negative numbers. Consider jle instead. (You can consult this handy resource about jumps to use.)

A loop in assembly doesn't work why?

i have problem. I tried build a loop in assembly (nasm,linux). The loop should "cout" number 0 - 10, but it not work and i don't know why. Here is a code :
section .text
global _start
_start:
xor esi,esi
_ccout:
cmp esi,10
jnl _end
inc esi
mov eax,4
mov ebx,1
mov ecx,esi
mov edx,2
int 80h
jmp _ccout
_end:
mov eax,1
int 80h
section .data
Well, the loop is working, but you aren't using the syscall correctly. There are some magic numbers involved here, so let's get that out of the way first:
4 is the syscall number for write
1 is the file descriptor for the standard output
So far, so good. write requires a file descriptor, the address of a buffer and the length of that buffer or the part of it that it's supposed to write to the file descriptor. So, the way this is supposed to look is similar to
mov eax,4 ; write syscall
mov ebx,1 ; stdout
mov ecx,somewhere_in_memory ; buffer
mov edx,1 ; one byte at a time
compare that to your code:
mov eax,4
mov ebx,1
mov ecx,esi ; <-- particularly here
mov edx,2
int 80h
What you are doing there (apart from passing the wrong length) is passing the contents of esi to write as a memory address from which to read the stuff it's supposed to write to stdout. By pure happenstance this doesn't crash, but there's no useful data at that position in memory.
In order to solve this, you will need a location in memory to put it. Moreover, since write works on characters, not numbers, you'll have to to the formatting yourself by adding '0' (which is 48 in ASCII). All in all, it could look something like this:
section .data
text db 0 ; text is a byte in memory
section .text
global _start
_start:
xor esi,esi
_ccout:
cmp esi,10
jnl _end
inc esi
lea eax,['0'+esi] ; print '0' + esi. lea == load effective address
mov [text],al ; is useful here even though we're not really working on addresses
mov eax,4 ; write
mov ebx,1 ; to fd 1 (stdout)
mov ecx,text ; from address text
mov edx,1 ; 1 byte
int 80h
jmp _ccout
_end:
mov [text],byte 10 ; 10 == newline
mov eax,4 ; write that
mov ebx,1 ; like before.
mov ecx,text
mov edx,1
int 80h
mov eax,1
mov ebx,0
int 80h
The output 123456789: is probably not exactly what you want, but you should be able to take it from here. Exercise for the reader and all that.

How to save a file in assembly language with nasm in Linux x86?

I'm just wondering how I can throw a buffer to a file. I know I can set up the registers like:
mov eax, 4
mov ebx, (file descriptor here)
mov ecx, myBuffer
mov edx, myBufferLen
int 80h
and then close the file with:
mov eax, 6
int 80h
but I'm not sure how to get the file descriptor. Someone told me that whenever you open a file, after calling the service dispatcher, the eax has the file descriptor. Whatever I try, it doesn't create a new file or save the current one.
mov eax, 5 ; __NR_open
mov ebx, filename ; zero terminated!
mov ecx, 2 ; O_WRITE? Check me on this!
mov edx, 777q ; permissions - Check me on this, too!
int 80h
; did we succeed?
cmp eax, -4096
ja exit
mov [descriptor], eax
; and so on...
;...
xor eax, eax ; claim no error
exit:
mov ebx, eax
neg ebx
mov eax, 1 ; __NR_exit
int 80h
I'm uncertain on the "magic numbers" for open flags and permissions, and too lazy to look them up right now (fs.h?). You may need to "or" the open flags with O_CREATE to create a new file(?). The trick at "exit:" negates the (negative) ERRNO (if any) so you can read it easily with "echo $?". Something like that...
Thanks to #gnometorule for the solution. So this is how you store a buffer into file:
in this case you are receiving the file name as a first parameter when you run the program.
section .text
global _start
_start:
pop ebx ; this is the amount of parameter received (in this case 2)
pop ebx ; this is the direction of our program
pop ebx ; this is the first parameter (our file name eg. "text.txt")
so we open it and move it to a buffer to edit or whatever we want with it.
textEdit:
;we open the file
mov eax, 5
push ebx ; we save the file name in the stack for later use
mov ecx,0
int 80h
; and then we move the information read to buffer
mov eax, 3
mov ebx, eax ; really dont know how this works, but it does.
mov ecx, fileBuffer
mov edx, fileBufferSize
int 80h
now fileBuffer (wich is a buffer defined in section .bss) has what we had in the file we opened. Here you can edit the information. When we want to save it back to the file it goes like this:
saveFile:
;open the file
mov eax, 5
pop ebx ; the file name was stored in the stack,
push ebx ; so we retrieve it and save it back for later use
mov ecx, 2 ; ecx has the access mode ("2"aloud us to read/write)
mov edx, $0666
int 80h
;now we are going to write to the file
mov ebx, eax ; eax has the file descriptor we opened. so we move it to ebx.
mov eax, 4 ; we are going to use the write service (#4)
mov ecx, fileBuffer ; ecx has the adress of what we want to write
mov edx, fileBufferSize ; edx has the amount of bytes we want to write
int 80h
; close the file
mov eax, 6
int 80h
jmp exit ; jump wherever you want, or end your program... etc.
This worked for me.

Resources