[英]Assembly - Simple lock/unlock program bug
我編寫的程序有一個錯誤,我不明白為什么。 應該在用戶按下任意鍵后將狀態設置為鎖定/解鎖,並提供一些反饋。 但是經過兩個周期后,輸出出錯。 例:
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
調用read_str
您需要將a0設置為指向虛擬輸入緩沖區,否則您將覆蓋恰好指向的a0。 您還需要將a1設置為要讀取的字符數。
請注意,僅讀取單個字符的syscall 12可能是更好的選擇。
請參閱syscall列表 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.