kornicameister
3rd March 2011, 10:00
recently I've started to learning assembler (using TASM syntax), my first task was to made bubble sort...
I made an implementation, I checked it in Turbo Debugger and elements are in fact replaced if the appropriate condition is met, elements are swapped
but the program runs into infinite loop and I can not notice why is that
here is the entire code
;program do metody babelkowej
.MODEL SMALL
; Algorytm sortowania babelkowego:
; 1. Start.
; 2. index = 0; zamiany = 0;
; 3. Odczytaj dwa sasiadujace elementy z tablicy o pozycjach index oraz
; index + 1; jezeli pierwszy z odczytanych elementow jest wiekszy od
; swojego nastepnika, to zamien elementy miejscami oraz zwieksz zamiany.
; 4. Zwieksz index o jeden.
; 5. Jezeli index < dlugosc_tablicy - 2 to skacz do 3.
; 6. Skacz do 2 jezeli zamiany rozne od zera.
; 7. Stop.
.DATA
t DB 01h, 02h, 00h, 10h, 12h, 33h, 15h, 09h, 11h, 08h, 0Ah, 00h
; 1, 2, 0, 16, 18, 51, 21, 9, 17, 8, 10, 0 ;po sorcie2=> 00h,00h,01h,02h,08h,09h,0Ah,10h,11h,12h,15h,33h
t_size EQU 11 ; t_lenght
again_s db 0 ; if again_s == 1, swap was made, again
.CODE
s:
mov ax,@DATA
mov ds,ax
;buble sort
prepare:
mov bx, OFFSET t ; load the t offset into bx
again:
mov again_s,0 ; XOR the flag
xor si,si ; XOR used indexing register
nextElement:
mov ax,[bx+si] ; first compared element
mov cx,[bx+si+1] ; second one
cmp ax,cx ; comparing
jbe noSwap ; if first <= next jmp no swap
xchg ax,cx ; else swap
mov again_s,1 ; set the flag
noSwap:
inc si ; increment index
cmp si,t_size ; compare the index with t_size
jb nextElement
cmp again_s,1 ; if last element, check the flag
je again
koniec:
mov ax,4c00h
int 21h
.STACK 100H
end s
an important attention : I set t_size equal to 11, even though the real size equals 12. I made is so, because I tried to meet the condition when the index points at the last element (counting from 0)
I made an implementation, I checked it in Turbo Debugger and elements are in fact replaced if the appropriate condition is met, elements are swapped
but the program runs into infinite loop and I can not notice why is that
here is the entire code
;program do metody babelkowej
.MODEL SMALL
; Algorytm sortowania babelkowego:
; 1. Start.
; 2. index = 0; zamiany = 0;
; 3. Odczytaj dwa sasiadujace elementy z tablicy o pozycjach index oraz
; index + 1; jezeli pierwszy z odczytanych elementow jest wiekszy od
; swojego nastepnika, to zamien elementy miejscami oraz zwieksz zamiany.
; 4. Zwieksz index o jeden.
; 5. Jezeli index < dlugosc_tablicy - 2 to skacz do 3.
; 6. Skacz do 2 jezeli zamiany rozne od zera.
; 7. Stop.
.DATA
t DB 01h, 02h, 00h, 10h, 12h, 33h, 15h, 09h, 11h, 08h, 0Ah, 00h
; 1, 2, 0, 16, 18, 51, 21, 9, 17, 8, 10, 0 ;po sorcie2=> 00h,00h,01h,02h,08h,09h,0Ah,10h,11h,12h,15h,33h
t_size EQU 11 ; t_lenght
again_s db 0 ; if again_s == 1, swap was made, again
.CODE
s:
mov ax,@DATA
mov ds,ax
;buble sort
prepare:
mov bx, OFFSET t ; load the t offset into bx
again:
mov again_s,0 ; XOR the flag
xor si,si ; XOR used indexing register
nextElement:
mov ax,[bx+si] ; first compared element
mov cx,[bx+si+1] ; second one
cmp ax,cx ; comparing
jbe noSwap ; if first <= next jmp no swap
xchg ax,cx ; else swap
mov again_s,1 ; set the flag
noSwap:
inc si ; increment index
cmp si,t_size ; compare the index with t_size
jb nextElement
cmp again_s,1 ; if last element, check the flag
je again
koniec:
mov ax,4c00h
int 21h
.STACK 100H
end s
an important attention : I set t_size equal to 11, even though the real size equals 12. I made is so, because I tried to meet the condition when the index points at the last element (counting from 0)