//Assign4.s
// Look-up table approach
// MUL approach for the next location
.equ memAddr, 0x00000200
.equ LED, 0xFF200000
.equ myID,0x02928272
.global _start
_start:
	LDR r2, =myID
	LDR r3, =memAddr
	BL split8
	LDR r3, =memAddr
	MOV r6,#8
redo:
	LDR r5,=LED
	BL barChart

	STR r0, [r5]
	BL delay1s
	ADD r3, r3, #4
	SUBS r6, r6, #1
	BNE redo
	
	//turn off at the end
	MOV r0, #0
	STR r0,[r5]
stop:
	B stop

//split8
//split the 8-digit number into 8 separate digits
//and store them to memory location
//both given by the caller
//input r2 = 8-digit ID
//input r3 = base memory address where the separate digits are stored
//output  - store separate digits to the memory location
//          highest digit to lowest address
split8:
	MOV r0, #8
	ADD r3, r3, #28
	PUSH {r4} //which is used in the function
continue:
	AND r4,r2,#0x0F
	STR r4,[r3],#-4 
	LSR r2,r2,#4
	SUBS r0,r0,#1
	BNE continue
	POP {r4}  // now r4 is intact
	BX lr

// bar chart function
// input  r3 = memor location where a number is located
// output r0 = Word pattern for the number for barchart
barChart:
	MOV r0,#0
	PUSH {r1,r8,r10} //keep these intact
	MOV r10, #4
	LDR r8, [r3]
	MUL r8,r8,r10
	LDR r1, =lookup
	ADD r1, r1, r8
	LDR r0, [r1]
	POP {r1,r8,r10}
	BX lr

delay1s:
//manual delay without
//using time delay
	PUSH {r8}
	LDR r8, =0x200000 //2 seconds
keep:
	SUBS r8, r8, #1
	BNE keep
	POP {r8}
	BX lr


.data
.org 0x0100
lookup:
	.word  0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF 

.end