	include "\SrcAll_SH2\BasicMacros.asm"
	include "\SrcAll_SH2\BasicHeader.asm"
		
	;bra ExampleMoves
	;nop
	
	;bra ExampleAddsSubs
	;nop
	
	bra ExampleCallsAndJumps
	nop
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
		
;# Marks immediate

;Source is on LEFT of comma
;Destination is on the RIGHT of comma

;-128 to +127 can be stored in the assembled command word

ExampleMoves:

	mov #127,r0		;Positive number
	
	mov #-1,r1		;Negative numbers are sign extended
	
	mov #-128,r2	;Negative numbers are sign extended
	
	bsr Monitor3	;Show R0,R1,R2
	nop				;Must Put a NOP after BSR (Delay slot)
	
;NOP - NO OPERATION - Does nothing - this is just a 'spacer' after a command
;with a delay slot	
	
;This will be moved into literal table	
;It must be neerby! it's defined by a LTORG command

	mov #12345678,r0			;Decimal 
	
	mov #%1010101010101010,r1	;Binary  %
	
	mov #$66606660,r2			;Hexadecimal $
	
	mov #'ABCD',r3				;Ascii '
		
	bsr Monitor6			;Show R0,R1,R2,R3,R4,R5
	nop						;Must Put a NOP after BSR (Delay slot)
		
;We can transfer values between registers
	
	mov r0,r1				;Copy R0 -> R1
	mov r0,r2				;Copy R0 -> R2
	mov r1,r14				;Copy R1 -> R14
	
	bsr Monitor3			;Show R0,R1,R2
	nop
		
;R15 has a special purpose, the Stack pointer!
;R15 / SP have the same meaning - Register 15
;Don't use it for now!

	mov r15,r0				;Copy R15 -> R0
	mov SP,r1				;Copy R15 -> R1
	
	bsr Monitor3			;Show R0,R1,R2
	nop						;Must Put a NOP after BSR (Delay slot)
	
;We can load an address
	mov.l #TestValue,r5		;Load address of TestValue into R5
	mov.l #TestValue2,r4	;Load address of TestValue2 into R4
	
;... Then use that address as a source to read data 

	mov.l @r5,r0	;Move 32 bit value from address in R5 into R0
	
	mov.w @r5,r1	;Move 16 bit value from address in R5 into R1
	
	mov.b @r4,r2	;Move 8 bit value from address in R4 into R2
	
	mov @r5,r3				;If we don't specify a .B .W .L 
								;MOV will default to L
								
;Most commands default to .L - but a few commands are 16 bit only and default to .W
							
	bsr Monitor6			;Show R0,R1,R2,R3,R4,R5
	nop
	
	
	mov.b @r4+,r0	;Move 8 bit value from R4 into R0, and inc R4
	mov.b @r4+,r1	;Move 8 bit value from R4 into R1, and inc R4
	mov.b @r4+,r2	;Move 8 bit value from R4 into R2, and inc R4
	mov.b @r4+,r3	;Move 8 bit value from R4 into R3, and inc R4
	
	bsr Monitor6			;Show R0,R1,R2,R3,R4,R5
	nop
	
	bra ExampleDone
	nop 
	
	
TestValue:	dc.l $FEDCBA98	;Define a long (32 bit)
TestValue2:	dc.l $12345678	;Define a long (32 bit)
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
ExampleAddsSubs:
	mov #2,r0
	mov #2,r1
	mov #1,r2
		
	bsr Monitor3	;Show R0,R1,R2
	nop				;Must Put a NOP after BSR (Delay slot)
		
	add r2,r0		;r0=r0+r2
	
	sub r2,r1		;r1=r1+r2
	
	bsr Monitor3	;Show R0,R1,R2
	nop				;Must Put a NOP after BSR (Delay slot)
	
	add r2,r0		;r0=r0+r2
	
	sub r2,r1		;r1=r1+r2
	
	bsr Monitor3	;Show R0,R1,R2
	nop				;Must Put a NOP after BSR (Delay slot)
	
	add #1,r0		;r0=r0+1
	
	add #-1,r1		;r0=r0-1 ... There is no SUB #1
	
	;sub #1,r1		;No such command!
	
	bsr Monitor3	;Show R0,R1,R2
	nop				;Must Put a NOP after BSR (Delay slot)
	
	bra ExampleDone
	nop 
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
ExampleCallsAndJumps:	
	
	bsr ShowHello		;Call the sub ShowHello 
	nop						;(Return address into PR)
	
	bsr NewLine
	nop					;BSR & JSR have a delay slot 

;Branch can be used when destination is neerby

	mov #'A',r0			;Load R0 with 'A'
	bsr PrintChar		;Branch to subroutine Printchar
	
	;mov #'?',r0		;Delay slot!?
	nop
	
;Jump only works with a register containing address, 
; but destination can be anywhere
	
	mov #'B',r0
	mov #PrintChar,r13
	jsr @r13			;Jump to address in r13
	nop
	
;Branch has no return part, it's like a basic Goto
	bra SkipC			;Branch (no return)
	nop
	
	mov #'C',r0
	mov #PrintChar,r13
	jsr @r13			;Jump to address in r13
	nop
	
SkipC:

	mov #'D',r0
	mov #PrintChar,r13
	jsr @r13			;Jump to address in r13
	nop

;Jump is like Branch, but has no range limit 
	;and only works with a register
	
	mov #SkipE,r13
	jmp @r13			;Jump can only go to a register
	nop
	
	mov #'E',r0
	mov #PrintChar,r13
	jsr @r13			;Jump to address in r13
	nop
	
SkipE:
	
	mov #'F',r0
	mov #PrintChar,r13
	jsr @r13			;Jump to address in r13
	nop
		
	bsr NewLine
	nop
	
;Far Branches/Jumps	
	
	mov #ShowHello-(*+6),r13		;Calculate offset to ExampleDone
	bsrf r13
	nop
	
	mov #ExampleDone-(*+6),r13		;Calculate offset to ExampleDone
	braf r13
	nop

	mov #Monitor3,r13	
	jsr @r13			;Show R0,R1,R2
	nop						;Must Put a NOP after BSR (Delay slot)
	
	bra ExampleDone
	nop 
	
	
	
	
ShowHello:

	sts.l pr,@-sp		;Back up PR (used by BSR/RTS)

		mov #'H',r0			;Load R0 with 'H'
		bsr PrintChar		;Branch to subroutine Printchar
		nop					;Delay slot
		
		mov #'E',r0			;Load R0 with 'E'
		bsr PrintChar		;Branch to subroutine Printchar
		nop					;Delay slot
		
		mov #'L',r0			;Load R0 with 'L'
		bsr PrintChar		;Branch to subroutine Printchar
		nop					;Delay slot
		
		mov #'L',r0			;Load R0 with 'L'
		bsr PrintChar		;Branch to subroutine Printchar
		nop					;Delay slot
				
		bsr PrintChar		;Branch to subroutine Printchar
		mov #'O',r0			;Load R0 with 'O' - in the delay slot!
		
	lds.l @sp+,pr		;Restore PR (used by BSR/RTS)	
	
	rts 				;Return to the caller
	nop
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
	
ExampleDone:	
	bsr PageFlip			;Flip Buffer (for 32x)
	nop
	
InfLoop:	
	bra	InfLoop
	nop

	
	
;Anything too big for a 16 bit command (EG #$12345678) will be put
; at LTORG automatically by our assembler!
	
	ltorg		;Literal Org, Values too large 
					;will be stored here by the assembler 
		
	align 4		;Ensure we're still 32 bit aligned
					;(4 bytes = 32 bit)
	

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


Monitor3:
	pushall
		mov sp,r3
		add #60,r3	;Point to first pushed reg
		mov #0,r1 	;Reg label
		
		
		mov #3,r5	;Reg per line
		
		mov #3,r4	;Reg count
MonitorAgain3:
		mov.l @r3,r2	;Get reg
		add #-4,r3		;Move to next reg
		
		bsr ShowReg
		nop
				
		add #1,r1		;Reg label
		
		dt r5
		bf MonitorNoNL3
		
		mov #3,r5	;Reg per line
		bsr NewLine
		nop
MonitorNoNL3:
		dt r4
		bf MonitorAgain3
		
		bsr NewLine
		nop
	popall
	rts
	nop
	
Monitor6:
	pushall
		mov sp,r3
		add #60,r3	;Point to first pushed reg
		mov #0,r1 	;Reg label
		
		mov #3,r5	;Reg per line
		
		mov #6,r4	;Reg count
MonitorAgain6:
		mov.l @r3,r2	;Get reg
		add #-4,r3		;Move to next reg
		
		bsr ShowReg
		nop
				
		add #1,r1		;Reg label
		
		dt r5
		bf MonitorNoNL6
		
		mov #3,r5	;Reg per line
		bsr NewLine
		nop
MonitorNoNL6:
		dt r4
		bf MonitorAgain6
		
		bsr NewLine
		nop
	popall
	rts
	nop
	
	include "\SrcAll_SH2\BasicFooter.asm"
	