比较 MIPS 中的两个字节 [英] Comparing two bytes in MIPS
问题描述
我正在尝试制作一个简单的 MIPS 程序.我想检查用户输入的字符是否为空格.我在 .data
部分定义了一个变量 space
.然后我接受用户输入,并使用 jal
调用我的函数来检查这个.我将字节 space
加载到 $a0
中,然后想检查 $t0
和 $a0
是否相同.如果它们不是 $t1
应该设置为 1
然后我使用 branch not equal to branch 到我的其他函数,该函数将响应打印给用户.但是,无论我输入什么字符,它总是分支.我不确定我做错了什么.
I'm trying to make a simple MIPS program work. I want to check if the character inputted by the user is a space. I have defined a variable space
in my .data
section. I then take the user input, and use jal
to call my function which should check this. I load the byte space
into $a0
and then want to check whether $t0
and $a0
are the same. If they aren't $t1
should be set to 1
and then I used branch not equal to branch to my other function which prints out the response to the user. However, no matter what character I enter it always branches. I'm not sure what I'm doing wrong.
space: .byte ' '
main:
#Getting user input
li $v0,8
la $a0,str1
li $a1, 20
syscall
jal is_space
#Indicate the end of main function
li $v0,10
syscall
is_space:
add $t0, $a0, $zero
lb $a0, space
sltu $t1, $a0, $t0
bne $t1, $zero, spaceinput
推荐答案
您甚至忘记访问已读取的字符.它位于 str1
的缓冲区中.另外,如果您想检查相等性,为什么要使用 sltu
?将空间存储在内存中也是一种浪费.
You forgot to even access the character that was read. It's placed in a buffer at str1
. Also, if you want to check for equality, why are you using sltu
? Storing the space in memory is a waste, too.
main:
#Getting user input
li $v0,8
la $a0,str1
li $a1, 20
syscall
lb $a0, ($a0) # fetch first character entered
jal is_space
#Indicate the end of main function
li $v0,10
syscall
is_space:
li $t0, ' '
beq $a0, $t0, spaceinput
这篇关于比较 MIPS 中的两个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!