flat assembler
Message board for the users of flat assembler.

Index > Linux > read from file numbers ...

Author
Thread Post new topic Reply to topic
catafest



Joined: 05 Aug 2010
Posts: 120
catafest
Hi ...
I try understand how is best way to read from file under linux .
The file is like the example bellow :
Code:
12 23
23 34
45 22 
34 45 
    

Also is need to use this infos in a loop .
Thank you for help .
Regards .
Post 27 Dec 2011, 11:36
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom
Hullo. This is using libc functions (I don't know any other way to do it). It loops through all the integers in "test.txt" and prints them to the console.

Code:
format elf

extrn 'fopen' as fopen:dword
extrn 'feof' as feof:dword
extrn 'fscanf' as fscanf:dword
extrn 'printf' as printf:dword
extrn 'exit' as exit:dword

public main

section '.data' writable

file_ptr rd 1
integer rd 1
file_name db "test.txt",0
mode db "r",0
fmt_in db " %d ",0
fmt_out db "Read number %d",10,0

section '.text' executable

main:

push mode
push file_name
call fopen
add esp, 8
mov [file_ptr], eax

.loop_start:

push [file_ptr]
call feof
add esp, 4
cmp eax, 0
jnz .exit_loop

push integer
push fmt_in
push [file_ptr]
call fscanf
add esp, 12

push [integer]
push fmt_out
call printf
add esp, 8

jmp .loop_start

.exit_loop:

push 0
call exit
    


To assemble, link, run:

Code:
fasm read.asm
gcc read.o -lc
./a.out
    


Note: It fails for an empty file. I believe you can determine the size of the file using other libc calls but that would need a fair bit more code.
Post 30 Dec 2011, 11:57
View user's profile Send private message Reply with quote
catafest



Joined: 05 Aug 2010
Posts: 120
catafest
I will test it today.
I was thinking the source code in assembler, but I use the example with functions.
Thank you. Regards.
Post 31 Dec 2011, 08:21
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
catafest



Joined: 05 Aug 2010
Posts: 120
catafest
The code is working well . I have a question about the compiling file.
Why the output is named with "a.out" ?
How i set the name of output file ? (gcc)
Thank you.
Post 07 Jan 2012, 10:17
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom
Hi,

I believe it's called a.out by convention because originally in Unix it was the "assembler output" i.e. the "output from the assembler" (tho not in this case obviously). Also old Unix systems tended to be scheduled to delete all files called a.out once a day, so it was a way of letting the system know the file is not needed.

To give a different name use the -o option.

Code:
gcc read.o -o name -lc
    


Happy coding Smile
Post 07 Jan 2012, 11:25
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.