'MIPS, Digital Lab Sim, Hexadecimal keyboard handling

I have a problem with handling the keyboard in Digital Lab Sim. I don't know how to check which button is clicked. I tried to do this in loop:

.data
key:        .word 0x11, 0x21, 0x41, 0x81, 0x12, 0x22, 0x42, 0x82, 0x14, 0x24, 0x44, 0x84, 0x18, 0x28, 0x48, 0x88
val:        .word 0, 0, 0


.text

        addi $t0, $zero, 1
        add $t3, $zero, $zero

        la $t4, val
        la $t1, 0xffff0014

loop:   beq $t0, 0, exit

        lb $t2, ($t1)

        beqz $t2, loop



        sw $t2, ($t4)
        addi $t3, $t3, 1
        addi $t4, $t4, 4

        bne $t3, 3, exit

        j loop

exit:

but it does not work.

Thanks in advance.



Solution 1:[1]

In the help part, it said that the mips program have to scan, one by one, each row(send 1,2,4,8, ...) and then observe if a key is pressed. So it means that in order to read value (at 0xffff0014), first you have to write command row value (at 0xffff0012). Here is an example of read number 0 from keyboard:

li $s1, 0xffff0012
li $s2, 0xffff0014
li $t1, 0x01
sb $t1, 0($s1)
lw $t2, 0($s2) # now if you press button 0, $t2 will be 0x11

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Minh Quang Nguyen