/*		Q/C V3.2 Utility Library		*/
/*     Copyright (c) 1984 Quality Computer Systems	*/
/*			11/25/84			*/

#include "stdio.h"

#ifdef PORTABLE
strncmp(s1, s2, n)
register char *s1, *s2;
	{
	if (n <= 0)
		return 0;
	while (--n > 0 && *s1) {
		if (*s1 != *s2)
			break;
		++s1; ++s2;
		}
	return (*s1 - *s2);
	}
#else

strncmp()
	{
#	asm 8080
	PUSH	B	;save stack frame pointer
	LXI	H,4	;get args off stack
	DAD	SP
	MOV	C,M	;len
	INX	H
	MOV	B,M
	INX	H
	MOV	E,M	;s2
	INX	H
	MOV	D,M
	INX	H
	MOV	A,M	;s1
	INX	H
	MOV	H,M
	MOV	L,A
	XCHG		;reverse args
?sncm1: MOV	A,B	;check number of chars left
	ORA	C
	JZ	?sncm2	;strings match to specified length
	LDAX	D	;current char of s1
	SUB	M	;compute *s1-*s2
	JNZ	?sncm2	;they don't match here
	ORA	M	;end-of-string (0)?
	JZ	?sncm2	;they match to end-to-string
	INX	D	;next char of s1
	INX	H	;same for s2
	DCX	B	;count down chars to compare
	JMP	?sncm1	;loop
?sncm2: MOV	L,A	;set return value (*s1-*s2)
	RLC		;sign-extend into H
	SBB	A
	MOV	H,A
	ORA	L	;set Z flag
	POP	B
#	endasm
	}
#endif

