*******************************************************************************
* This quite useless prog takes a number n between 0 and 9 as input and
* prints a string (labelled with n) n+1 times.
* Compile with: "ESA example.esa" and then assemble "example.s" with
* your favourite assembler.
* CLI only.
*******************************************************************************

	machine	68020

	section	ESAcode,CODE

*******************************************************************************
* some inits

	move.l d0,ArgLen § move.l a0,ArgPtr	;cmdline

	movea.l 4.w,a6 § moveq.l #0,d0
	lea.l	DosName,a1
	jsr	(-552,a6)	;open dos.library
	move.l d0,DosBase § beq end
	movea.l	d0,a6
	jsr	(-60,a6)
	move.l d0,StdOut § beq .closedos	;get output handle

*******************************************************************************
* arg check

	when	#1ĞArgLen	;any arg in cmdline?
	moveq.l	#-48,d0	;get arg #
	movea.l	ArgPtr,a0
	add.b	(a0),d0	;get numerical arg

	when	{#0ğd0.b} | {#9Ğd0.b}	;is it correct?
	print(#te)
	bra	.closedos
	ewhen

*******************************************************************************
* silly way of selecting text

	extb.l	d0
	on d0.l,a0 gosub (.l0,.l1,.l2,.l3,.l4,.l5,.l6,.l7,.l8,.l9)
	bra.s	.printloop

.l0	lea.l t0,a0 § rts
.l1	lea.l t1,a0 § rts
.l2	lea.l t2,a0 § rts
.l3	lea.l t3,a0 § rts
.l4	lea.l t4,a0 § rts
.l5	lea.l t5,a0 § rts
.l6	lea.l t6,a0 § rts
.l7	lea.l t7,a0 § rts
.l8	lea.l t8,a0 § rts
.l9	lea.l t9,a0 § rts

*******************************************************************************
* show results

.printloop	for d3.b=#0 upto d0
	print(a0)
	next.s

	othw

	print(#te)

	ewhen

*******************************************************************************
* cleanup

.closedos	movea.l DosBase,a1 § movea.l 4.w,a6
	jsr	(-414,a6)	;CloseLibrary()
end	rts

*******************************************************************************
* print routine

	procedure print(a0),d0-d3/a0-a1/a6
	move.l	StdOut,d1
	move.l	a0,d2
	move.l	a0,d3
.getlen	tst.b	(a0)+
	bne.s	.getlen
	subq.l	#1,a0
	sub.l	a0,d3
	neg.l	d3
	movea.l	DosBase,a6
	jsr	(-48,a6)	;Write()
	eproc

*******************************************************************************
* data

	section	MiscData,DATA

DosBase	dc.l	0
StdOut	dc.l	0
ArgLen	dc.l	0
ArgPtr	dc.l	0

DosName	dc.b	"dos.library",0
t0	dc.b	"text0",10,0
t1	dc.b	"text1",10,0
t2	dc.b	"text2",10,0
t3	dc.b	"text3",10,0
t4	dc.b	"text4",10,0
t5	dc.b	"text5",10,0
t6	dc.b	"text6",10,0
t7	dc.b	"text7",10,0
t8	dc.b	"text8",10,0
t9	dc.b	"text9",10,0
te	dc.b	"arg required: 0...9",10,0
