@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 
@ This program prints numbers 0 to 15 in hex.
@
@ It defines and uses a function print_digit.
@ 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@
@ Program part 1: definition of main.
@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

.globl _start
_start:
	@initialize sp at start of program
	mov sp, #0x100000		

	@ program main body 
	ldr r4,=0x101f1000	
	@ ASCII codes stored at [r4] get printed		

	mov r5, #0x0
my_loop:
	cmp r5, #0xf
	bgt program_exit

	mov r0, r5
	bl print_digit
	
	add r5, r5, #1
	b my_loop

program_exit:		@ print "END" on a new line
	mov r1, #'\r'
	str r1, [r4]		
	mov r1, #'\n'		
	str r1, [r4]		
	mov r1, #'E'		
	str r1, [r4]		
	mov r1, #'N'		
	str r1, [r4]		
	mov r1, #'D'		
	str r1, [r4]

the_end:			@ do infinite loop at the end
	b the_end	

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@
@ Program part 2: definition of functions.
@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

print_digit:	
	@ print_digit preamble
	sub sp, sp, #16
	str lr, [sp, #0]
	str r0, [sp, #4]
	str r4, [sp, #8]
	str r5, [sp, #12]

	@ print_digit main body
	ldr r4,=0x101f1000	
	@ ASCII codes stored 
	@ at [r4] get printed
	
	cmp r0, #10
	addlt r5, r0, #48
	addge r5, r0, #55
	str r5, [r4]

	@ print_digit wrap-up
	ldr lr, [sp, #0]
	ldr r0, [sp, #4]
	ldr r4, [sp, #8]
	ldr r5, [sp, #12]
	add sp, sp, #16
	bx lr


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


