-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbubble_sort.asm
More file actions
75 lines (62 loc) · 2.69 KB
/
bubble_sort.asm
File metadata and controls
75 lines (62 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
; =============================================================================
; TITLE: Bubble Sort (16-bit)
; DESCRIPTION: Implementation of Bubble Sort algorithm for a set of
; unsigned 16-bit word integers.
; AUTHOR: Amey Thakur (https://github.com/Amey-Thakur)
; REPOSITORY: https://github.com/Amey-Thakur/8086-ASSEMBLY-LANGUAGE-PROGRAMS
; LICENSE: MIT License
; =============================================================================
.MODEL SMALL
.STACK 100H
; -----------------------------------------------------------------------------
; DATA SEGMENT
; -----------------------------------------------------------------------------
.DATA
; Word array (DW)
A DW 0005H, 0ABCDH, 5678H, 1234H, 0EFCDH, 45EFH
NUM EQU 6 ; Total count of words
MSG DB 'Word-sized Bubble Sort completed.$'
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
START:
; Context setup
MOV AX, @DATA
MOV DS, AX
MOV BX, NUM ; Load count
DEC BX ; N-1 comparisons per pass
; -------------------------------------------------------------------------
; BUBBLE SORT OPERATION
; -------------------------------------------------------------------------
PASS_LOOP:
MOV CX, BX ; Inner loop counter
LEA SI, A ; Reset SI for each new pass
SWAP_INNER:
MOV AX, [SI] ; Load current Word
CMP AX, [SI+2] ; Compare with next Word (+2 byte offset)
JBE NO_EXCH ; If AX <= Next, continue
; Exchange contents
XCHG AX, [SI+2]
MOV [SI], AX
NO_EXCH:
ADD SI, 2 ; Move to next Word address
LOOP SWAP_INNER ; Repeat for this pass
DEC BX ; Optimization: one less comparison next time
JNZ PASS_LOOP ; Repeat until done
; Result output
LEA DX, MSG
MOV AH, 09H
INT 21H
; End process
MOV AH, 4CH
INT 21H
END START
; =============================================================================
; TECHNICAL NOTES
; =============================================================================
; 1. DATA SIZE:
; - This version handles 16-bit data (Words). SI increments by 2 accordingly.
; - Bubble sort derives its name from smaller values "bubbling" to the surface.
; - Average and worst-case complexity: O(N^2).
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =