;*************************************************************************************
; MODULE: STRING.INC                                    DATE: FEBRUARY 2, 1997
; AUTHOR: SEAN M. KESSLER
; TARGET: 32 BIT FLAT MODEL
; FUNCTION : STRING RELATED MACROS
;*************************************************************************************
STRSTR MACRO szStringOne,szStringTwo
  push offset szStringTwo              ; save string one 
	push offset szStringOne							 ; save string two
	call _strstr												 ; call standard library strstr
	add esp,08h													 ; reset stack frame
ENDM

STRCHR MACRO szString,charByte
  push charByte                        ; save charByte
	push offset szString								 ; save string
	call _strchr												 ; call standard library strchr
	add esp,08h													 ; reset stack frame
ENDM

STRCAT MACRO szDstString,szSrcString
  push offset szSrcString              ; save source string
	push offset szDstString							 ; save destination string
	call _strcat												 ; call standard library strcat
	add esp,08h                          ; reset stack frame
ENDM

STRLEN MACRO szData
  push edi                             ; save destination index register
	push ecx														 ; save ecx register
	mov edi,offset szData								 ; get string to destination index register
	mov ecx,0FFFFh											 ; move 65535 to ecx register
	xor eax,eax													 ; clear eax register
	repnz scasb                          ; scan string
	mov eax,0FFFFh											 ; move 65535 to eax
	sub eax,ecx													 ; subtract number of bytes scanned
	dec eax															 ; decrement result
	pop ecx															 ; restore ecx register
	pop edi															 ; restore destination index register
ENDM

MEMCPY MACRO szDstData,szSrcData,lengthCopy
	push esi                             ; save source index register
	push edi														 ; save destination index register
	mov esi,offset szSrcData						 ; move source data to source index register
  mov edi,offset szDstData						 ; move destination data to destination index register
	mov ecx,lengthCopy                   ; move number of bytes to copy to ecx register
	rep movsb														 ; copy data
	pop edi															 ; restore destination index register
	pop esi															 ; restore source index register
ENDM

MEMSET MACRO szBuffer,setFill,length
  push edi                             ; save destination index register
  mov edi,szBuffer                     ; move szBuffer into edi register
	mov eax,setFill                      ; move value to set into eax register
	mov ecx,length                       ; move count of bytes to ecx register
	rep stosb                            ; set data
	pop edi                              ; restore destination index register
ENDM

STRCMP MACRO szStringOne,szStringTwo
LOCAL @@STRCMPnotEqual,@@STRCMPequal,@@STRCMPexit
  push ecx                             ;  save ecx register
	push edi														 ; save destination index register
	push esi														 ; save source index register
	mov edi,offset szStringOne					 ; move string one to destination index register
	mov esi,offset szStringTwo					 ; move string two to source index register
  STRLEN szStringOne                   ; get length of string one
	mov ecx,eax													 ; save length to ecx register
	STRLEN szStringTwo									 ; get length of string two
	cmp ecx,eax													 ; compare lengths
	jne @@STRCMPnotEqual								 ; if lengths differ, strings are not equal
@@STRCMPloop:													 ; loop control
  mov al,byte ptr[esi]								 ; get byte from string two
	cmp al,byte ptr[edi]								 ; compare with byte from string one
	jne @@STRCMPnotEqual                 ; if bytes differ then we're done 
  loop @@STRCMPloop										 ; iterate through string length
@@STRCMPequal:												 ; equality control
  mov eax,0001h												 ; set return code
	jmp @@STRCMPexit										 ; we're done
@@STRCMPnotEqual:											 ; inequality control
  xor eax,eax                          ; set return code
@@STRCMPexit:													 ; exit sync address
  pop esi                              ; restore source index register
	pop edi															 ; restore destination index register
  pop ecx															 ; restore ecx register
ENDM                                   

STRNCMP MACRO szStringOne,szStringTwo
LOCAL @@STRNCMPloop,@@STRNCMPequal,@@STRNCMPnotEqual,@@STRNCMPexit
  push ecx														 ; save ecx register
	push edi														 ; save edi register
	push esi														 ; save source index register
	mov edi,offset szStringOne					 ; move string one to destination index register
	mov esi,offset szStringTwo					 ; move string two to source index register
	STRLEN szStringOne									 ; get length of string one
	mov ecx,eax													 ; save length to ecx register
	STRLEN szStringTwo									 ; get length of string two
	cmp ecx,eax													 ; compare the lengths
	jle @@STRNCMPloop										 ; string one is less equal to string two
	mov ecx,eax													 ; string two is less, so use it's length
@@STRNCMPloop:                         ; loop control
  mov al,byte ptr[esi]                 ; get byte from string one
	cmp al,byte ptr[edi]								 ; compare with byte from string two
	jne @@STRNCMPnotEqual								 ; if byte are unequal, strings are unequal
	inc edi															 ; increment along string two
	inc esi															 ; increment along string one
  loop @@STRNCMPloop                   ; keep going
@@STRNCMPequal:                        ; string equal sync address
  mov eax,0001h												 ; set return code
	jmp @@STRNCMPexit										 ; we're done
@@STRNCMPnotEqual:										 ; string unequal sync address
  xor eax,eax													 ; set return code
@@STRNCMPexit:												 ; exit sync address
  pop esi                              ; restore source index register
	pop edi															 ; restore destination index register
  pop ecx                              ; restore ecx register
ENDM
extrn _strstr:near
extrn _strchr:near
extrn _strcat:near
