-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy patharray_descending.asm
More file actions
77 lines (63 loc) · 2.54 KB
/
array_descending.asm
File metadata and controls
77 lines (63 loc) · 2.54 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 Descending Sort
; DESCRIPTION: Arrange an 8-bit numeric array in descending order using
; the bubble-exchange technique.
; 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
VALUES DB 99H, 12H, 56H, 45H, 36H ; Random input bytes
V_LEN EQU 5
MSG DB 'Array successfully sorted in Descending order.$'
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
START:
; Setup Segment
MOV AX, @DATA
MOV DS, AX
MOV CX, V_LEN ; Outer loop counter
DEC CX ; N-1 passes
; -------------------------------------------------------------------------
; DESCENDING BUBBLE LOGIC
; -------------------------------------------------------------------------
OUTER_SORT:
PUSH CX
LEA SI, VALUES ; Start of array
COMPARE_DESC:
MOV AL, [SI] ; Element 1
MOV BL, [SI+1] ; Element 2
CMP AL, BL ; Compare for Descending (AL > BL)
JAE NO_ACTION ; If AL >= BL, carry on
; SWAP
XCHG [SI], BL ; Swap in memory directly
MOV [SI+1], BL
NO_ACTION:
INC SI ; Advance pointer
LOOP COMPARE_DESC
POP CX
LOOP OUTER_SORT ; Repeat until sorted
; User Notification
LEA DX, MSG
MOV AH, 09H
INT 21H
; Exit
MOV AH, 4CH
INT 21H
END START
; =============================================================================
; TECHNICAL NOTES
; =============================================================================
; 1. LOGIC:
; - This is the mirror of the ascending sort.
; - The 'JAE' (Jump if Above or Equal) is the key control flow here for
; the descending property.
; - Final sorted order will be: 99h, 56h, 45h, 36h, 12h.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =