Skip to content

Instantly share code, notes, and snippets.

@biern
Created May 4, 2011 14:08
Show Gist options
  • Save biern/955278 to your computer and use it in GitHub Desktop.
Save biern/955278 to your computer and use it in GitHub Desktop.
## Wypisanie elementów tablicy do pliku / stdout
## - stałe systemowe -
.equ kernel, 0x80
.equ stdout, 1
.equ exit, 0x01
.equ write, 0x04
.equ create, 0x08 #create file function
.equ close, 0x06 #close file function
## - inne przydatne
.equ zero_str, '0'
.data
table:
.long 10,70,50,90,60,80,40,20,0,30
count:
.long ( . - table) >> 2 # Dzielenie przez 4 (rozmiar long)
item_num:
.string " "
item_desc:
.string ". "
item_value:
.string " \n"
item_end:
ending:
.string "-----------\n"
ending_len:
.long (. - ending)
file_name:
.string "tablica.txt"
file_h:
.long 0
file_create_error:
.ascii "File creation error!\n"
file_create_error_len:
.long ( . - file_create_error )
.equ mode, 0x1FF #attributes for file creating ([000]111|111|111)
.equ item_len, (item_end - item_num)
.text
.global _start
_start:
CALL create_file
CALL disp_table
CALL sort_table_bubble
CALL disp_table
JMP quit
## -Funkcje--------------------------------------------------------------
## Zwraca file handle na końcu stosu
.type create_file, @function
create_file:
MOVL $create, %eax
MOVL $file_name, %ebx
MOVL $mode, %ecx
INT $kernel
NOP
CMPL $0, %eax
JAE _create_file_no_error
## Błąd:
MOVL $write, %eax
MOVL $stdout, %ebx
MOVL $file_create_error, %ecx
MOVL file_create_error_len, %edx
INT $kernel
MOVL $2, %eax
NOP
_create_file_no_error:
MOVL %eax, file_h
RET
## Sortowanie bąbelkowe
.type sort_table_bubble, @function
sort_table_bubble:
## count - argument - liczba elementów
## edx - zewnętrzny licznik
## ecx - wewnętrzny licznik
MOVL count, %edx
sort_table_bubble_outer:
DEC %edx
MOVL %edx, %ecx
XOR %esi, %esi
sort_table_bubble_inner:
PUSH %ecx
PUSH %edx
## Właściwe sortowanie (dla każdego elementu w każdym przebiegu)
## esi - index aktualnego elementu
MOVL table(,%esi,4), %edx
CMPL table+4(,%esi,4), %edx
## Omijamy zamianę elementów
JB sort_table_bubble_noswap
sort_table_bubble_swap:
XCHGL table+4(,%esi,4), %edx
MOVL %edx, table(,%esi,4)
## debug
NOP
MOVL count, %edx
CALL disp_table
NOP
MOVL count, %edx
CALL disp_table
sort_table_bubble_noswap:
INC %esi
POP %edx
POP %ecx
LOOP sort_table_bubble_inner
CMPL $1, %edx
JNZ sort_table_bubble_outer
RET
## ## sortowanie przez wstawianie
## .type sort_table_pick, @function
## sort_table_pick:
## ## edi - licznik zewnętrzny
## ## eci - licznik wewnętrzny
## MOVL count, %edi
## MOVL %edi, %eci
## sort_table_pick_outer:
## DEC %edi
## sort_table_pick_inner:
## Wyświetlanie tableki
.type disp_table, @function
disp_table:
PUSH %esi
XOR %esi, %esi
MOVL count, %ecx
_disp_item:
PUSH %ecx
MOVL table(,%esi,4), %ebx
INC %esi
CALL makestr
MOVL $write, %eax
MOVL file_h, %ebx
MOVL $item_num, %ecx
MOVL $item_len, %edx
INT $kernel
POP %ecx
## Loop działa w oparciu o %ecx (dekrementuje)
LOOP _disp_item
MOVL $write, %eax
MOVL file_h, %ebx
MOVL $ending, %ecx
MOVL ending_len, %edx
INT $kernel
POP %esi
RET
#----------------------------------------------------------------
## eax - liczba
## edi - miejsce w pamięci gdzie liczbę (kod ascii) wrzucić
## tzn tam gdzie liczba ma się skończyć. edi jest dekrementowane!
## przyjmujemy że index tablicy jest w %esi
.type makestr,@function
makestr:
MOV %esi,%eax
MOVL $item_num + 3,%edi
CALL n2str
MOV %ebx,%eax
MOVL $item_value + 4,%edi
CALL n2str
RET
#----------------------------------------------------------------
.type n2str,@function
n2str:
PUSH %ebx
PUSH %edx
MOVL $10,%ebx
nextdig:
XOR %edx,%edx
DIV %ebx
ADDB $zero_str,%dl
MOVB %dl,(%edi)
CMPL $0,%eax
JZ empty
DEC %edi
JMP nextdig
empty:
POP %edx
POP %ebx
RET
#----------------------------------------------------------------
quit:
MOV $exit, %eax
INT $kernel
MOVL $write, %eax
MOVL $stdout, %ebx
MOVL $item_desc, %ecx
MOVL $2, %edx
INT $kernel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment