Is there a system call to read just one file line on Assembly 8086?

Asked

Viewed 181 times

1

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

1 answer

2

depends on the system, in case the system calls or syscall are usually linked to a particular operating system and not necessarily to an architecture like x86 (sometimes depedendo sim kkk), see for example syscall open (5) and read (4) from int80 on the linux system, Voce can read a number of bytes in a file for it, so it is possible to create a Generico fgets (Voce can read byte per byte until finding the line break character would be an alternative)

global _start

section .text
_start:
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

repetir:
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

sair:
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

Voce can also use the libc API, with this it is possible to 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

main:

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 at the strace in the part that he calls the fgets and putchar will realize that are open, read and write

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.