.equ SW, 0x10000040						/* Memory address for switches */
.equ LEDR, 0x10000000					/* Memory address for red LED lights */

.global _start
_start:	
	movia	r3, DELAY					/* Get Location of the DELAY value */
	ldw		r4, 0(r3)					/* Set maximum delay */
	movia	r5, 1						/* Set r3 to 1. */		
	movia	r6, SW						/* location in memory to read switches from */
	movia	r7, LEDR					/* location in memory to output to LEDR */
	movia	r8, 0						/* Counter to display on LEDR. */

WAIT_SWITCH:
	ldwio	r9, 0(r6)					/* Read state of switches. */
	and		r9, r9, r5					/* Check state of SW[0] only - since r3 is one, we mask out all bits but the LSB. */
	beq		r9, zero, WAIT_SWITCH		/* which corrsponds to SW[0] */
	stwio	r8, 0(r7)					/* Show counter value on LEDRs */
	addi	r8, r8, 1					/* Increment display counter */
	
WAIT_DELAY:
	addi	r4, r4, -1					/* Increment delay counter. */
	bne		r4, zero, WAIT_DELAY		/* Wait for 100000 instructions before proceeding further */
	ldw		r4, 0(r3)					/* Reset delay counter */
	br		WAIT_SWITCH					/* Loop back to display counter value. */

DELAY:
.word 100000							/* Set delay in terms of instructions executed. */	

.end

