bubble sorting of an array in 8086 assembly language

18

Transcript of bubble sorting of an array in 8086 assembly language

Page 1: bubble sorting of an array in 8086 assembly language
Page 2: bubble sorting of an array in 8086 assembly language
Page 3: bubble sorting of an array in 8086 assembly language

Group Members

Muharam Ali 14093122-033M.Zunair 14093122-035M.Shanawar 14093122-032Abdul Rauf 14093122-029Sufian Ahmed 14093122-031Rizwan Ali 14093122-036

Page 4: bubble sorting of an array in 8086 assembly language

Write a program that lets the user type a list of single-digit numbers, with one blank between numbers, calls BUBBLE to sort them, and prints the sorted list on the next line.

For example: enter elements: 2 5 8 0 1

after sorting: 0 1 2 5 8

Page 5: bubble sorting of an array in 8086 assembly language

Bubble Sort Example9, 6, 2, 12, 11, 9, 3, 76, 9, 2, 12, 11, 9, 3, 76, 2, 9, 12, 11, 9, 3, 76, 2, 9, 12, 11, 9, 3, 76, 2, 9, 11, 12, 9, 3, 76, 2, 9, 11, 9, 12, 3, 76, 2, 9, 11, 9, 3, 12, 76, 2, 9, 11, 9, 3, 7, 12

Page 6: bubble sorting of an array in 8086 assembly language

Bubble Sort Example

6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 11, 9, 3, 7, 122, 6, 9, 9, 11, 3, 7, 122, 6, 9, 9, 3, 11, 7, 122, 6, 9, 9, 3, 7, 11, 126, 2, 9, 11, 9, 3, 7, 12First Pass

Second Pass

Page 7: bubble sorting of an array in 8086 assembly language

Bubble Sort Example

2, 6, 9, 9, 3, 7, 11, 122, 6, 9, 3, 9, 7, 11, 122, 6, 9, 3, 7, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 9, 3, 7, 11, 12

Second Pass

First Pass

Third Pass

Page 8: bubble sorting of an array in 8086 assembly language

Bubble Sort Example

2, 6, 9, 3, 7, 9, 11, 122, 6, 3, 9, 7, 9, 11, 122, 6, 3, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 9, 3, 7, 11, 12

Second Pass

First Pass

Third Pass

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

Page 9: bubble sorting of an array in 8086 assembly language

Bubble Sort Example

2, 6, 3, 7, 9, 9, 11, 122, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 9, 3, 7, 11, 12

Second Pass

First Pass

Third Pass

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

2, 6, 3, 7, 9, 9, 11, 12Fifth Pass

Page 10: bubble sorting of an array in 8086 assembly language

Algorithm of Bubble Sort ProcedureSI= Offset addressBX=Array sizeÞ Outer Loop

Set SI=1st Element of Array Set DI=2nd Element of ArrayÞ Inner Loop Compare SI with DI If SI < DI Then jump to SKIP else exchange SI with DIÞ SKIP: set SI= 2nd element set DI= 3rd element ;jump to label inner loop If BX ! = 0 jump inner loop Loop outer loop

Page 11: bubble sorting of an array in 8086 assembly language

1. .MODEL SMALL2. .STACK 100H3. .DATA4. MSG1 DB 'enter elements: $'5. MSG2 DB 'AFTER SORTING: $'6. ARR DB 100 dup (0)7. .CODE8. MAIN PROC9. MOV AX,@DATA10.MOV DS,AX11. MOV AH,912.lea DX,MSG1 ;DISPLAY MSG113.INT 21H

Code

Page 12: bubble sorting of an array in 8086 assembly language

14.XOR CX, CX ;clear CX15.MOV AH,116.INT 21H ;first input17.XOR SI,SI18.WHILE_:19. CMP AL, 0dH ;compare input with CR20. JE END_WHILE21. MOV ARR[SI],AL ;move input into array22. INC SI ;SI+123. INC CX24. MOV AH,225. MOV DL,' ' ;display space26. INT 21h27. MOV AH,128. INT 21H29. JMP WHILE_

Page 13: bubble sorting of an array in 8086 assembly language

30. END_WHILE:31. MOV AH,232. MOV DL,0DH33. INT 21H34. MOV DL,0AH35. INT 21H36. JCXZ EXIT37. LEA SI,ARR38. MOV BX,CX39. CALL BUBBLE_SORT

Page 14: bubble sorting of an array in 8086 assembly language

40. MOV AH,941. LEA DX,MSG242. INT 21H43. XOR SI,SI44. TOP:45. MOV AH,246. MOV DL,ARR[SI]47. INT 21H48. MOV DL,' '49. INT 21H50. INC SI51. LOOP TOP52. EXIT:53. MOV AH,4CH54. INT 21H55. MAIN ENDP

Page 15: bubble sorting of an array in 8086 assembly language

1. BUBBLE_SORT PROC2. ; this procedure will sort the array in ascending

order3. ; input : SI=offset address of the array4. ; : BX=array size5. ; output : Sorted Array

6. PUSH AX ; push AX onto the STACK 7. PUSH BX ; push BX onto the STACK8. PUSH CX ; push CX onto the STACK9. PUSH DX ; push DX onto the STACK10. PUSH DI ; push DI onto the STACK

11. MOV AX, SI ; set AX=SI12. MOV CX, BX ; set CX=BX13. DEC CX ; set CX=CX-1

Page 16: bubble sorting of an array in 8086 assembly language

14.@OUTER_LOOP: ; loop label15. MOV BX, CX ; set BX=CX16. MOV SI, AX ; set SI=AX17. MOV DI, AX ; set DI=AX18. INC DI ; set DI=DI+119. @INNER_LOOP: ; loop label 20. MOV DL, [SI] ; set DL=[SI]21. CMP DL, [DI] ; compare DL with [DI]22. JNG @SKIP_EXCHANGE ; jump to label

@SKIP_EXCHANGE if DL<[DI]

23. XCHG DL, [DI] ; set DL=[DI], [DI]=DL24. MOV [SI], DL ; set [SI]=DL25. @SKIP_EXCHANGE: ; jump label26. INC SI ; set SI=SI+127. INC DI ; set DI=DI+128. DEC BX ; set BX=BX-129. JNZ @INNER_LOOP ; jump @INNER_LOOP if BX!=030. LOOP @OUTER_LOOP ; jump @OUTER_LOOP while

Page 17: bubble sorting of an array in 8086 assembly language

31. POP DI ; pop a value from STACK into DI32. POP DX ; pop a value from STACK into DX33. POP CX ; pop a value from STACK into CX34. POP BX ; pop a value from STACK into BX35. POP AX ; pop a value from STACK into AX

36. RET ; return control to the calling procedure

37. BUBBLE_SORT ENDP38.END MAIN

Page 18: bubble sorting of an array in 8086 assembly language

Output