PDA

View Full Version : Bubble Sort in TASM



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)

kornicameister
3rd March 2011, 15:10
fixed the issues
still do not know how to check if sorting is correct

I would be glad if someone just took a look on fixed 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,5h, 09h, 11h, 08h, 0Ah, 00h
t_size EQU 3 ; dlugosc tablicy
again_s db 0 ; przyjmie 1 jesli byla zamiana
.CODE
s:
mov ax,@DATA
mov ds,ax
;buble sort
prepare:
mov bx, OFFSET t
again:
mov again_s,0
xor si,si
nextElement:
mov ah,[bx+si]
mov al,[bx+si+1]
cmp ah,al
jbe noSwap
mov [bx+si],al
mov [bx+si+1],ah
mov again_s,1
noSwap:
inc si
cmp si,t_size
jl nextElement

cmp again_s,1
je again=

mov ax,4c00h
int 21h
.STACK 100H
end s

stampede
5th March 2011, 15:35
In my free time I'm learning assembler as well - using HLA ( assembly language developed for academic purposes ), I've rewritten your program in this syntax and added sorting check:


program TestProg;

#include ("stdlib.hhf")

static
t_size: int32 := 12;
again_s: int8 := 0;
t: int8[12] := [1, 2, 0, 16, 44, 11,5, 9, 11, 8, 12, 3];

begin TestProg;

again:
mov (0,again_s);
xor (esi,esi);

nextElement:
mov( t[esi],ah); ;
mov (t[esi+1],al);
cmp (al,ah);
jbe (noSwap);
mov (al,t[esi]);
mov (ah,t[esi+1]);
mov (1,again_s);

noSwap:
inc (esi);
cmp (esi,t_size);
jl (nextElement);
cmp (again_s,1);
je (again);

xor(esi,esi);
print:
stdout.put(t[esi], " ");
inc(esi);
cmp(esi,t_size);
jl(print);

stdout.put(nl);
stdout.put("start check..." nl);

xor(esi,esi);
check:
mov(t[esi],ah);
mov(t[esi+1],al);
inc(esi);
cmp(t_size,esi);
jl(check_ok);
cmp(al,ah);
jbe(check);

check_failed:
stdout.put("not ok" nl);
jmp end_prog;

check_ok:
stdout.put("check ok");

end_prog:

end TestProg;

Here you have more about HLA if you are interested: link (http://webster.cs.ucr.edu/AsmTools/HLA/index.html).

kornicameister
6th March 2011, 22:10
I am literally forced to use tasm :)
but thank you for Your time :)