//Assign5.s
// 3 letters + 3 digits (000 - 999) on 7 segs
// ab c526  for example
.equ seven1, 0xFF200020
.equ seven2, 0xFF200030
.equ ab, 0x00005F7C  //__ab for seven2
.equ c, 0x58000000  //c___ for seven1
.global _start
_start:
	LDR r10, =segPattern
	LDR r11, =seven1
	LDR r12, =seven2
	LDR r8, =ab
	LDR r9, =c
//Clear 7-seg
	MOV r1, #0
	STR r1, [r11]
	STR r1, [r12]
	BL delay
//dispaly __ab
	STR r8,[r12] //dispHex2
//	LDR r4, =#0x1000 //counter
redo:
	MOV r2, r1
	BL h2d		//read from r2 --return to r0
	MOV r2, r0
	BL dispDec3 //read from r2 -- no return, just disp on Hex1
	BL delay
	ADDS r1, r1,#1
	CMP r1, #1000
	BEQ stop
	B redo
	
stop:
	B stop

/* 3 hexa digit to 3-digit decimal converion */
// input1: r2 - hexa number of 3 digits
// input2: r0 - 3 digit decimal, each occupying a byte

h2d:
	PUSH {r7, r8}
	MOV r0, #0
	MOV r7,#0  //3rd dec
	MOV r8, #0  //2nd dec
again:
	CMP r2,#100
	BLT less
	SUBS r2,r2, #100
	ADD r7, r7, #1
	B again	
less:
	CMP r2, #10
	BLT nomore
	SUBS r2, r2, #10
	ADD r8, r8, #1
	B less
nomore:			//collect r7, r8, and r2
	LSL r7, r7, #16
	LSL r8, r8, #8
	ADD r0, r7,r8
	ADD r0, r0, r2
	POP {r7,r8}
	BX lr

//r2 holds the 2 digit HEX such as 0xFF
//dispHex2:
//	PUSH	{r4}
//	STR r8, [r2] //write to seven2 seg
//	POP {r4}
//	BX lr
	
//r2 holds the 3 digit DEC such as 0x00 01 00 05  for 0105
dispDec3:
	PUSH	{r4,r5,r6,r8,r9}
	MOV r4, r2
	MOV r5, #0
	AND r5,r4,#0x0F
	LDR r6, [r10, r5, LSL #2]
	
	LSR r4, r4, #8
	AND r5, r4, #0xF
	LDR r8, [r10, r5, LSL #2]
	LSL r8, r8, #8
	
	LSR r4, r4, #8
	AND r5, r4, #0xF
	LDR r0, [r10, r5, LSL #2]
	LSL r0, r0, #16	
	
	ADD r0, r0, r8
	ADD r0, r0, r6
	ADD r0, r9   //c___ added
	STR r0, [r11]	
	POP {r4,r5, r6,r8,r9}
	BX lr
	
delay:
//manual delay without
//using time delay
	PUSH {r5}
	LDR r5, =0x20000 //2 seconds
keepD:
	SUBS r5, r5, #1
	BNE keepD
	POP {r5}
	BX lr

.data
segPattern:
	.word 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D,0x07,0x7F,0xEF
//for a, b, c, d, e, and f
	.word 0x5F, 0x7C, 0x58, 0x5E,0x7B, 0x71 



.end
	