Is there a system call to read only one line of file in assembly 8086?


Type the fget of c, which sends to a variable all the characters of the line between the beginning of it and the carriage return character

asked by anonymous 28.06.2017 / 09:49

1 answer


depends on the system, in case the system calls or syscall are usually linked to a certain operating system and not necessarily to an architecture like x86 (sometimes kkk sometimes), see for example syscall open (5) and read (4) of the int80 in the linux system, you can read a number of bytes in a file by it, with this it is possible to create a generic fgets (you can read byte by byte until you find the line break character would be an alternative) / p>

global _start

section .text
mov eax,5 ;syscall open
mov ebx,arq ;stackoverflow.txt
mov ecx,0  ;r
mov edx,0  ;permissão 000
int 0x80

mov [descritor],eax

mov ebx,[descritor] ;move o descritor para ebx
mov eax,0x3 ;syscall do read 
mov ecx,kodo;joga na variavel kodo
mov edx,0x1 ;ler 1bytes do arquivo
int 0x80

mov ebx,1 ;descritor stdout
mov eax,0x4 ;syscall do write 
mov ecx,kodo;joga na variavel kodo
mov edx,0x1 ;ler 1bytes do arquivo
int 0x80

mov edx,[kodo] ;ler o endereço onde foi armazenado
cmp edx,0xa ;se for igual 0xa
je sair ;pula para sair

jmp repetir ; se nao volta a repetir

mov eax,0x1 ;syscall do exit
mov ebx,0x0
int 0x80

section .data
arq: db "stackoverflow.txt"

section .bss
kodo: resb 200
descritor: resb 4

You can also use the libc APIs, so you can use the functions fgets, printf, scanf and etc, see an example opening a file with fopen reading with fgetc and displaying with putchar

;nasm -f elf kodo.asm -o kodo.o
;gcc kodo.o -o kodo.out

global main
extern putchar
extern fgetc
extern fopen
extern exit

section .text


push arq_tipo
push arq
call fopen

push eax ;esse é o descriot passado pelo fopen
call fgetc

push eax ;joga o retorno do getc na pilha
call putchar

push 0x0
call exit

section .data
arq: db "stackoverflow.txt",0x0
arq_tipo: db "r",0x0

Without saying that many functions like fgets are created by lower calls like read and some others, so much so that if you look in strace on the part he calls fgets and putchar will realize that they are open, read and write

28.06.2017 / 12:15