KEMBAR78
bubble sorting of an array in 8086 assembly language | PPTX
Group Members
Muharam Ali 14093122-033
M.Zunair 14093122-035
M.Shanawar 14093122-032
Abdul Rauf 14093122-029
Sufian Ahmed 14093122-031
Rizwan Ali 14093122-036
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
Bubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 11, 12, 9, 3, 7
6, 2, 9, 11, 9, 12, 3, 7
6, 2, 9, 11, 9, 3, 12, 7
6, 2, 9, 11, 9, 3, 7, 12
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, 12
6, 2, 9, 11, 9, 3, 7, 12
First Pass
Second Pass
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, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
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, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
2, 6, 9, 3, 7, 9, 11, 12Fourth Pass
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, 12
2, 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
Algorithm of Bubble Sort Procedure
SI= Offset address
BX=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
1. .MODEL SMALL
2. .STACK 100H
3. .DATA
4. MSG1 DB 'enter elements: $'
5. MSG2 DB 'AFTER SORTING: $'
6. ARR DB 100 dup (0)
7. .CODE
8. MAIN PROC
9. MOV AX,@DATA
10.MOV DS,AX
11. MOV AH,9
12.lea DX,MSG1 ;DISPLAY MSG1
13.INT 21H
Code
14.XOR CX, CX ;clear CX
15.MOV AH,1
16.INT 21H ;first input
17.XOR SI,SI
18.WHILE_:
19. CMP AL, 0dH ;compare input with CR
20. JE END_WHILE
21. MOV ARR[SI],AL ;move input into array
22. INC SI ;SI+1
23. INC CX
24. MOV AH,2
25. MOV DL,' ' ;display space
26. INT 21h
27. MOV AH,1
28. INT 21H
29. JMP WHILE_
30. END_WHILE:
31. MOV AH,2
32. MOV DL,0DH
33. INT 21H
34. MOV DL,0AH
35. INT 21H
36. JCXZ EXIT
37. LEA SI,ARR
38. MOV BX,CX
39. CALL BUBBLE_SORT
40. MOV AH,9
41. LEA DX,MSG2
42. INT 21H
43. XOR SI,SI
44. TOP:
45. MOV AH,2
46. MOV DL,ARR[SI]
47. INT 21H
48. MOV DL,' '
49. INT 21H
50. INC SI
51. LOOP TOP
52. EXIT:
53. MOV AH,4CH
54. INT 21H
55. MAIN ENDP
1. BUBBLE_SORT PROC
2. ; this procedure will sort the array in ascending
order
3. ; input : SI=offset address of the array
4. ; : BX=array size
5. ; output : Sorted Array
6. PUSH AX ; push AX onto the STACK
7. PUSH BX ; push BX onto the STACK
8. PUSH CX ; push CX onto the STACK
9. PUSH DX ; push DX onto the STACK
10. PUSH DI ; push DI onto the STACK
11. MOV AX, SI ; set AX=SI
12. MOV CX, BX ; set CX=BX
13. DEC CX ; set CX=CX-1
14.@OUTER_LOOP: ; loop label
15. MOV BX, CX ; set BX=CX
16. MOV SI, AX ; set SI=AX
17. MOV DI, AX ; set DI=AX
18. INC DI ; set DI=DI+1
19. @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]=DL
24. MOV [SI], DL ; set [SI]=DL
25. @SKIP_EXCHANGE: ; jump label
26. INC SI ; set SI=SI+1
27. INC DI ; set DI=DI+1
28. DEC BX ; set BX=BX-1
29. JNZ @INNER_LOOP ; jump @INNER_LOOP if BX!=0
30. LOOP @OUTER_LOOP ; jump @OUTER_LOOP while
31. POP DI ; pop a value from STACK into DI
32. POP DX ; pop a value from STACK into DX
33. POP CX ; pop a value from STACK into CX
34. POP BX ; pop a value from STACK into BX
35. POP AX ; pop a value from STACK into AX
36. RET ; return control to the calling
procedure
37. BUBBLE_SORT ENDP
38.END MAIN
Output

bubble sorting of an array in 8086 assembly language

  • 3.
    Group Members Muharam Ali14093122-033 M.Zunair 14093122-035 M.Shanawar 14093122-032 Abdul Rauf 14093122-029 Sufian Ahmed 14093122-031 Rizwan Ali 14093122-036
  • 4.
    Write a programthat 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
  • 5.
    Bubble Sort Example 9,6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12, 3, 7 6, 2, 9, 11, 9, 3, 12, 7 6, 2, 9, 11, 9, 3, 7, 12
  • 6.
    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, 12 6, 2, 9, 11, 9, 3, 7, 12 First Pass Second Pass
  • 7.
    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, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass
  • 8.
    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, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass 2, 6, 9, 3, 7, 9, 11, 12Fourth Pass
  • 9.
    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, 12 2, 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
  • 10.
    Algorithm of BubbleSort Procedure SI= Offset address BX=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
  • 11.
    1. .MODEL SMALL 2..STACK 100H 3. .DATA 4. MSG1 DB 'enter elements: $' 5. MSG2 DB 'AFTER SORTING: $' 6. ARR DB 100 dup (0) 7. .CODE 8. MAIN PROC 9. MOV AX,@DATA 10.MOV DS,AX 11. MOV AH,9 12.lea DX,MSG1 ;DISPLAY MSG1 13.INT 21H Code
  • 12.
    14.XOR CX, CX;clear CX 15.MOV AH,1 16.INT 21H ;first input 17.XOR SI,SI 18.WHILE_: 19. CMP AL, 0dH ;compare input with CR 20. JE END_WHILE 21. MOV ARR[SI],AL ;move input into array 22. INC SI ;SI+1 23. INC CX 24. MOV AH,2 25. MOV DL,' ' ;display space 26. INT 21h 27. MOV AH,1 28. INT 21H 29. JMP WHILE_
  • 13.
    30. END_WHILE: 31. MOVAH,2 32. MOV DL,0DH 33. INT 21H 34. MOV DL,0AH 35. INT 21H 36. JCXZ EXIT 37. LEA SI,ARR 38. MOV BX,CX 39. CALL BUBBLE_SORT
  • 14.
    40. MOV AH,9 41.LEA DX,MSG2 42. INT 21H 43. XOR SI,SI 44. TOP: 45. MOV AH,2 46. MOV DL,ARR[SI] 47. INT 21H 48. MOV DL,' ' 49. INT 21H 50. INC SI 51. LOOP TOP 52. EXIT: 53. MOV AH,4CH 54. INT 21H 55. MAIN ENDP
  • 15.
    1. BUBBLE_SORT PROC 2.; this procedure will sort the array in ascending order 3. ; input : SI=offset address of the array 4. ; : BX=array size 5. ; output : Sorted Array 6. PUSH AX ; push AX onto the STACK 7. PUSH BX ; push BX onto the STACK 8. PUSH CX ; push CX onto the STACK 9. PUSH DX ; push DX onto the STACK 10. PUSH DI ; push DI onto the STACK 11. MOV AX, SI ; set AX=SI 12. MOV CX, BX ; set CX=BX 13. DEC CX ; set CX=CX-1
  • 16.
    14.@OUTER_LOOP: ; looplabel 15. MOV BX, CX ; set BX=CX 16. MOV SI, AX ; set SI=AX 17. MOV DI, AX ; set DI=AX 18. INC DI ; set DI=DI+1 19. @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]=DL 24. MOV [SI], DL ; set [SI]=DL 25. @SKIP_EXCHANGE: ; jump label 26. INC SI ; set SI=SI+1 27. INC DI ; set DI=DI+1 28. DEC BX ; set BX=BX-1 29. JNZ @INNER_LOOP ; jump @INNER_LOOP if BX!=0 30. LOOP @OUTER_LOOP ; jump @OUTER_LOOP while
  • 17.
    31. POP DI; pop a value from STACK into DI 32. POP DX ; pop a value from STACK into DX 33. POP CX ; pop a value from STACK into CX 34. POP BX ; pop a value from STACK into BX 35. POP AX ; pop a value from STACK into AX 36. RET ; return control to the calling procedure 37. BUBBLE_SORT ENDP 38.END MAIN
  • 18.