0

I'm new in assembly programming and I have an assignment in which I have to read a text file line by line and use what is written in the file and pass it to another function. My problem is that I'm not sure of how to read the text this way because from what I have discovered for reading a text file first I have to create a buffer reserving certain quantity of bytes for storing what is in the file. Howerver in this case I want to read line by line (like a loop) until the end of file so I dont know how much bytes I have to reserve. Thanks.

Btw here is the code I'm trying to use:

SECTION .data

file_name db 'instruct.txt',0

SECTION .bss

fd_out resb 1
fd_in  resb 1
info resb  20

SECTION .text

global main        

main:   ;tell linker entry point
push ebp
mov ebp, esp
push ebx
;open the file for reading
mov eax, 5
mov ebx, file_name
mov ecx, 2       
mov edx, 0777       ;read, write and execute by all
int  0x80
mov  [fd_in], eax
loop:  
;read from file
mov eax, 3
mov ebx, [fd_in]
mov ecx, info
mov edx, 5
int 0x80
cmp eax, 0
  ;check EOF  
je exit
; print the info 
mov eax, 4
mov ebx, 1
mov ecx, info
mov edx, 5
int 0x80
; 
jmp loop 
mov eax,1  ;system call number (sys_exit)
int 0x80   ;call kernel
exit:
; close the file
mov eax, 6
mov ebx, [fd_in]
pop ebx
mov esp, ebp
pop ebp
ret
David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
Ricardo Muñoz
  • 27
  • 1
  • 3
  • 9
  • 1
    Easy way, but very slow, read a byte at a time. Better, read BUFSIZ at a time and process it, until done. I like `BUFSIZ equ 10000h` - old dos-head thinks 64k is a nice round number. Try a small value to make sure you're handling end of buffer correctly. When `read` returns zero, you're done. Best(?), but perhaps complicated for a beginner, `mmap` the file. – Frank Kotler Nov 15 '14 at 18:42
  • @FrankKotler the main problem I have is that I dont know how to continue reading from the point in which I stopped last time. What I mean is that every time a try to read the next line or byte it starts reading the file from the beginning – Ricardo Muñoz Nov 15 '14 at 22:19

1 Answers1

0

use mmap syscall, read entire file to memory and search for 0x0A sequence. This is the ASCII code for end of line. Perhaps usefull too to check for 0x0D (in case you are dealing with windows text files. There the sequence 0x0A,0x0D indicates an new line and thus an end of line. mmap will try to allocate memory for you without the overhead of administration for you. Otherwise determine the file length and reserve memory with syscall sbrk. Works also but you have to program a bit more. My suggestion is that mmap is the best way.

Agguro
  • 348
  • 3
  • 11
  • While I agree with allocating memory with mmap, arbitrarily reading the whole file into RAM is not a great idea. This creates a very fragile solution. What happens when someone uses the code to open a 400 gigabyte file? – David Hoelzer Apr 30 '15 at 11:48
  • http://stackoverflow.com/questions/7222164/mmap-an-entire-large-file maybe this link can help you further. – Agguro May 12 '15 at 12:20