DataMuseum.dk

Presents historical artifacts from the history of:

CP/M

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

See our Wiki for more about CP/M

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦a55414d2d⟧ TextFile

    Length: 3200 (0xc80)
    Types: TextFile
    Names: »CONCAT.MAC«

Derivation

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

TextFile

;
;
;	Title		String Concation
;	Name:		CONCAT
;
;	Purpose:	Concateenate 2 strings into one string
;
;	Entry:		Register pair HL = base address of string 1
;			Register pair DE = base address of string 2
;			Register B = maximum lenght of string 1
;
;			  A string is maximum of 255 bytes long plus
;			  a length byte witch precedes it.
;
;	Exit:		String 1 := string 1 concatenated with string 2
;			if no errors then
;			  CARRY := 0
;			else
;			  begin
;			    CARRY := 1
;			    if the concatenation makes string 1 too
;			    long, concatenate only enough of string 2
;			    to give string 1 its maximum lenght.
;			    if lenght(string1) > maximum lenght then
;			      no concatenation is done
;			end;
;
;	Registers used:	AF,BC,DE,HL
;
;	Time:		Approximately 21 * (lenght of string 2) cycles
;			plus 288 cycles overhead
;
;	Size:		Program 83 bytes
;			Data     5 bytes
;
;
;
CONCAT:
	;Determine where to start concatenating
	;Concatemation starts at end of string 1
	;End of string 1 = base1 + lenght1 + 1, where
	;  the extra 1 makes up for the lenght byte
	;New characters come from string 2, starting at
	;  base2 + 1 (skipping over lenght byte)
	ld	(s1adr),hl	;Save address of string 1
	push	bc		;Save maximum lenght of string 1
	ld	a,(hl)		;Save lenght of string 1
	ld	(s1len),a
	ld	c,a		;End1 = base1 + lenght1 + 1
	ld	b,0
	add	hl,bc
	inc	hl		;HL = start of concatenation
	ld	a,(de)		;Save lenght of string 2
	ld	(s2len),a
	inc	de		;DE = first character of string 2
	pop	bc		;Restore maximum lenght

	;Determine how many characters to concatenate
	ld	c,a		;Add lengths of strings
	ld	a,(s1len)
	add	a,c
	jr	c,toolng	;Jump if sum exceeds 255
	cp	b		;Compare to maximum lenght
	jr	z,lenok		;Jump if new string is max lenght
	jr	c,lenok		;  or less

	;Combined string id too long
	;  indicate a string overflow, strgov := 0ffh
	;  number of characteres to concatenate = maxlen - s1len
	;  length of string 1 = maximum length
toolng:
	ld	a,0ffh		;Indicate string overflow
	ld	(strgov),a
	ld	a,(s1len)	;Calculate maxlen - s1len
	ld	c,a
	ld	a,b
	sub	c
	ret	c		;Exit if original string too long
	ld	(s2len),a	;Change s2len to maxlen - s1len
	ld	a,b		;Lenght of string 1 = maximum
	ld	(s1len),a
	jr	docat		;Perform concatenation

	;Resulting lenght does not exceed maximum
	;  length of string 1 = s1len + s2len
	;  indicate no overflow, strgov := 0
	;  number of characters to concatenate = length of string 2
lenok:
	ld	(s1len),a	;Save sum of length
	sub	a		;Indicate no overflow
	ld	(strgov),a

	;Concatenate strings by moving characters from string 2
	;  to end of string 1
docat:
	ld	a,(s2len)	;Get number of characters
	or	a
	jr	z,exit		;Exit if nothing to concatenate
	ld	c,a		;BC = number of characters
	ld	b,0
	ex	de,hl		;DE = destination
				;HL = source
	ldir			;Move characters
exit:
	ld	a,(s1len)	;Establish new lenght of string 1
	ld	hl,(s1adr)
	ld	(hl),a
	ld	a,(strgov)	;Carry = if overflow, 0 if not
	rra
	ret

	;DATA
s1adr:	ds	2		;Base address of string 1
s1len:	ds	1		;Length of string 1
s2len:	ds	1		;Length of string 2
strgov:	ds	1		;String overflow flag