在MIPS比较两个字符串 [英] Comparing Two Strings in MIPS
问题描述
我是想,因为我的加入整数code,我posted这里之前。我可以把字符串从用户,也将其转换为整数,但是字符串比较过程中我都面临着一个问题(最大尺寸为20,如果字符串小于20,将有空格),从用户的阅读和我的字符* 。我想,如果我可以比较他们,他们是不相等的,我转换成整数,并继续增加,如果他们是平等它将从环退出。
I was trying to compare two strings because of my adding integers code which i posted here before. I could take string from user, also convert that to integer, but i have faced a problem during comparison of string (max size is 20 and if string is less than 20, there will be spaces) read from user and my character " * ". I thought that if i can compare them and they are not equal, i convert to integer and continue to adding, if they are equal it will exit from loop.
我写了一个简单的code比较两个字符串,但是这并没有给结果。这里是我的code;
I wrote a simple code for comparing two strings, however that didn't give the result. Here is my code;
.data
str1: .asciiz "Comp"
str2: .asciiz "Comp"
.text
main:
la $s2, str1
la $s3, str2
move $s6, $s2
move $s7, $s3
li $s1, 5
beq $s6, $s7, exit
move $a0, $s1
li $v0, 1
syscall
exit:
li $v0, 10
syscall
我已经检查QtSpim的寄存器$ S6和S7 $后,我观察到有不同的值。我怎样才能比较两个字符串?谢谢你。
After i have checked QtSpim's registers $s6 and $s7, i observed that there are different values. How can i compare two strings? Thank you.
推荐答案
我已经适应你的程序提示字符串的用户,这样你就可以快速尝试许多值。在 cmploop
是字符串的肉相比较,这样你可以使用,如果你想。
I've adapted your program to prompt the user for strings, so you can try many values quickly. The cmploop
is the "meat" of the string compare, so you can just use that if you wish.
下面是它的[请原谅无偿风格清理]:
Here's it is [please pardon the gratuitous style cleanup]:
.data
prompt: .asciiz "Enter string ('.' to end) > "
dot: .asciiz "."
eqmsg: .asciiz "strings are equal\n"
nemsg: .asciiz "strings are not equal\n"
str1: .space 80
str2: .space 80
.text
.globl main
main:
# get first string
la $s2,str1
move $t2,$s2
jal getstr
# get second string
la $s3,str2
move $t2,$s3
jal getstr
# string compare loop (just like strcmp)
cmploop:
lb $t2,($s2) # get next char from str1
lb $t3,($s3) # get next char from str2
bne $t2,$t3,cmpne # are they different? if yes, fly
beq $t2,$zero,cmpeq # at EOS? yes, fly (strings equal)
addi $s2,$s2,1 # point to next char
addi $s3,$s3,1 # point to next char
j cmploop
# strings are _not_ equal -- send message
cmpne:
la $a0,nemsg
li $v0,4
syscall
j main
# strings _are_ equal -- send message
cmpeq:
la $a0,eqmsg
li $v0,4
syscall
j main
# getstr -- prompt and read string from user
#
# arguments:
# t2 -- address of string buffer
getstr:
# prompt the user
la $a0,prompt
li $v0,4
syscall
# read in the string
move $a0,$t2
li $a1,79
li $v0,8
syscall
# should we stop?
la $a0,dot # get address of dot string
lb $a0,($a0) # get the dot value
lb $t2,($t2) # get first char of user string
beq $t2,$a0,exit # equal? yes, exit program
jr $ra # return
# exit program
exit:
li $v0,10
syscall
这篇关于在MIPS比较两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!