-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy patharray_ascending.asm
More file actions
77 lines (64 loc) · 2.84 KB
/
array_ascending.asm
File metadata and controls
77 lines (64 loc) · 2.84 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
76
77
; =============================================================================
; TITLE: Array Ascending Sort
; DESCRIPTION: Implementation of an exchange-based sorting algorithm to
; arrange a given byte array in ascending order.
; 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
ARR DB 8, 2, 7, 4, 3 ; Target array
LIMIT EQU 5 ; Array size
MSG DB 'Array successfully sorted in Ascending order.$'
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
START:
; Register initialization
MOV AX, @DATA
MOV DS, AX
MOV CX, LIMIT ; Setup outer loop counter (N)
DEC CX ; Need N-1 passes
; -------------------------------------------------------------------------
; OUTER BUBBLE PASS
; -------------------------------------------------------------------------
OUTER_PASS:
PUSH CX ; Save outer counter
LEA SI, ARR ; Reset pointer to start of array
; -------------------------------------------------------------------------
; INNER COMPARISON LOOP
; -------------------------------------------------------------------------
INNER_COMPARE:
MOV AL, [SI] ; Get current element
CMP AL, [SI+1] ; Compare with next neighbor
JLE SKIP_SWAP ; If AL <= Next, no swap needed
; Perform the Swap
XCHG AL, [SI+1]
MOV [SI], AL
SKIP_SWAP:
INC SI ; Move to next pair
LOOP INNER_COMPARE ; Repeat CX times for this pass
POP CX ; Restore outer counter
LOOP OUTER_PASS ; Start next pass
; Output Result Message
LEA DX, MSG
MOV AH, 09H
INT 21H
; Terminal exit
MOV AH, 4CH
INT 21H
END START
; =============================================================================
; TECHNICAL NOTES
; =============================================================================
; 1. ALGORITHM:
; - Standard Bubble Sort.
; - Complexity: O(N^2). Suitable for very small datasets only.
; - In-place: No additional memory used outside the original array.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =