MIPS-将输入字符串与存储在内存中的一个字符串进行比较 [英] MIPS - compare input string to one stored in memory
问题描述
我有一个用MIPS编写的有效的字符串比较方法(用户从输入中对两个字符串进行逐位比较),但是我正在尝试对其进行更新,以将第二个输入与我存储在内存中的一个输入进行比较. (如果这两个相等,我想在其他地方使用第一个字符串.)
I have a functioning string compare method written in MIPS (bit by bit comparison of two strings from an input by the user), but I'm trying to update it to compare the second input with one that I have stored in memory. (If those two are equal, I want to use the first string somewhere else).
但是,我遇到了一些问题.这是我的代码:
However, I'm running into some issues. Here's my code:
.data
str1: .space 20
str2: .space 20
msg1:.asciiz "Please enter string (max 20 characters): "
msg2: .asciiz "\n Please enter method (max 20 chars): "
msg3:.asciiz "\nSAME"
msg4:.asciiz "\nNOT SAME"
.text
.globl main
main:
li $v0,4 #loads msg1
la $a0,msg1
syscall
li $v0,8
la $a0,str1
addi $a1,$zero,20
syscall #got string to manipulate
li $v0,4 #loads msg2
la $a0,msg2
syscall
li $v0,8
la $a0,str2
addi $a1,$zero,20
syscall #got string method
la $a0,str1 #pass address of str1
la $a1,str2 #pass address of str2
jal methodComp #call methodComp
beq $v0,$zero,ok #check result
li $v0,4
la $a0,msg4
syscall
j exit
ok:
li $v0,4
la $a0,msg3
syscall
exit:
li $v0,10
syscall
methodComp:
add $t0,$zero,$zero
add $t1,$zero,$a0
add $t2,$zero,$a1
loop:
lb $t3($t1) #load a byte from each string
lb $t4($t2)
beqz $t3,checkt2 #str1 end
beqz $t4,missmatch
slt $t5,$t3,$t4 #compare two bytes
bnez $t5,missmatch
addi $t1,$t1,1 #t1 points to the next byte of str1
addi $t2,$t2,1
j loop
missmatch:
addi $v0,$zero,1
j endfunction
checkt2:
bnez $t4,missmatch
add $v0,$zero,$zero
endfunction:
jr $ra
本质上,我希望做的是在开始时声明
Essentially, what I was hoping to do was declare at the beginnning
strToCompare: .asciiz "foo"
,然后更改我通过的地址str1
的地址
and change where I pass the adress of str1
from
la $a0,str1 #pass address of str1
到
la $a0,strToCompare #pass address of strToCompare
有人知道为什么这行不通吗,或者有更好的方法来实现吗? 非常感谢,如果我的格式设置已关闭,对不起,这是我第一次发布.
Does anyone know why this would not work, or a better way to implement this? Thanks a bunch, sorry if my formatting is off, this is my first time posting.
推荐答案
您缺少输入法(用于从用户输入数据的系统调用)所添加的换行符(\ n).结尾. 您可以预处理输入字符串以删除该换行符,也可以将其添加到存储的字符串中. 后者很容易,只需更改
You are missing the line feed character (\n) that your input method (the system call you are using to input data from the user) is adding at the end. You can either preprocess your input string to remove that line feed or add it to your stored string. The latter is easy, just change
strToCompare: .asciiz "foo"
到
strToCompare: .asciiz "foo\n"
这篇关于MIPS-将输入字符串与存储在内存中的一个字符串进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!