DataMuseum.dk

Presents historical artifacts from the history of:

Jet Computer Jet80

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about Jet Computer Jet80

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦a628b3846⟧ TextFile

    Length: 3840 (0xf00)
    Types: TextFile
    Names: »COPY.MAC«

Derivation

└─⟦01b5c9619⟧ Bits:30005906 Microsoft Multiplan v1.05 og HELP
    └─ ⟦this⟧ »COPY.MAC« 

TextFile

;
;
;	Title		Copy a substring from a string
;	Name:		COPY
;
;
;	Purpose:	Copy a substring from a string given a starting
;			index and the number of bytes
;
;	Entry:		Register pair HL = address of source string
;			Register pair DE = address of destination string
;			Register A = maximum length of destination string
;			Register B = number of bytes to copy
;			Register C = starting index into sourc string
;				     Index 1 is first character of string
;
;			  A string is a maximum of 255 bytes long plus
;			  a length byte which precedes it.
;
;	Exit:		Destination string := The substring from the string
;			if no errors then
;			  CARRY := 0
;			else
;			  begin
;			    the following conditions cause an
;			    error and the CARRY flag = 1.
;			    if (index = 0) or (maxlen = 0) or
;			        (index > length(source)) then
;			      the destination string will have a zero
;			      length
;			    if (index + count - 1) > length (source)
;			    then
;			      the destination string becomes everything
;			      from index to the end of source string.
;			END;
;
;	Registers used:	AF,BC,DE,HL
;
;	Time:		Approximately (21 * count) cycles plus 237
;			cycles overhead.
;
;	Size:		Program 73 bytes
;			Data     2 bytes
;
;
;
COPY:
	;Save maximum length of destination string
	ld	(maxlen),a	;Save maximum length

	;Initialize length of destination string and error flag
	sub	a
	ld	(de),a		;Lenght of destination string = zero
	ld	(cpyerr),a	;Assume no errors

	;If number of bytes to copy is 0, exit with no errors
	or	b		;Test number of bytes to copy
	ret	z		;Exit with no errors
				;CARRY := 0;

	;If maximum length is 0, take error exit
	ld	a,(maxlen)	;Test maximum length
	or	a
	jr	z,erexit	;Error exit if index is 0

	;If starting index is greater than length of source
	;  source string, take error exit
	ld	a,(hl)		;Get length of source string
	cp	c		;Compare to starting index
	ret	c		;Error exit if length less than index
				;CARRY := 1;

	;Check if copy area fits in source string
	;otherwise, copy only to end of string
	;copy area fits if starting index + number of
	;  characters to copy - 1 is less than or equal to
	;  length of source string
	;NOTE that strings are never more than 255 bytes long
	ld	a,c		;Form starting index + copy length
	add	a,b
	jr	c,recalc	;Jump if sum > 255
	dec	a
	cp	(hl)
	jr	c,cnt1ok	;Jump if more than enough to copy
	jr	z,cnt1ok	;Jump if exactly enough

	;Caller asked for too too many characters. Return everything
	;  between index and end of source string.
	;  set count := lenghthsource) - index + 1;
recalc:
	ld	a,0ffh		;Indicate truncation of count
	ld	(cpyerr),a
	ld	a,(hl)		;Count = lenght - index + 1
	sub	c
	inc	a
	ld	b,a		;Change number of bytes

	;Check if count less than or equal to maximum length of
	;  destination string. If not, set count to maximum length
	;    if count > maxlen then
	;      count := maxlen;
cnt1ok:
	ld	a,(maxlen)	;Is max length large enough ?
	cp	b
	jr	nc,cnt2ok	;Jump if it is
	ld	b,a		;Else limit copy to maxlen
	ld	a,0ffh		;Indicate string overflow
	ld	(cpyerr),a

	;Move substring to destination string
cnt2ok:
	ld	a,b		;Test number of bytes to move
	or	a
	jr	z,erexit	;Error exit if no bytes to copy
	ld	b,0		;Start copying at starting index
	add	hl,bc
	ld	(de),a		;Set length of destination string
	ld	c,a		;Restore number of bytes
	inc	de		;Move destination address past
				;  length byte
	ldir			;Copy substring

	;Check for copy error
	ld	a,(cpyerr)	;Test for errors
okexit:
	or	a
	ret	z		;Return with c = 0 if no errors

	;Error exit
erexit:
	scf			;Set carry to indicate an error
	ret

	;DATA section
maxlen:	ds	1		;Maximum lenght of destination string
cpyerr:	ds	1		;Copy error flag


«eof»