Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
StrCompare
◄Example► ◄Back► ◄Contents► ◄Index►
──────────────────────────────────────────────────────────────────────────────
;* StrCompare - Compares two strings for equality. See StrWrite, StrFindChar,
;* WinOpen, and WinClose procedures for other examples of string instructions.
;*
;* Shows: Instructions - cmpsb cmpsw repe test jcxz
;*
;* Params: str1 - Pointer to first string
;* str2 - Pointer to second string
;* len - Length in bytes for comparison. Strings need not be of
;* equal length; however if len is an even number, comparison
;* is made on a word-by-word basis and thus is more efficient.
;*
;* Return: Null pointer if strings match; else pointer to string #1 where
;* match failed.
StrCompare PROC \
USES ds di si, \
str1:PTR BYTE, str2:PTR BYTE, len:WORD
LoadPtr es, di, str1 ; ES:DI points to string #1
LoadPtr ds, si, str2 ; DS:SI points to string #2
mov cx, len ; Length of search in bytes
and al, 0 ; Set ZR flag in case CX = 0
jcxz nullp ; Assume success if length = 0
test cl, 1 ; Even number?
jz wrdcmp ; Yes? Compare word-by-word
repe cmpsb ; No? Compare byte-by-byte
jmp SHORT nullp
wrdcmp: shr cx, 1 ; Decrease count by half
repe cmpsw ; Compare word-by-word
sub di, 2 ; Back up 2 characters
sub si, 2
cmpsb ; Match?
jne nullp ; No? Then failure
cmpsb ; Compare last characters
nullp: mov ax, 0 ; Set null pointer without
mov dx, 0 ; disturbing flags
je exit ; If strings match, exit
dec di ; Else point to failure
mov ax, di
mov dx, es
exit: ret
StrCompare ENDP
-♦-