	.macro ShowStack
		mov sp,@#SP2
		call MonM0
	.endm
	
	.LIST
	.ASECT
	.=001000


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
		
START:
	;jmp TstSt1	;Simple Stack test
;	jmp TstCll	;Call Stack test
	;jmp AddSub32 ;ADC/SBC Test
	jmp Multiply ;Multiply/Divide Test

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
		
		
TstSt1:			;Simple Stack test
	mov sp,r4
	sub #16.,r4
	mov r4,@#StackPointer
	mov #17777,r5
	ShowStack
	mov r5,-(sp)			;Push 17777 onto the stack
		mov #16666,r5
		ShowStack
		mov r5,-(sp)		;Push 16666 onto the stack
			mov #15555,r5
			call NewLine
			ShowStack	
			call NewLine
		mov (sp)+,r5		;Pop 16666 off the stack
		ShowStack
	mov (sp)+,r5			;Pop 17777 the stack
	ShowStack
	jmp InfLoop

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
		
	
TstCll:			;Call Stack test
	mov sp,r4
	sub #16.,r4
	mov r4,@#StackPointer
	mov #17777,r5
	ShowStack
	mov r5,-(sp)			;Push 17777 onto the stack
		Call Nest1
	mov (sp)+,r5			;Pop 17777 off the stack
	ShowStack
InfLoop:
	jmp InfLoop

Nest1:
	mov #16666,r5
	ShowStack
	mov r5,-(sp)			;Push 16666 onto the stack
		Call Nest2
	mov (sp)+,r5			;Pop 16666 off the stack
	ShowStack
	return

Nest2:
	mov #15555,r5
	ShowStack
	mov r5,-(sp)			;Push 15555 onto the stack
		mov #14444,r5
		call NewLine
		ShowStack
		call NewLine
	mov (sp)+,r5			;Pop 15555 off the stack
	ShowStack
	return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
		

Multiply:
	call ClearRegs
;Using Even Registers these commands work at 32 bit
	mov #2,r0
	mov #3,r2
	call Monitor
	mul r0,r2		;R2.R3=R0*R2
	call Monitor
	inc r3
	call Monitor
	
	div r0,r2		;R2=R2.R3/R0 Remainder in R3
	call Monitor
	
	call NewLine
	call ClearRegs

;Using Odd Registers these commands work at 16 bit
	mov #2,r1
	mov #3,r3
	call Monitor
	mul r1,r3		;R1=R0*R2
	call Monitor
	inc r3
	call Monitor
	div r1,r3		;Doesn't work! Must be even register.
	call Monitor		;R3=No good!
	
	jmp InfLoop

ClearRegs:
	mov #177777,r0	
	mov #177777,r1
	mov #177777,r2
	mov #177777,r3
	mov #177777,r4
	mov #177777,r5
	return




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
		
	
AddSub32:
	mov #0,r0
	mov #177775,r1
	
	mov #10,r2
	call MonR01
Loop:	
	add #1,r1		;Add 1 to R1
	adc r0			;add carry to R0
	call MonR01
	sob r2,Loop		;Repeat until R2=0
	
	call NewLine
	
	mov #10,r2
Loop2:	
	sub #1,r1		;Subtract 1 to R1
	sbc r0			;Subtract carry to R0 (borrow)
	call MonR01
	sob r2,Loop2	;Repeat until R2=0
	
	jmp InfLoop	
		

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
		
	
StackPointer: .word 0	
SP2: .word 0	
R4Bak: .word 0	
MonM0:
	mov (sp)+,r4
	mov r4,-(sp)
	
	mov r2,-(sp)
	mov r1,-(sp)	
	mov r0,-(sp)	
		mov #'5,r1
		mov r5,r2
		jsr PC,MONREG		
		
		
		mov #'P,r0
		jsr PC,PrintChar
		mov #'C,r0
		jsr PC,PrintChar
		mov #':,r0
		jsr PC,PrintChar
		mov r4,r2	;Source address to show
		jsr pc,SHOCT16
		
		mov #' ,r0
		jsr PC,PrintChar
		
		mov #'S,r0
		jsr PC,PrintChar
		mov #'P,r0
		jsr PC,PrintChar
		mov #':,r0
		jsr PC,PrintChar
		mov @#SP2,r2	;Source address to show
		jsr pc,SHOCT16
		;mov #' ,r0
		;jsr pc,PrintChar
		call NewLine
		;mov r4,@#R4Bak
			mov @#StackPointer,r4	;Source address to show
			mov #8.,r3			;Number of words 
			jsr pc,MemDump		;Show memory onscreen
		;mov @#R4Bak,r4
		
	mov (sp)+,r0
	mov (sp)+,r1
	mov (sp)+,r2
	RTS PC
	
MonR01:
	mov r2,-(sp)
	mov r1,-(sp)	
	mov r0,-(sp)
		mov r1,-(sp)
			mov #'0,r1
			mov r0,r2
			jsr PC,MONREG		
		mov (sp)+,r1
		mov r1,r2
		mov #'1,r1
		jsr PC,MONREG
		call NewLine
	mov (sp)+,r0
	mov (sp)+,r1
	mov (sp)+,r2
	RTS PC

MonR0:
	mov r2,-(sp)
	mov r1,-(sp)	
		mov #'0,r1
		mov r0,r2
		jsr PC,MONREG		
	mov (sp)+,r1
	mov (sp)+,r2
	RTS PC


	.INCLUDE /Funct.mac/
	.INCLUDE /Monito.mac/
	

	

	.EVEN			
;Space for Stack - Stack starts at end of program
	.BLKB 128.	

	.END    START
