-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstring_comparison_lexicographical_check.asm
More file actions
81 lines (70 loc) · 3.09 KB
/
string_comparison_lexicographical_check.asm
File metadata and controls
81 lines (70 loc) · 3.09 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
;=============================================================================
; Program: Compare Two Strings
; Description: Compare two strings using CMPSB instruction.
; Demonstrates string comparison operations.
;
; Author: Amey Thakur
; Repository: https://github.com/Amey-Thakur/8086-ASSEMBLY-LANGUAGE-PROGRAMS
; License: MIT License
;=============================================================================
;-----------------------------------------------------------------------------
; DATA SEGMENT
;-----------------------------------------------------------------------------
DATA SEGMENT
STR1 DB 'HELLO$' ; First string
STR2 DB 'HELLO$' ; Second string
LEN EQU 5 ; String length
MSG_SAME DB 0DH, 0AH, 'Strings are EQUAL$'
MSG_DIFF DB 0DH, 0AH, 'Strings are NOT EQUAL$'
DATA ENDS
;-----------------------------------------------------------------------------
; CODE SEGMENT
;-----------------------------------------------------------------------------
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, ES:DATA
START:
; Initialize Segments
MOV AX, DATA
MOV DS, AX
MOV ES, AX ; ES = DS for CMPSB
;-------------------------------------------------------------------------
; Setup String Pointers
;-------------------------------------------------------------------------
LEA SI, STR1 ; Source string (DS:SI)
LEA DI, STR2 ; Destination string (ES:DI)
MOV CX, LEN ; String length
;-------------------------------------------------------------------------
; Compare Strings
; CLD: Forward direction (increment SI/DI)
; REPE CMPSB: Repeat Compare String Byte while Equal
;-------------------------------------------------------------------------
CLD ; Clear direction flag
REPE CMPSB ; Compare bytes until mismatch or CX=0
;-------------------------------------------------------------------------
; Check Result
; If ZF=1, strings are equal
;-------------------------------------------------------------------------
JE EQUAL ; Jump if strings are equal
; Strings are different
LEA DX, MSG_DIFF
JMP DISPLAY
EQUAL:
LEA DX, MSG_SAME
DISPLAY:
MOV AH, 09H
INT 21H
;-------------------------------------------------------------------------
; Program Termination
;-------------------------------------------------------------------------
MOV AH, 4CH
INT 21H
CODE ENDS
END START
;=============================================================================
; STRING COMPARE NOTES:
; - CMPSB: Compare byte at DS:SI with ES:DI
; - CMPSW: Compare word at DS:SI with ES:DI
; - REPE: Repeat while Equal (ZF=1) AND CX!=0
; - REPNE: Repeat while Not Equal (ZF=0) AND CX!=0
; - After REPE CMPSB, ZF=1 means all bytes matched
;=============================================================================