; strncmpi.asm		Compare two strings without regard to case.  Comparison is
;					halted when a) either string is exhausted or b) the limit
;					specified by the third parameter is reached.  If limit is 
;					0 strings are equal.
;
			public	strncmpi
strncmpi_asm	segment	'code'
			assume	cs:strncmpi_asm

strncmpi	proc	far

s1			equ		dword ptr [bp+0ch]		; parameter 1 first string
s2			equ		dword ptr [bp+08h]		; parameter 2 second string
lim			equ		word ptr [bp+06h]		; parameter 3 limit of compare

			push	bp						; Setup stack pointers, and save
			mov		bp,sp					; registers we're using.
			push	si						;
			push	di						;
			push	ds						;

			mov		cx,lim					; Compare at most this many.
			jcxz	equalto					; 0 to compare? They must be equal.
			cld								; Clear direction flag.
			lds		si,s1					; Setup pointers to strings.
			les		di,s2					;

cmploop:								; Compare loop
			lodsb							; Get string 1 char into AX
			cmp		al,'a'					; If it's alphabetic, convert
			jl		tu_done					; it to lower case in AL
			cmp		al,'z'					;
			jg		tu_done					;
			sub		al,'a''A'				;

tu_done:
			mov		ah,es:byte ptr [di]		; Get char from string 2 into AH
			inc		di						; Bump pointer to next char (DI)
			cmp		ah,'a'					; If char is alphabetic, convert
			jl		tu_done1				; to lowercase in AH
			cmp		ah,'z'					;
			jg		tu_done1				;
			sub		ah,'a''A'				;

tu_done1:
			cmp		al,ah					; Compare AH AL
			jne		notequal				; Jump, if not equal.
			test	al,al					; Reached end of strings?
			jz		equalto					; Yes, they're equal, jump.
			loop	cmploop				; Compare loop, again, if not.

equalto:
			sub		ax,ax					; Return 0 for equal strings.
			jmp		done					; 

notequal:
			mov		ax,0					; Zero into AX. Flag set above.
			jb		less					; String 1 was less, jump.
			inc		ax						; String 1 was greater, set AX = 1.
			jmp		done					;

less:
			dec		ax						; Set AX = 1.

done:
			pop		ds						; Cleanup and return.
			pop		di
			pop		si
			pop		bp
			ret		0ah

strncmpi		endp
strncmpi_asm	ends
			end
UFVtF