Results 1 to 4 of 4

Thread: Bubble Sort in TASM

  1. #1
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Bubble Sort in TASM

    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
    Qt Code:
    1. ;program do metody babelkowej
    2.  
    3. .MODEL SMALL
    4. ; Algorytm sortowania babelkowego:
    5. ; 1. Start.
    6. ; 2. index = 0; zamiany = 0;
    7. ; 3. Odczytaj dwa sasiadujace elementy z tablicy o pozycjach index oraz
    8. ; index + 1; jezeli pierwszy z odczytanych elementow jest wiekszy od
    9. ; swojego nastepnika, to zamien elementy miejscami oraz zwieksz zamiany.
    10. ; 4. Zwieksz index o jeden.
    11. ; 5. Jezeli index < dlugosc_tablicy - 2 to skacz do 3.
    12. ; 6. Skacz do 2 jezeli zamiany rozne od zera.
    13. ; 7. Stop.
    14. .DATA
    15.  
    16. t DB 01h, 02h, 00h, 10h, 12h, 33h, 15h, 09h, 11h, 08h, 0Ah, 00h
    17. ; 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
    18. t_size EQU 11 ; t_lenght
    19. again_s db 0 ; if again_s == 1, swap was made, again
    20. .CODE
    21. s:
    22. mov ax,@DATA
    23. mov ds,ax
    24. ;buble sort
    25. prepare:
    26. mov bx, OFFSET t ; load the t offset into bx
    27. again:
    28. mov again_s,0 ; XOR the flag
    29. xor si,si ; XOR used indexing register
    30. nextElement:
    31. mov ax,[bx+si] ; first compared element
    32. mov cx,[bx+si+1] ; second one
    33. cmp ax,cx ; comparing
    34. jbe noSwap ; if first <= next jmp no swap
    35. xchg ax,cx ; else swap
    36. mov again_s,1 ; set the flag
    37. noSwap:
    38. inc si ; increment index
    39. cmp si,t_size ; compare the index with t_size
    40. jb nextElement
    41. cmp again_s,1 ; if last element, check the flag
    42. je again
    43. koniec:
    44. mov ax,4c00h
    45. int 21h
    46. .STACK 100H
    47. end s
    To copy to clipboard, switch view to plain text mode 

    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)
    Last edited by kornicameister; 3rd March 2011 at 10:05. Reason: translation from polish code comments
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  2. #2
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Bubble Sort in TASM

    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

    Qt Code:
    1. ;program do metody babelkowej
    2.  
    3. .MODEL SMALL
    4. ; Algorytm sortowania babelkowego:
    5. ; 1. Start.
    6. ; 2. index = 0; zamiany = 0;
    7. ; 3. Odczytaj dwa sasiadujace elementy z tablicy o pozycjach index oraz
    8. ; index + 1; jezeli pierwszy z odczytanych elementow jest wiekszy od
    9. ; swojego nastepnika, to zamien elementy miejscami oraz zwieksz zamiany.
    10. ; 4. Zwieksz index o jeden.
    11. ; 5. Jezeli index < dlugosc_tablicy - 2 to skacz do 3.
    12. ; 6. Skacz do 2 jezeli zamiany rozne od zera.
    13. ; 7. Stop.
    14. .DATA
    15.  
    16. t DB 01h, 02h, 00h, 10h, 12h, 33h,5h, 09h, 11h, 08h, 0Ah, 00h
    17. t_size EQU 3 ; dlugosc tablicy
    18. again_s db 0 ; przyjmie 1 jesli byla zamiana
    19. .CODE
    20. s:
    21. mov ax,@DATA
    22. mov ds,ax
    23. ;buble sort
    24. prepare:
    25. mov bx, OFFSET t
    26. again:
    27. mov again_s,0
    28. xor si,si
    29. nextElement:
    30. mov ah,[bx+si]
    31. mov al,[bx+si+1]
    32. cmp ah,al
    33. jbe noSwap
    34. mov [bx+si],al
    35. mov [bx+si+1],ah
    36. mov again_s,1
    37. noSwap:
    38. inc si
    39. cmp si,t_size
    40. jl nextElement
    41.  
    42. cmp again_s,1
    43. je again=
    44.  
    45. mov ax,4c00h
    46. int 21h
    47. .STACK 100H
    48. end s
    To copy to clipboard, switch view to plain text mode 
    Last edited by kornicameister; 3rd March 2011 at 16:41.
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Bubble Sort in TASM

    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:
    Qt Code:
    1. program TestProg;
    2.  
    3. #include ("stdlib.hhf")
    4.  
    5. static
    6. t_size: int32 := 12;
    7. again_s: int8 := 0;
    8. t: int8[12] := [1, 2, 0, 16, 44, 11,5, 9, 11, 8, 12, 3];
    9.  
    10. begin TestProg;
    11.  
    12. again:
    13. mov (0,again_s);
    14. xor (esi,esi);
    15.  
    16. nextElement:
    17. mov( t[esi],ah); ;
    18. mov (t[esi+1],al);
    19. cmp (al,ah);
    20. jbe (noSwap);
    21. mov (al,t[esi]);
    22. mov (ah,t[esi+1]);
    23. mov (1,again_s);
    24.  
    25. noSwap:
    26. inc (esi);
    27. cmp (esi,t_size);
    28. jl (nextElement);
    29. cmp (again_s,1);
    30. je (again);
    31.  
    32. xor(esi,esi);
    33. print:
    34. stdout.put(t[esi], " ");
    35. inc(esi);
    36. cmp(esi,t_size);
    37. jl(print);
    38.  
    39. stdout.put(nl);
    40. stdout.put("start check..." nl);
    41.  
    42. xor(esi,esi);
    43. check:
    44. mov(t[esi],ah);
    45. mov(t[esi+1],al);
    46. inc(esi);
    47. cmp(t_size,esi);
    48. jl(check_ok);
    49. cmp(al,ah);
    50. jbe(check);
    51.  
    52. check_failed:
    53. stdout.put("not ok" nl);
    54. jmp end_prog;
    55.  
    56. check_ok:
    57. stdout.put("check ok");
    58.  
    59. end_prog:
    60.  
    61. end TestProg;
    To copy to clipboard, switch view to plain text mode 
    Here you have more about HLA if you are interested: link.
    Last edited by stampede; 5th March 2011 at 15:43.

  4. #4
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Bubble Sort in TASM

    I am literally forced to use tasm
    but thank you for Your time
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

Similar Threads

  1. How to sort a Qlist AND get a sort key?
    By agerlach in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2010, 18:44
  2. can't get right data after sort
    By wirasto in forum Qt Programming
    Replies: 5
    Last Post: 28th June 2009, 18:19
  3. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  4. How to sort QMap
    By mamyte03@gmail.com in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 10:11
  5. When sort the lists?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 14th April 2006, 16:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.