// Assembly and emulation test program
// This program displays the numbers from
// 0 to 32

main:
	LDI		R0, 0		// the number to display
	LDI		R1, 1		// one for add
	LDI		R2, 20		// last value to display (32)


mainlp:
	CALLA		dispnum:	// display the number in r0
	CALLA		newline:	// line feed

	ADD		R0, R1		// next number
	CMP		R0, R2		// compare with last

	BLE		mainlp:		// not above? loop again

	HALT			// end of emulation



// This displays the number in R0
dispnum:
	LDI		R2, A		// 10 for division
	LDI		R4, 0		// 0 for compare

	LDI		R5, 1		//  1 for add
	LDI		R6, 0		// digit counter

displp1:

	LDR		R1, R0		// copy number into r1
	MOD		R1, R2		//	remainder when div by 10
	LDI		R3, 30		// base start of numbers '0'
	ADD		R1, R3		// add to remainder

	PUSH		R1			// push to the stack

	ADD		R6, R5		// increment the digit counter
	DIV		R0, R2		// divide this digit out
	CMP		R0, R4		// last digit?

	BG		displp1:	// if not, divide more


displp2:			// dump all of the digits on the stack

	PULL		R0			// remove the values from the stack
	OUT		R0			// display it
		
	SUB		R6, R5		// dec counter
	CMP		R6, R4		// done

	BG		displp2:	// if not loop again

	ret
// end of procedure


// move to the next line
newline:
	LDI		R0, 0D		// line feed
	OUT		R0
	LDI		R0, 0A		// return
	OUT		R0
	ret
// end of procedure



