-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment3.asm
115 lines (112 loc) · 3.83 KB
/
assignment3.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
.eqv IN_ADRESS_HEXA_KEYBOARD 0xFFFF0012
.eqv OUT_ADRESS_HEXA_KEYBOARD 0xFFFF0014
.data
Message: .asciiz "Key scan code "
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# MAIN Procedure
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.text
main:
#---------------------------------------------------------
# Enable interrupts you expect
#---------------------------------------------------------
# Enable the interrupt of Keyboard matrix 4x4 of Digital LabSim
li $t1, IN_ADRESS_HEXA_KEYBOARD
li $t3, 0x80 # bit 7 = 1 to enable
sb $t3, 0($t1)
#---------------------------------------------------------
# Loop an print sequence numbers
#---------------------------------------------------------
xor $s0, $s0, $s0 # count = $s0 = 0
Loop: addi $s0, $s0, 1 # count = count + 1
prn_seq:addi $v0,$zero,1
add $a0,$s0,$zero # print an auto sequence number
syscall
prn_eol:addi $v0,$zero,11
li $a0,'\n' # print endofline
syscall
sleep: addi $v0,$zero,32
li $a0,300 # sleep 300 ms
syscall
nop # WARNING: nop is mandatory here.
b Loop # Loop
end_main:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# GENERAL INTERRUPT SERVED ROUTINE for all interrupts
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ktext 0x80000180
#-------------------------------------------------------
# SAVE the current REG FILE to stack
#-------------------------------------------------------
IntSR: addi $sp,$sp,4 # Save $ra because we may change it later
sw $ra,0($sp)
addi $sp,$sp,4 # Save $ra because we may change it later
sw $at,0($sp)
addi $sp,$sp,4 # Save $ra because we may change it later
sw $v0,0($sp)
addi $sp,$sp,4 # Save $a0, because we may change it later
sw $a0,0($sp)
addi $sp,$sp,4 # Save $t1, because we may change it later
sw $t1,0($sp)
addi $sp,$sp,4 # Save $t3, because we may change it later
sw $t3,0($sp)
#--------------------------------------------------------
# Processing
#--------------------------------------------------------
prn_msg:addi $v0, $zero, 4
la $a0, Message
syscall
get_cod: li $t1, IN_ADRESS_HEXA_KEYBOARD
li $t3, 0x81 # check row 1 and re-enable bit 7
sb $t3, 0($t1) # must reassign expected row
li $t1, OUT_ADRESS_HEXA_KEYBOARD
lb $a0, 0($t1)
bne $a0, $zero, prn_cod
nop
li $t1, IN_ADRESS_HEXA_KEYBOARD
li $t3, 0x82 # check row 2 and re-enable bit 7
sb $t3, 0($t1) # must reassign expected row
li $t1, OUT_ADRESS_HEXA_KEYBOARD
lb $a0, 0($t1)
bne $a0, $zero, prn_cod
nop
li $t1, IN_ADRESS_HEXA_KEYBOARD
li $t3, 0x84 # check row 3 and re-enable bit 7
sb $t3, 0($t1) # must reassign expected row
li $t1, OUT_ADRESS_HEXA_KEYBOARD
lb $a0, 0($t1)
bne $a0, $zero, prn_cod
nop
li $t1, IN_ADRESS_HEXA_KEYBOARD
li $t3, 0x88 # check row 4 and re-enable bit 7
sb $t3, 0($t1) # must reassign expected row
li $t1, OUT_ADRESS_HEXA_KEYBOARD
lb $a0, 0($t1)
prn_cod: li $v0,34
syscall
li $v0,11
li $a0,'\n' # print endofline
syscall
#--------------------------------------------------------
# Evaluate the return address of main routine
# epc <= epc + 4
#--------------------------------------------------------
next_pc:mfc0 $at, $14 # $at <= Coproc0.$14 = Coproc0.epc
addi $at, $at, 4 # $at = $at + 4 (next instruction)
mtc0 $at, $14 # Coproc0.$14 = Coproc0.epc <= $at
#--------------------------------------------------------
# RESTORE the REG FILE from STACK
#--------------------------------------------------------
restore: lw $t3, 0($sp) # Restore the registers from stack
addi $sp,$sp,-4
lw $t1, 0($sp) # Restore the registers from stack
addi $sp,$sp,-4
lw $a0, 0($sp) # Restore the registers from stack
addi $sp,$sp,-4
lw $v0, 0($sp) # Restore the registers from stack
addi $sp,$sp,-4
lw $at, 0($sp) # Restore the registers from stack
addi $sp,$sp,-4
lw $ra, 0($sp) # Restore the registers from stack
addi $sp,$sp,-4
return: eret # Return from exception