	OPT c+,l-,o+

******************
*                *
*  MsgEcho 1.0   *
*                *
*  PowerPeak '90 *
******************

	INCDIR "INCLUDE:"
	INCLUDE "exec/types.i"
	INCLUDE "exec/memory.i"
	INCLUDE "exec/ports.i"
	INCLUDE "exec/execbase.i"
	INCLUDE "exec/exec_lib.i"
	INCLUDE "libraries/dos.i"
	INCLUDE "libraries/dos_lib.i"

**** THE PROGRAM ****

	move.b #0,-1(a0,d0.w)
	bsr.s PrintMsg
	lea newline(PC),a0
	bra.s PrintMsg

newline:
	dc.b 10,0

**** THE ACTUAL MSGPRINT ROUTINE ****

; call from C as		'PrintMsg ("foobar");'

; call from ASM as	'lea string(PC),a0'
;							'bsr PrintMsg		'

; Modula-2 and other (stupid ?!?) languages not welcome :')

	XDEF _PrintMsg

_PrintMsg:
	move.l 4(a7),a0				; get argument from the stack
PrintMsg:	
	movem.l d7/a4-a6,-(a7)
	move.l a0,a5
	move.l ($4).w,a6
	jsr _LVOForbid(a6)
	lea portname(PC),a1
	jsr _LVOFindPort(a6)
	move.l d0,a4
	jsr _LVOPermit(a6)
	move.l a4,d0
	beq.s bailout
	move.l a5,a0
strlen:
	tst.b (a0)+
	bne.s strlen
	sub.l a5,a0
	move.l a0,d0					; d0 holds string length
	add.l #MN_SIZE,d0
	move.l d0,d7
	move.l #MEMF_PUBLIC+MEMF_CLEAR,d1
	jsr _LVOAllocMem(a6)
	tst.l d0
	beq.s bailout
	move.l d0,a1
	move.w d7,MN_LENGTH(a1)
	lea MN_SIZE(a1),a0
copyloop:
	move.b (a5)+,(a0)+
	bne.s copyloop
	move.l a4,a0
	jsr _LVOPutMsg(a6)
bailout:
	movem.l (a7)+,d7/a4-a6
	rts
portname:	dc.b 'MsgPrint.port',0
	EVEN

**** END OF ROUTINE ****

	END

