abc Asembler - elektronski priručnik
XX dio Upravljanje fajlovima
Ažuriranje fajla
Za ažuriranje fajla, treba izvršiti sljedeće zadatke:
- Staviti broj 19 sistemskog poziva sys_lseek (), u EAX registar.
- Staviti fajl deskriptor u EBX registar.
- Staviti offset vrijednost u ECX registar.
- Staviti poziciju reference za offset u EDX registar.
Pozicija reference može biti:
- Početak fajla - vrijednost 0
- Tekuća pozicija - vrijednost 1
- Kraj fajla - vrijednost 2
Sistemski poziv vraća, u slučaju greške, kod greške u EAX registar.
Primjer
Sljedeći program kreira i otvara fajl imena myfile.txt, i upisuje tekst 'Welcome to Banja Luka' u taj fajl. Sljedeće, program čita iz fajla i sprema podatke u bafer imena info. Na kraju, prikazuje tekst koji je spremljen u info.
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
;create the file
mov eax, 8
mov ebx, file_name
mov ecx, 0777 ;read, write and execute by all
int 0x80 ;call kernel
mov [fd_out], eax
; write into the file
mov edx, len ;number of bytes
mov ecx, msg ;message to write
mov ebx, [fd_out] ;file descriptor
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
; close the file
mov eax, 6
mov ebx, [fd_out]
; write the message indicating end of file write
mov eax, 4
mov ebx, 1
mov ecx, msg_done
mov edx, len_done
int 0x80
;open the file for reading
mov eax, 5
mov ebx, file_name
mov ecx, 0 ;for read only access
mov edx, 0777 ;read, write and execute by all
int 0x80
mov [fd_in], eax
;read from file
mov eax, 3
mov ebx, [fd_in]
mov ecx, info
mov edx, 26
int 0x80
; close the file
mov eax, 6
mov ebx, [fd_in]
; print the info
mov eax, 4
mov ebx, 1
mov ecx, info
mov edx, 26
int 0x80
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
file_name db 'myfile.txt'
msg db 'Welcome to Banja Luka'
len equ $-msg
msg_done db 'Upisano u fajl', 0xa
len_done equ $-msg_done
section .bss
fd_out resb 1
fd_in resb 1
info resb 26
Kada se gore navedeni kod kompajlira i izvrši, on će proizvesti sljedeći rezultat:
Upisano u fajl
Welcome to Banja Luka
Zatvaranje fajla < Index > Upravljanje memorijom
|
 |