flat assembler
Message board for the users of flat assembler.
Index
> Linux > read from file numbers ... |
Author |
|
ProphetOfDoom 30 Dec 2011, 11:57
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. |
|||
30 Dec 2011, 11:57 |
|
catafest 31 Dec 2011, 08:21
I will test it today.
I was thinking the source code in assembler, but I use the example with functions. Thank you. Regards. |
|||
31 Dec 2011, 08:21 |
|
catafest 07 Jan 2012, 10:17
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. |
|||
07 Jan 2012, 10:17 |
|
ProphetOfDoom 07 Jan 2012, 11:25
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 |
|||
07 Jan 2012, 11:25 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.