在下面的 mips 代码中,我如何移动数组中的字符 P,以及如何结束显示网格的循环

问题描述 投票:0回答:0

下图是角色P所在的格子the image of the grid。 我需要通过键盘轮询使用 wasd 移动字符 P。其中 w = up , a= down , s=left 和 d= right。以下是我使用的源码

.data
    array: .byte 
             0,83,67,79,82,69,58,10
             35,35,35,35,35,35,35,35,35,35,35,10
             35,32,32,32,32,32,32,32,32,32,35,10
             35,32,32,32,32,32,32,32,32,32,35,10
             35,32,32,32,112,32,32,32,32,32,35,10
             35,32,32,32,32,32,32,32,32,32,35,10
             35,32,32,82,32,32,32,32,32,32,35,10
             35,35,35,35,35,35,35,35,35,35,35,10
        pos: .word 44
     
.globl main
.text

main: 
    li $t0,0xffff000c
    la $t1,array
    lb $t2,($t1)
    sb $t2,($t0)
    li $t3,0 #loop counter
    li $t4,0 #row index
    la $a0,10

row:
    addi $t4,$t4,1   # next row index
    addi $t5,$t4,12 # byte offset
    addi $t6,$t4,12 # next byte offset

loop:
    lb $t2, ($t1)      # load current element into $t2
   
    sb $t2, ($t0)      # write modified element to MMIO display buffer
    addi $t1, $t1, 1   # increment array pointer to point to next element
    addi $t3, $t3, 1 #increase loop counter
    beq $t1, $t6, loop  
    beq $t4, $a0, poll
    addi $t1,$t1,12  
    j row

poll:
    
     li $t0, 0xffff0004   # Address to read keyboard input
     li $t1, 0           # Initialize $t1 to 0
     lbu $t1, ($t0)  # Read keyboard input
     beq $t1, $zero, poll  # Loop until input is detected
        # The value of $t1 now holds the ASCII code for the input character

        # Display the letter "w" at memory location 0xffff0004
     li $t2, 0xffff0004   # Address to display output
     sb $t1, ($t2)        # Store the input character at memory location 0xffff0004
     li $v0,12
     syscall
     beq $v0,100,Right
     j poll
     
Right:
    lw $s0,pos
    addiu $s0,$s0,1
    sw $s0,pos
    
    #update array
    la $t1,array
    lw $t2,pos
    addi $t1,$t1,1   
    sb $t2,($t0)
    j main 

我必须解决这个问题的想法是,当按下 d 时,字符 P 替换右侧的数组,网格再次更新,这对于 w、a 和 s 都是一样的。但我面临的问题是键盘和显示 mmio 模拟器中的光标继续运行,我无法使用键盘输入用 P 替换数组元素。

mips
© www.soinside.com 2019 - 2024. All rights reserved.