-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathreverse_array.asm
More file actions
70 lines (59 loc) · 2.58 KB
/
reverse_array.asm
File metadata and controls
70 lines (59 loc) · 2.58 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
; =============================================================================
; TITLE: Array Reversal
; DESCRIPTION: Reverses the contents of a byte array. It uses a second buffer
; to store the reversed copy. In-place reversal (using XCHG)
; is an alternative not demonstrated here.
; 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
SRC_ARR DB 1, 2, 3, 4, 5 ; Original
DST_ARR DB 5 DUP(?) ; Destination
ARR_LEN EQU 5
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; --- Step 1: Initialize Data Segment ---
MOV AX, @DATA
MOV DS, AX
MOV ES, AX ; ES needed for potential string ops (optional here)
; --- Step 2: Setup Pointers ---
LEA SI, SRC_ARR ; SI -> Start of Source
LEA DI, DST_ARR ; DI -> Start of Dest
ADD DI, ARR_LEN - 1 ; DI -> End of Dest (Reverse fill)
MOV CX, ARR_LEN
; --- Step 3: Copy Loop ---
REV_LOOP:
MOV AL, [SI] ; Load from Start
MOV [DI], AL ; Store at End
INC SI ; Move Forward
DEC DI ; Move Backward
LOOP REV_LOOP
; Verification: DST_ARR is now {5, 4, 3, 2, 1}
; --- Step 4: Exit ---
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES & ARCHITECTURAL INSIGHTS
; =============================================================================
; 1. POINTER ARITHMETIC:
; We use two pointers moving in opposite logical directions relative to their
; arrays:
; - SI increments (0 -> N)
; - DI decrements (N -> 0)
; This effectively maps Source[i] to Dest[N-1-i].
;
; 2. SEGMENT INITIALIZATION:
; While this program uses DS for both reads and writes, initializing ES is
; good practice if we were using STOSB or MOVSB instructions.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =