bubble sorting of an array in 8086 assembly language
12567800
22,571 views
18 slides
Sep 07, 2016
Slide 1 of 18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
About This Presentation
assembly language programming and organization of the ibm pc by ytha yu (chapter 10 q.6)
Size: 978.54 KB
Language: en
Added: Sep 07, 2016
Slides: 18 pages
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
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