        ; Laboratory Exercise 2, Home Assignment 4

SWITCHES 	EQU	0x70001000
LEDS     	EQU	0x70001004
BUTTONS  	EQU	0x70001008

IRQ		EQU	0x80

		AREA my_code, CODE

Introutine
		; Add your code here!

		ENTRY

Start		LDR	r0,=BUTTONS
		MOV	r1,#0x7f
		STR	r1,[r0]		; Reset interrupt port

		ADR 	r1,Introutine	; Get Introutine address
		SUB	r1,r1,#0x8	; Substract by 8 because of pipeline
		SUB	r1, r1, #0x18	; Substract by 0x18 is the address of PC 
					; at the time IRQ start execute
		MOV	r1, r1, LSR #2	; Shift right 2 bit to get offset by word 
					; instead of offset by byte
		ADD	r1, r1, #0xea000000 ; Add Branch instruction 
		MOV	r0,#0x18		; 
		STR	r1,[r0]		; Copy to IRQ vector

		BL	enable_irq	; Enable interrupts

Loop		BL	Comp            ; Perform heavy computations
		B	Loop		; Repeat loop

; This function enables the IRQ interrupt
enable_irq
		MRS r0, CPSR		; Read status word
		BIC r0,r0, #IRQ		; Clear the IRQ enable bit
		MSR CPSR_c, r0		; Write back control part of CPSR
		MOV r15,r14		; Return	

Comp		LDR	r0,=0xffffff	; Initialize counter value
Delay		SUBS	r0, r0, #1    	; Decrease counter by 1
		BNE	Delay		; Test if ready
		MOV	r15,r14		; Return loop
	
		END

