程序集 - 简单 lock/unlock 程序错误
Assembly - Simple lock/unlock program bug
我写的程序有一个错误,我不明白为什么。它应该在用户按下任意键后将状态设置为 lock/unlock,并提供一些反馈。但是在两个循环之后输出出错了。示例:
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, [=11=], 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, [=11=], 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, [=11=], 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, [=11=], 1
# output new status
li $v0, 4 # system call code for print_str
la $a0, locked # print "The door is locked"
syscall
j loop
当您调用 read_str
时,您需要将 a0 设置为指向虚拟输入缓冲区,否则您将覆盖 a0 恰好指向的任何内容。您还需要将 a1 设置为要读取的字符数。
请注意,仅读取单个字符的系统调用 12 可能是更好的选择。
我写的程序有一个错误,我不明白为什么。它应该在用户按下任意键后将状态设置为 lock/unlock,并提供一些反馈。但是在两个循环之后输出出错了。示例:
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, [=11=], 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, [=11=], 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, [=11=], 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, [=11=], 1
# output new status
li $v0, 4 # system call code for print_str
la $a0, locked # print "The door is locked"
syscall
j loop
当您调用 read_str
时,您需要将 a0 设置为指向虚拟输入缓冲区,否则您将覆盖 a0 恰好指向的任何内容。您还需要将 a1 设置为要读取的字符数。
请注意,仅读取单个字符的系统调用 12 可能是更好的选择。