yazdmich

Bubble Sort in ASM

May 8th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. ORG C0 ; Place following code starting at [C0] (VDU)
  2. DB "12345" ; Place list to be sorted in memory
  3. ORG 0 ; Place following code starting at [00]
  4. START:
  5. CLO ; Close unwanted windows
  6. MOV AL,0 ; Swap register
  7. MOV BL,0 ; Swap register
  8. MOV CL,C0 ; Display pointer
  9. MOV DL,0 ; Swap counter
  10. MOV AL,20 ; Move " " to AL
  11. MOV [C5],AL ; Move AL to RAM [C5] (hardcoded for display popup)
  12. MOV AL,0 ; Reset AL for program start
  13. MAIN:
  14. CMP DL,5 ; Check for sorting completion (swap counter starts at 0,
  15. ; decrements at each comparison with a swap, increments at
  16. ; each comparison without a swap, value of 5 marks fully sorted with no false positives)
  17. JZ BYE ; Exit if list is sorted
  18. CMP CL,C4 ; Is the display pointer at the end of the list?
  19. JZ RESET ; If yes, jump to reset label
  20. MOV AL,[CL] ; Move value at display pointer to AL
  21. INC CL ; Increment display pointer to get next value
  22. MOV BL,[CL] ; Move value at display pointer to BL
  23. CMP AL,BL ; Is AL greater than BL?
  24. JNS SWAP ; If yes, jump to swap label
  25. CMP BL,AL ; Is BL greater than AL?
  26. JNS CHECK ; If yes, jump to check label
  27. CHECK:
  28. INC DL ; Increment swap counter
  29. JMP MAIN ; Jump back to main loop
  30. SWAP:
  31. DEC DL ; Decrement swap counter
  32. PUSH AL ; Push value 1 to stack from AL
  33. PUSH BL ; Push value 2 to stack from BL
  34. POP AL ; Pop value 2 from stack to AL
  35. POP BL ; Pop value 1 from stack to BL
  36. DEC CL ; Decrement display pointer
  37. MOV [CL],AL ; Move AL to [CL]
  38. INC CL ; Increment display pointer
  39. MOV [CL],BL ; Move BL to [CL]
  40. JMP MAIN ; Jump back to main loop
  41. RESET:
  42. MOV CL,C0 ; Reset display pointer to C0
  43. JMP MAIN ; Jump back to main loop
  44. BYE:
  45. END ; End program
Advertisement
Add Comment
Please, Sign In to add comment