Skip to content

Instantly share code, notes, and snippets.

@biern
Created May 5, 2011 10:54
Show Gist options
  • Save biern/956867 to your computer and use it in GitHub Desktop.
Save biern/956867 to your computer and use it in GitHub Desktop.
Niech ryczy z bólu ranny łoś,
Zwierz zdrów przebiega knieje.
Ktoś nie śpi, aby spać mógł ktoś,
To są zwyczajne dzieje.
.equ kernel, 0x80 #Linux system functions entry
.equ create, 0x08 #create file function
.equ open, 0x05 #open file function
.equ close, 0x06 #close file function
.equ read, 0x03 #read data from file function
.equ write, 0x04 #write data to file function
.equ exit, 0x01 #exit program function
.equ mode, 0x180 #attributes for file creating
.equ flags, 0 #attributes for file opening
.equ stdout, 1
.equ stderr, 2
.equ tooval, 1
.equ errval, 2
.data
file_n:
.string "czytaj.txt"
file_h:
.long 0
buffer:
.space 128, 0
buffer_len:
.long ( . - buffer)
b_read:
.long 0
file_error_msg: #file error message
.ascii "File error!\n"
file_error_msg_len:
.long ( . - file_error_msg )
.text
.global _start
_start:
## 1) Otwieramy plik
MOVL $open, %eax
MOVL $file_n, %ebx
MOVL $flags, %ecx
MOVL $mode, %edx
INT $kernel
MOVL %eax, file_h
## Sprawdzanie błędów przy otwarciu
CMP $0, %eax
JL error # $0 < %eax (jump less)
## 2) Czytanie i wypisywanie w pętli
read_loop:
MOVL $read, %eax #read function
MOVL file_h, %ebx #file handle in EBX
MOVL $buffer, %ecx #ECX points to data buffer
MOVL buffer_len, %edx #bytes to be read
INT $kernel
## Sprawdzanie błędu odczytu
CMP $0,%eax
JL error # if EAX < 0 then something went wrong
## Wypisywanie wczytanej zawartości
MOVL %eax, b_read
MOVL $write, %eax #write function
MOVL $stdout, %ebx #file handle in EBX
MOVL $buffer, %ecx #offset to first character
MOVL b_read, %edx #count of characters
INT $kernel
## Czy wczytano cały plik?
## warunek stopu - liczba wczytanych bajtów jest inna niż
## rozmiar bufora
MOVL b_read, %eax
CMPL buffer_len, %eax
JAE read_loop # %eax >= bufsize, czyli jeszcze nie cały plik
## Wczytano cały
## 3) Zamykanie pliku
MOVL $close, %eax #close function
MOVL file_h, %ebx #file handle in EBX
INT $kernel
CMP $0,%eax
JL error #if EAX<0 then something went wrong
MOVL $0, %ebx
JMP quit
## Błąd pliku
error:
MOVL $write, %eax #write function
MOVL $stderr, %ebx #file handle in EBX
MOVL $file_error_msg, %ecx #ECX points to file error message
MOVL file_error_msg_len, %edx
INT $kernel
MOVL $errval, %ebx
## Koniec
quit:
MOVL $exit,%eax #exit program function
INT $kernel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment