Metropoli BBS
VIEWER: fmt.s MODE: TEXT (ASCII)
**********************************************************************
* Name:		FMT
* Stack:	(   $ #n #m --> $' )
*		( hxs #n #m --> $' )
* Desc:		Format data encoded in $ or hxs into readable form
*		n = size of single element
*		m = number of elements to put on a line
**********************************************************************
ASSEMBLE
	CON(1)	8
RPL
xNAME FMT
::
  CK3&Dispatch
  #311
  ::
    COERCE2
    GARBAGE				( * Make safe * )
    CODE
DoFmt		GOSBVL	=POP2#
		R3=A	A		R3[A]=element size
		R4=C	A		R4[A]=elements on line
		GOSBVL	=SAVPTR
		GOSBVL	=ROOM
		LA(5)	100		Safety memory
		C=C-A	A
		GONC	+
		GOVLNG	=GPMEMERR
+		C=C+CON	A,16		Allocate atleast 16 nibbles
		R1=C	A		R1[A]=free
		GOSBVL	=MAKE$N		* Is safe *
		A=DAT1	A
		A=A+CON	A,5
		D1=A
		C=DAT1	A
		C=C+A	A
		R2=C	A		R2[A]=->end of input
		D1=D1+	5		->input
		AD0EX
		AD1EX			Make D0 source and D1 input
		AD0EX
fmtloop		C=R4	A
		D=C	A		D[A] = elements on line
fmtloop1	AD0EX
		D0=A
		C=R2	A		C[A] = ->end of input
		?C<=A	A
		GOYES	fmtexit
		GOSUB	fmtone
		D=D-1	A		online--
		?D=0	A
		GOYES	fmtline+
		LCASC	' '
		GOSUB	fmtchr
		GONC	fmtloop1
fmtline+	LCASC	'\n'
		GOSUB	fmtchr
		GONC	fmtloop
fmtexit		AD1EX
		D0=A
		GOSBVL	=Shrink$
		GOVLNG	=GPOverWrR0Lp

* Output a character

fmtchr		CR1EX
		C=C-CON	A,2
		CR1EX
		GOC	fmtfail
		DAT1=C	B
		D1=D1+	2
		RTNCC
fmtfail		GOVLNG	=GPMEMERR

* Output a segment of nibbles

fmtone		A=R3	A
		B=A	A		B[A]=chars in element
		A=A+A	A
		C=R1	A
		C=C-A	A		free--
		GOC	fmtfail
		R1=C	A
		CD1EX
		C=C+A	A		Skip to end of output line
		CD1EX
		B=B-1	A		chars--
fmtoneloop	LCASC	'9'
		A=DAT0	B
		D0=D0+	1
		ACEX	P
		?C<=A	P
		GOYES	+
		C=C+CON	B,7
+		D1=D1-	2
		DAT1=C	B
		B=B-1	A
		GONC	fmtoneloop
		A=R3	A
		A=A+A	A
		CD1EX
		C=C+A	A
		CD1EX
		RTNCC
    ENDCODE
  ;
  #B11
  ::
    COERCE2
    GARBAGE				( * Make safe * )
    CODE
		GOTO	DoFmt
    ENDCODE
  ;
;
**********************************************************************
[ RETURN TO DIRECTORY ]