Advertisement
Guest User

Bubblesort NASM

a guest
Nov 13th, 2010
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [ORG 0x100]
  2. [BITS 16]
  3.  
  4. ;----------------------------------------------------------
  5. ;                       CONSTANTS
  6. ;----------------------------------------------------------
  7.  
  8. vidmem      equ 0xb800; Start of VGA text-memory
  9. scrw        equ 80*25   ; Size of video memory in words
  10. eom         equ 0       ; end of message (\0, null character like C)
  11.  
  12. ;----------------------------------------------------------
  13. ;                         DATA
  14. ; Description:
  15. ;   Here we define and reserve all of our global variables
  16. ;Contains:
  17. ;   int print_start -> position where printing begins
  18. ;   char color      -> color attribute (black background, white foreground)
  19. ;   char msg[]      -> message to be printed
  20. ;   char msg_end    -> termination character for msg
  21. ;   int msg_len     -> length of msg
  22. ;
  23. ;   System data and Protected Mode related data structures
  24. ;  
  25. ;----------------------------------------------------------
  26. [SECTION .data]
  27.  
  28. ;-----------------------------------------------------------------------------
  29. ;-------------           APPLICATION RELATED               -------------------
  30. ;-----------------------------------------------------------------------------
  31.  
  32. print_start:    dw 160
  33. color:          db 7
  34. ex_array:       db '987654321'
  35. msg_end:        db eom
  36. array_len   db 9
  37. msg_len:        dw (msg_end - ex_array)
  38.  
  39. ;----------------------------------------------------------
  40. ;                         BSS
  41. ; Description:
  42. ;   Here we define our uninitialized variables
  43. ;   Most of the time, it is used for the stack
  44. ; Contains:
  45. ;   stack 256 bytes long, aligned to 4-byte limit
  46. ;   (start address of stack can be divided by 4)
  47. ;   stack_end       -> pointer to the next memory address after the stack
  48. ;----------------------------------------------------------
  49. [SECTION .bss]
  50. alignb 4
  51.  
  52. stack:          resb 256
  53. stack_end:
  54.  
  55. ;----------------------------------------------------------
  56. ;                         TEXT
  57. ; Description:
  58. ;   The part of the file containing the code
  59. ; Functions:
  60. ;   start           -> entry point of the program
  61. ;   main            -> implements our algorithm
  62. ;   cls             -> Clears the screen
  63. ;   printstr        -> Prints our message
  64. ;----------------------------------------------------------
  65.  
  66. [SECTION .text]
  67.  
  68. start:
  69.  
  70. ; Initialize the stack pointer
  71.                 mov ax,cs
  72.                 mov ss,ax
  73.                 mov sp,stack_end
  74.                
  75. ; Set DS register to current data segment
  76.                 mov ax,cs
  77.                 mov ds,ax
  78.  
  79.                 call main
  80.        
  81. ; Return to DOS
  82.                 mov ax,0x4C00
  83.                 int 0x21
  84.  
  85.  
  86. ;----------------------------------------------------------
  87. ; Procedure void main()
  88. ;    performs the main part of the program
  89. ;    Arguments:
  90. ;       none
  91. ;    Local Variables:
  92. ;       none
  93. ;    Returns:
  94. ;       nothing
  95. ;----------------------------------------------------------
  96.      
  97. main:
  98.  
  99. ; Make Extra Segment (ES) point to videomemory
  100.                 mov ax,vidmem
  101.                 mov es,ax
  102.                
  103. ; Calculate where printing stops
  104.                 mov dx,scrw
  105.                 sub dx,[msg_len]
  106.                 sal dx,1
  107.  
  108. move_msg:
  109.                 call cls  
  110.         call bubblesort  
  111.         call printstr                  
  112.                 ret
  113.                
  114. ;----------------------------------------------------------
  115. ; Procedure void cls()
  116. ;    clears screen
  117. ;    Arguments:
  118. ;       none
  119. ;    Local Variables:
  120. ;       none
  121. ;    Returns:
  122. ;       nothing
  123. ;----------------------------------------------------------
  124.  
  125. cls:
  126.                 mov di,0            ; es:di point to start of vidmem          
  127.                 mov al,' '          ; Lower byte of each char (char to print) = ' '
  128.                 mov ah,[color]      ; Higher byte of each char (color) = white
  129.                 mov cx,scrw         ; cx (repeat counter) = number of chars in screen
  130.        
  131. ; Repear for cx times -> Store ax in es:di and increase di by 2
  132.                 rep stosw
  133.                 ret
  134.                
  135. ;----------------------------------------------------------
  136. ; Procedure void printstr()
  137. ;    prints msg on screen, by copying each character of msg
  138. ;    at videomem starting by offset print_start.
  139. ;    Ends copying when it encounters eom
  140. ;    Arguments:
  141. ;       none
  142. ;    Local Variables:
  143. ;       none
  144. ;    Returns:
  145. ;       nothing
  146. ;----------------------------------------------------------
  147.  
  148. printstr:
  149.         push si
  150.         push di
  151.                 mov esi,ex_array          ; load si with offset of msg      
  152.                 mov di,160          ; load di with 160
  153.  
  154. prints_loop:
  155.                 cmp byte [si],eom   ; if current char of msg == eom
  156.                 je prints_exit      ;    exit
  157.                 movsb               ; else mov[ds:si]->[es:di] - copy char to srceen
  158.                                     ; and increase si and di
  159.                 inc di              ; di now points to color byte -> increase more
  160.                 jmp prints_loop     ; repeat
  161.  
  162. prints_exit:    pop di
  163.         pop si
  164.  
  165.                 ret
  166. ;-----------------------------BubbleSort-----------------------
  167.  
  168.                
  169. bubblesort: push esi
  170.         push edi
  171.         push ebx
  172.         push ax
  173.         push dx
  174.  
  175.         xor edi,edi
  176.         xor esi,esi
  177.         mov ebx,ex_array
  178.         mov esi, array_len
  179.         inc esi
  180.                
  181. outerLoop:  dec esi
  182.         mov edi,0
  183.         jz end_outer
  184.  
  185. innerLoop:  inc edi
  186.         cmp edi,esi
  187.         ja outerLoop
  188.         jmp compare
  189.        
  190. compare:    mov al,byte [ebx+edi*1-1]
  191.         mov dl, byte [ebx+edi*1]
  192.         cmp al,dl
  193.         jng innerLoop
  194.  
  195. swap:       mov [ebx+edi*1-1],dl
  196.         mov [ebx+edi*1],al
  197.         jmp innerLoop
  198.  
  199. end_outer:  pop dx
  200.         pop ax
  201.         pop ebx
  202.         pop edi
  203.         pop esi
  204.  
  205.         ret
  206.  
  207. ;----------------------------------------------------------
  208. ; Procedure void software_delay()
  209. ;
  210.  
  211. software_delay:
  212.                 push bx
  213.                 push cx
  214.  
  215.                 mov cx,5000
  216. outer_Loop:     mov bx,1024
  217. inner_Loop:     dec bx
  218.                 jnz inner_Loop
  219.            
  220.                 loop outer_Loop
  221.  
  222.                 pop cx
  223.                 pop bx
  224.                 ret
  225.  
  226. ;----------------------------------------------------------
  227.        
  228. program_end:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement