; Amiga User International
; QUAD - September 1991
;
; How to print numbers in machine code - Part 2, the Better Way
;
; By storing a complete character set in memory, a program can
; print out the digits needed for each number. For example, if the
; number to be printed is 12345, then the '1' is printed first,
; followed by the '2' and so on. It may sound obvious, but getting
; the '1' and the '2' out from the number is tricky, and requires
; some maths. In the example source that follows, use is made of
; the M68000 'div' (divide) instruction, so speed is not of the
; essence. HOWEVER, it works - which is the main thing - and once
; you have sussed it out, you'll be able to write your own version.
;
;
; What you'll need to make use of this code...
;
; A DevPack compatable assembler, and some code that will allow
; you to set up a single plane bitmap screen of 40 bytes width 
; (that's low res to you).
;
; Make sure you use the binary file which came with this one -
; created with DPaint and hacked with
; the programmer's friend: PixMate - is available to the source.
; Better still, design your own.
;
; If you want to see this routine in action (after all, why belive
; me when I say it works) look for it in the AUI Coverdisk #2 game
; called Squamble, written by the BitPlane Brothers and me. In SQUAMBLE
; use of a copper list means the numbers are all different colours.
;
; Right. That's enough to be going on with. Any problems, just
; drop a line to the usual AUI address and ask for the Chief
; Genius. Or me.
;
; JK 22/6/91






printnumber

	;Action:
	;	Takes a five digit number in d1 and prints it to the
	;	screen in decimal format. Leading zeros are printed
	;	as necessary. The routine 'number' is used to print
	;	each digit in turn.
	;In:
	;	d1 = ?????.l
	;   a0 = screen address
	;Out:
	;   d0,d1,d2,a0 trashed
	
	moveq #0,d0

	move.l d1,d0
	divu #10000,d0
	move.l d0,d2
	mulu #10000,d0
	sub.w d0,d1
	move.l d2,d0
	and.l #15,d0
	bsr.s number

	move.l d1,d0
	divu #1000,d0
	move.l d0,d2
	mulu #1000,d0
	sub.w d0,d1
	move.l d2,d0
	and.l #15,d0
	bsr.s number

	move.l d1,d0
	divu #100,d0
	move.l d0,d2
	mulu #100,d0
	sub.w d0,d1
	move.l d2,d0
	and.l #15,d0
	bsr.s number


	move.l d1,d0
	divu #10,d0
	move.l d0,d2
	mulu #10,d0
	sub.w d0,d1
	move.l d2,d0
	and.l #15,d0
	bsr.s number

	move.l d1,d0
	and.l #15,d0
	bsr.s number

	rts


number

	;Print a number
	;In:
	;	d0 = digit 0.l to 9.l
	;	a0 = screen address
	;Out:
	;	a0 = screen address to right (unless at edge, when its junked)
	;	a1 = trashed
	;	d0 = trashed

	move.l a0,-(sp)			;Preserve a0 by stacking it
	
	move.l #digits+892,a1	;The start of the number data
	lsl.w #5,d0				;Multiply by 16 to get correct digit
	add.l d0,a1				;Now a1 points to correct data digit
	
	move.l #15,d0			;There are sixteen (15+1) lines of data
nl1	move.w (a1)+,(a0)		;Poke it into screen ram
	add.l #40,a0			;Assumes a bitplane width of 40 bytes!
	dbf d0,nl1				;Do next line

	move.l (sp)+,a0			;Get a0 back, and increase to left
	addq #2,a0
	rts

digits
	incbin "lettersdata.bin"		;Load the binary data containing
									;the miniature character set wot
									;I wrote. This file has also been
									;supplied for your own use.
