bubble sorting of an array in 8086 assembly language

12567800 22,571 views 18 slides Sep 07, 2016
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

assembly language programming and organization of the ibm pc by ytha yu (chapter 10 q.6)


Slide Content

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, 12 2, 6, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 11, 3, 7, 12 2, 6, 9, 9, 3, 11, 7, 12 2, 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, 12 2, 6, 9, 3, 9, 7, 11, 12 2, 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, 12 2, 6, 3, 9, 7, 9, 11, 12 2, 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, 12 Fourth Pass

Bubble Sort Example 2, 6, 3, 7, 9, 9, 11, 12 2, 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, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass

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

.MODEL SMALL .STACK 100H .DATA MSG1 DB 'enter elements: $' MSG2 DB 'AFTER SORTING: $' ARR DB 100 dup (0) .CODE MAIN PROC MOV AX,@DATA MOV DS,AX MOV AH,9 lea DX,MSG1 ;DISPLAY MSG1 INT 21H Code

XOR CX, CX ;clear CX MOV AH,1 INT 21H ; first input XOR SI,SI WHILE_: CMP AL, 0dH ; compare input with CR JE END_WHILE MOV ARR[SI],AL ; move input into array INC SI ; SI+1 INC CX MOV AH,2 MOV DL,' ' ; display space INT 21h MOV AH,1 INT 21H JMP WHILE_

END_WHILE: MOV AH,2 MOV DL,0DH INT 21H MOV DL,0AH INT 21H JCXZ EXIT LEA SI,ARR MOV BX,CX CALL BUBBLE_SORT

MOV AH,9 LEA DX,MSG2 INT 21H XOR SI,SI TOP: MOV AH,2 MOV DL,ARR[SI] INT 21H MOV DL,' ' INT 21H INC SI LOOP TOP EXIT: MOV AH,4CH INT 21H MAIN ENDP

BUBBLE_SORT PROC ; this procedure will sort the array in ascending order ; input : SI=offset address of the array ; : BX=array size ; output : Sorted Array PUSH AX ; push AX onto the STACK PUSH BX ; push BX onto the STACK PUSH CX ; push CX onto the STACK PUSH DX ; push DX onto the STACK PUSH DI ; push DI onto the STACK MOV AX, SI ; set AX=SI MOV CX, BX ; set CX=BX DEC CX ; set CX=CX-1

@OUTER_LOOP: ; loop label MOV BX, CX ; set BX=CX MOV SI, AX ; set SI=AX MOV DI, AX ; set DI=AX INC DI ; set DI=DI+1 @ INNER_LOOP: ; loop label MOV DL, [SI] ; set DL=[SI] CMP DL, [DI] ; compare DL with [DI] JNG @SKIP_EXCHANGE ; jump to label @ SKIP_EXCHANGE if DL<[DI] XCHG DL, [DI] ; set DL=[DI], [DI]=DL MOV [SI], DL ; set [SI]=DL @SKIP_EXCHANGE: ; jump label INC SI ; set SI=SI+1 INC DI ; set DI=DI+1 DEC BX ; set BX=BX-1 JNZ @INNER_LOOP ; jump @INNER_LOOP if BX!=0 LOOP @OUTER_LOOP ; jump @OUTER_LOOP while

POP DI ; pop a value from STACK into DI POP DX ; pop a value from STACK into DX POP CX ; pop a value from STACK into CX POP BX ; pop a value from STACK into BX POP AX ; pop a value from STACK into AX RET ; return control to the calling procedure BUBBLE_SORT ENDP END MAIN

Output
Tags