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

⟦f4219f9f1⟧ TextFile

    Length: 2560 (0xa00)
    Types: TextFile
    Names: »DEC2BN.MAC«

Derivation

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

TextFile

;
;
;
;
;	Title		Decimal ASCII to binary
;	Name:		DEC2BN
;
;
;
;	Purpose:	Convert ASCII characters to two byte of binary
;			data
;
;	Entry:		HL = Base address of input buffer
;
;	Exit:		HL = Binary value
;			if no errors then
;			carry = 0
;			else
;			carry = 1
;
;	Registers used:	AF,BC,DE,HL
;
;	Time:		Approximately 152 cycles per byte plus
;			a maximum of 186 cycles overhead
;
;	Size:		Program  79 bytes
;			Data	  1 byte
;
;
;
DEC2BN:
	;Initialize - save length, clear sign and value
	ld	a,(hl)		;Save lenght in B
	ld	b,a
	inc	hl		;point to byte after lenght
	sub	a
	ld	(ngflag),a	;assume number is positive
	ld	de,0		;Start with value = 0

	;Check for empty buffer
	or	b		;Is buffer lenght zero ?
	jr	z,erexit	;Yes, exit with value = 0

	;Check for minus or plus sign in front
init1:
	ld	a,(hl)		;Get first character
	cp	'-'		;Is it a minus sign ?
	jr	nz,plus		;No, branch
	ld	a,0ffh
	ld	(ngflag),a	;Yes, make sign of number negative
	jr	skip

plus:
	cp	'+'		;Is first character a plus sign ?
	jr	nz,chkdig	;No, start conversion
skip:	inc	hl		;Skip over sign byte
	dec	b		;Decrement count
	jr	z,erexit	;Error exit if only a sign in buffer

	;Conversion loop
	;  continue until the buffer is empty
	;  or a non-numeric character is found
cnvert:
	ld	a,(hl)		;Get next character
chkdig:	sub	'0'
	jr	c,erexit	;Error if <'0' (Not a digit)
	cp	9+1
	jr	nc,erexit	;Error if >'9' (Not a digit)
	ld	c,a		;Character is digit, save it

	;Valid decimal digit so
	;  value *= value *10
	;   = value * (8+2)
	;   = (value * 8) + (value * 2)
	push	hl		;Save buffer pointer
	ex	de,hl		;HL = value
	add	hl,hl		; * 2
	ld	e,l		;save times 2 in DE
	ld	d,h
	add	hl,hl		; * 4
	add	hl,hl		; * 8
	add	hl,de		;Value = value * (8*2)

	;Add in the next digit
	;  value := value + digit
	ld	e,c		;Move next digit to E
	ld	d,0		; high byte is 0
	add	hl,de		;Add digit to value
	ex	de,hl		;DE = value
	pop	hl		;Point to next character
	inc	hl
	djnz	cnvert		;Continue conversion

	;Conversion is complete, check sign
	ex	de,hl		;HL = value
	ld	a,(ngflag)
	or	a
	jr	z,okexit	;Jump if the value was positive
	ex	de,hl		;Else replace value with -value
	ld	hl,0
	or	a		;Clear carry
	sbc	hl,de		;Subtract value from 0

	;No errors, exit with carry clear
okexit:
	or	a		;Clear carry
	ret

	;An error, exit with carry set
erexit:
	ex	de,hl		;HL = value
	scf			;Set carry
	ret

;DATA
ngflag:	ds	1		;Sign of number
«eof»