str.S revision 1.6
11.6Sragge/*	$NetBSD: str.S,v 1.6 2017/05/22 17:00:19 ragge Exp $ */
21.1Smatt/*
31.1Smatt * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
41.1Smatt * All rights reserved.
51.1Smatt *
61.1Smatt * Redistribution and use in source and binary forms, with or without
71.1Smatt * modification, are permitted provided that the following conditions
81.1Smatt * are met:
91.1Smatt * 1. Redistributions of source code must retain the above copyright
101.1Smatt *    notice, this list of conditions and the following disclaimer.
111.1Smatt * 2. Redistributions in binary form must reproduce the above copyright
121.1Smatt *    notice, this list of conditions and the following disclaimer in the
131.1Smatt *    documentation and/or other materials provided with the distribution.
141.1Smatt *
151.1Smatt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161.1Smatt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171.1Smatt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181.1Smatt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191.1Smatt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
201.1Smatt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
211.1Smatt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
221.1Smatt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
231.1Smatt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241.1Smatt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251.1Smatt */
261.1Smatt
271.1Smatt/*
281.1Smatt * Small versions of the most common functions not using any
291.1Smatt * emulated instructions.
301.1Smatt */
311.1Smatt
321.1Smatt#include "asm.h"
331.1Smatt
341.1Smatt/*
351.1Smatt * atoi() used in devopen.
361.1Smatt */
371.1SmattENTRY(atoi, 0)
381.1Smatt	movl	4(%ap),%r1
391.1Smatt	clrl	%r0
401.1Smatt
411.1Smatt2:	movzbl	(%r1)+,%r2
421.1Smatt	cmpb	%r2,$48
431.1Smatt	blss	1f
441.1Smatt	cmpb	%r2,$57
451.1Smatt	bgtr	1f
461.1Smatt	subl2	$48,%r2
471.1Smatt	mull2	$10,%r0
481.1Smatt	addl2	%r2,%r0
491.1Smatt	brb	2b
501.1Smatt1:	ret
511.1Smatt
521.1Smatt/*
531.4Sjdolecek * strchr() small and easy.
541.1Smatt */
551.4SjdolecekENTRY(strchr, 0)
561.1Smatt	movq	4(%ap),%r0
571.3Sragge1:	cmpb	(%r0), 8(%ap)
581.1Smatt	beql	2f
591.1Smatt	tstb	(%r0)+
601.1Smatt	bneq	1b
611.1Smatt	clrl	%r0
621.1Smatt2:	ret
631.1Smatt
641.1Smatt/*
651.1Smatt * cmpc3 is emulated on MVII.
661.1Smatt */
671.2SdslENTRY(memcmp, 0)
681.2Sdsl	brb	10f
691.1SmattENTRY(bcmp, 0)
701.2Sdsl10:	movl	4(%ap), %r2
711.1Smatt	movl	8(%ap), %r1
721.1Smatt	movl	12(%ap), %r0
731.1Smatt2:	cmpb	(%r2)+, (%r1)+
741.1Smatt	bneq	1f
751.3Sragge	sobgtr	%r0, 2b
761.2Sdsl3:	ret
771.2Sdsl1:	bgtru	5f
781.2Sdsl	movl	$-1, %r0
791.2Sdsl	brb	3b
801.2Sdsl5:	movl    $1, %r0
811.2Sdsl	ret
821.1Smatt
831.1Smatt/*
841.3Sragge * movc can't do length in excess of 64K, so we shall not use them.
851.1Smatt */
861.1SmattENTRY(bzero,0)
871.3Sragge	movl	4(%ap),%r0
881.3Sragge	movl	8(%ap),%r1
891.3Sragge1:	clrb	(%r0)+
901.3Sragge	sobgtr	%r1,1b
911.1Smatt	ret
921.1Smatt
931.3Sragge/*
941.3Sragge * memcpy and bcopy are the same, except for argument order. Silly stuff.
951.3Sragge */
961.3SraggeENTRY(memcpy,0)
971.3Sragge	movl	8(%ap),%r0
981.3Sragge	movl	4(%ap),%r1
991.3Sragge	brb	1f
1001.1SmattENTRY(bcopy,0)
1011.3Sragge	movl	4(%ap),%r0
1021.3Sragge	movl	8(%ap),%r1
1031.3Sragge1:	movl	12(%ap),%r2
1041.3Sragge	cmpl	%r0,%r1
1051.3Sragge	bgtru	3f
1061.3Sragge	addl2	%r2,%r0
1071.3Sragge	addl2	%r2,%r1
1081.3Sragge2:	movb	-(%r0),-(%r1)
1091.3Sragge	sobgtr	%r2,2b
1101.3Sragge	ret
1111.3Sragge3:	movb	(%r0)+,(%r1)+
1121.3Sragge	sobgtr	%r2,3b
1131.1Smatt	ret
1141.1Smatt
1151.2SdslENTRY(memset,0)
1161.3Sragge	movl	4(%ap),%r0
1171.3Sragge	movl	12(%ap),%r1
1181.3Sragge1:	movb	8(%ap),(%r0)+
1191.3Sragge	sobgtr	%r1,1b
1201.2Sdsl	ret
1211.2Sdsl
1221.1SmattENTRY(strlen, 0)
1231.1Smatt	movl	4(%ap), %r0
1241.1Smatt1:	tstb	(%r0)+
1251.1Smatt	bneq	1b
1261.1Smatt	decl	%r0
1271.1Smatt	subl2	4(%ap), %r0
1281.1Smatt	ret
1291.1Smatt
1301.1SmattENTRY(strncmp, 0)
1311.1Smatt	movl	12(%ap), %r3
1321.2Sdsl	bneq	5f
1331.2Sdsl	brb	4f
1341.1Smatt
1351.1SmattENTRY(strcmp, 0)
1361.1Smatt	movl	$250, %r3	# max string len to compare
1371.1Smatt5:	movl	4(%ap), %r2
1381.1Smatt	movl	8(%ap), %r1
1391.1Smatt	movl	$1, %r0
1401.1Smatt
1411.1Smatt2:	cmpb	(%r2),(%r1)+
1421.1Smatt	bneq	1f		# something differ
1431.1Smatt	tstb	(%r2)+
1441.1Smatt	beql	4f		# continue, strings unequal
1451.1Smatt	decl	%r3		# max string len encountered?
1461.1Smatt	bneq	2b
1471.1Smatt
1481.1Smatt4:	clrl	%r0		# We are done, strings equal.
1491.1Smatt	ret
1501.1Smatt
1511.2Sdsl1:	bgtru	3f
1521.1Smatt	mnegl	%r0, %r0
1531.1Smatt3:	ret
1541.1Smatt
1551.1SmattENTRY(strncpy, 0)
1561.1Smatt	movl	4(%ap), %r1
1571.1Smatt	movl	8(%ap), %r2
1581.1Smatt	movl	12(%ap), %r3
1591.1Smatt	bleq	2f
1601.1Smatt
1611.1Smatt1:	movb	(%r2)+, (%r1)+
1621.1Smatt	beql	2f
1631.3Sragge	sobgtr	%r3,1b
1641.3Sragge	ret
1651.3Sragge2:	decl	%r1
1661.3Sragge3:	clrb	(%r1)+
1671.3Sragge	sobgtr	%r3,3b
1681.3Sragge	ret
1691.1Smatt
1701.1SmattENTRY(strcat, 0)
1711.1Smatt	pushl	4(%ap)
1721.1Smatt	calls	$1,_C_LABEL(strlen)
1731.1Smatt	addl2	4(%ap),%r0
1741.1Smatt	movl	8(%ap),%r1
1751.1Smatt1:	movb	(%r1)+,(%r0)+
1761.1Smatt	bneq	1b
1771.1Smatt	ret
1781.1Smatt
1791.1SmattENTRY(setjmp, 0)
1801.1Smatt	movl	4(%ap), %r0
1811.1Smatt	movl	8(%fp), (%r0)
1821.1Smatt	movl	12(%fp), 4(%r0)
1831.1Smatt	movl	16(%fp), 8(%r0)
1841.1Smatt	addl3	%fp,$28,12(%r0)
1851.1Smatt	clrl	%r0
1861.1Smatt	ret
1871.1Smatt
1881.1SmattENTRY(longjmp, 0)
1891.1Smatt	movl	4(%ap), %r1
1901.1Smatt	movl	8(%ap), %r0
1911.1Smatt	movl	(%r1), %ap
1921.1Smatt	movl	4(%r1), %fp
1931.1Smatt	movl	12(%r1), %sp
1941.1Smatt	jmp	*8(%r1)
1951.1Smatt
196