;ex5b_2400
*********************** version 1.6***********************
;remember to set /flash/linker/use memory layout from target dialog, otherwise it will not run
;and stack_pointer SP (r13) will not be set correctly.
;*******************************************************************
;test5b.s
;This programs shows how stack can be used to save register 
;values during nested subroutine calls
;each subroutine-call lowers the  stack pointer one level (16 bytes in this is example)
;Exercise: Run the program using single step, observe the changes of the stack pointer (r13)
; and contents of the stack (RAM memory locations from address 0x0000 0408)

; declare variables	  New test12D
	AREA	|.data|, DATA, READWRITE
Data1p 	DCD 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
		align
;-------------------------------------------------------------

; User Initial Stack & Heap
        AREA    |.text|, CODE, READONLY

        EXPORT  __main
__main	LDR R0, =Data1p 
		mov r3,#0
		mov r4,#0
		mov r5,#0

loop	MOV r0, #0xa1
		MOV r1, #0xa2
		MOV r2, #0xa1
		BL 	subroutine1
		B loop

		

subroutine1    STMED r13!, {r0-r2,r14}
			  add r3,#1
			  add r4,#2
			  add r5,#3
			  MOV r0,r3
			  MOV r1,r4
			  MOV r2,r5
			  BL 	subroutine2
			  LDMED r13!,{r0-r2,r14}

			  BX LR

subroutine2    STMED r13!, {r0-r2,r14}
			  add r3,#1
			  add r4,#2
			  add r5,#3
			  MOV r0,r3
			  MOV r1,r4
			  MOV r2,r5
			  LDMED r13!,{r0-r2,r14}
			  BX LR

		END