汇编 - 简单的锁定/解锁程序错误

时间:2016-07-09 11:32:26

标签: assembly mips

我写的程序有一个错误,我不明白为什么。它应该在用户按任意键后设置状态以锁定/解锁,并提供一些反馈。但经过两个周期后输出出错了。例如:

The door is locked Press any key to unlock The door is unlocked Press any key to lock The door is locked q The door is unlocked q The door is locked q The door is unlocked q

q是我按下的按钮。

这是代码。我将不胜感激任何帮助。

## program for a simple door locking device
## register use:    $v0: stores syscall code
##                  $a0: stores entered number
##                  $s0: stores status number

.data
    locked:     .asciiz "The door is locked\n"
    unlocked:   .asciiz "The door is unlocked\n"
    unlock:     .asciiz "Press any key to unlock\n"
    lock:       .asciiz "Press any key to lock\n"

.text
.globl main

main:

# store default status value
ori $s0, $0, 1      # store the default 1

# output the status
li $v0, 4           # system call code for print_str
la $a0, locked      # print "The door is locked"
syscall

loop: 

bgtz $s0, unlocking     # if $s0 > 0, start unlocking
beq $s0, $0, locking    # if $s0 = 0, start locking

unlocking:  

# output the user instruction
li $v0, 4               # system call code for print_str
la $a0, unlock          # print "Press any key to unlock"
syscall

# ask for input
li $v0, 8               # system call code for read_str
syscall

# set status to unlocked (0)
ori $s0, $0, 0      

# output new status
li $v0, 4               # system call code for print_str
la $a0, unlocked        # print "The door is unlocked"
syscall

j loop  

locking:

# output the user instruction
li $v0, 4               # system call code for print_str
la $a0, lock            # print "Press any key to lock"
syscall

# ask for input
li $v0, 8 # system call code for read_str
syscall

# set status to locked (1)
ori $s0, $0, 1  

# output new status
li $v0, 4           # system call code for print_str
la $a0, locked      # print "The door is locked"
syscall     

j loop  

1 个答案:

答案 0 :(得分:4)

当你调用read_str时,你需要设置a0指向虚拟输入缓冲区,否则你会覆盖a0恰好指向的任何东西。您还需要将a1设置为要读取的字符数。

请注意,只读取单个字符的系统调用12可能是更好的选择。

请参阅list of syscalls

相关问题