-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmemory_scan.asm
More file actions
92 lines (78 loc) · 3.1 KB
/
memory_scan.asm
File metadata and controls
92 lines (78 loc) · 3.1 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
; =============================================================================
; TITLE: Memory Search (Scan)
; DESCRIPTION: Search for the first occurrence of a specific byte within
; a memory block using the SCASB (Scan String Byte) instruction.
; 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
BUFFER DB 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
B_LEN EQU 10
TARGET DB 50 ; Value to look for
MSG_FOUND DB 'Value located! 1-based index position: $'
MSG_FAIL DB 'Value not found in the provided memory block.$'
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; Segment registers
MOV AX, @DATA
MOV DS, AX
MOV ES, AX ; ES is required for SCASB
; -------------------------------------------------------------------------
; STRING SCAN SETUP
; Pointer: ES:DI
; Search Criteria: AL
; -------------------------------------------------------------------------
LEA DI, BUFFER
MOV AL, TARGET
MOV CX, B_LEN ; Max bytes to scan
CLD ; Increment DI
; REPNE SCASB: Repeat 'Scan String Byte' While Not Equal AND CX > 0
; Compares AL with ES:[DI] and updates flags.
REPNE SCASB
; Logic check: If ZF is set, it means a match was found.
JNE NOT_FOUND
; Calculate the 1-based position:
; (Original Length - Remaining CX)
MOV AX, B_LEN
SUB AX, CX ; Correct index calculation
; Print success label
PUSH AX ; Save index
LEA DX, MSG_FOUND
MOV AH, 09H
INT 21H
POP AX ; Restore index
; Convert numeric position in AL to ASCII and print (assumes index < 10)
ADD AL, '0'
MOV DL, AL
MOV AH, 02H ; DOS: Print char
INT 21H
JMP EXIT_SUCCESS
NOT_FOUND:
LEA DX, MSG_FAIL
MOV AH, 09H
INT 21H
EXIT_SUCCESS:
; Terminate
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES
; =============================================================================
; 1. ANALOGY:
; - SCASB is the basis for the 'strchr' or 'memchr' functions in high-level
; languages.
; 2. INDEXING:
; - Since SCASB increments DI *after* a match is found, the CX register
; contains (Remaining - 1). The formula (Limit - CX) gives the match position.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =