I am learning to use only system calls to open, read, and write in assembly language. I'm trying to open a simple text file called "file1.txt", read the file and write the contents to another file called fileOUT.txt. With the help of some of you I've been able to get the code to run without any obvious errors, but it won't actually write to the fileOUT file. I'm running x86 on linux. I did run a strace on the program. It appears it doesn't like the address of the write file fileOUT though I'm not sure why. Here's a link to the strace and the code I'm trying to run.
https://i.stack.imgur.com/bYplM.png --strace, not allowed to paste yet
section .data
readFile: db "file1.txt", 0
len: db 15
writeFile: db "fileOUT.txt", 0
section .bss
fd_outfile: resb 4
fd_infile: resb 4
buffer: resb 15
section .text
global main
main:
;start stack
push ebp
mov ebp, esp
; open file1.txt
mov eax, 5
mov ebx, readFile
mov ecx, 0
mov edx, 0777
int 80h
;move file descriptor to variable
mov [fd_infile], eax
;read from file
mov eax, 3
mov ebx, [fd_infile]
mov ecx, buffer
mov edx, len
int 80h
;close file1
mov eax,6
mov ebx,[fd_infile]
int 80h
; open fileOUT.txt
mov eax, 5
mov ebx, writeFile
mov ecx, 1
mov edx, 0777
int 80h
; moves file descriptor to variable
mov [fd_outfile], eax
; write to fileOUT
mov eax, 4 ;system call number (sys_write)
mov ebx, [fd_outfile] ;file descriptor
mov ecx, buffer ;message to write
mov edx, len ;number of bytes
int 80h
;close fileOUT
mov eax,6
mov ebx,[fd_outfile]
int 80h
; exit program
mov esp, ebp
pop ebp
ret
Editor's note: The error is in mov edx, len ;number of bytes
. This requires the use of square brackets and needs to extend the byte to dword: movzx edx, byte [len]
.
BIG THANKS to Everyone!!!. My program now works and I also learned a lot from all your input.