string MIPS - 检查数组元素是否为空终止字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12379397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:35:25  来源:igfitidea点击:

MIPS - Checking if an element of an array is the null-terminating character

stringmips

提问by r123454321

So I'm trying to write a function that will find the length of a string in MIPS.

所以我正在尝试编写一个函数来在 MIPS 中找到字符串的长度。

I'm walking/traversing along the array, loading each character, and I want to compare each character to the null-terminating character to see if the string has "ended". At each successive iteration, I increment a counter, and then store the counter in $v0 once the string has "ended". However, how do I compare whether the current loaded character is the null terminating character, "\0"? More specifically, how do I represent this null-terminating character? Is it $zero, as I have done below? If so, then what else am I doing wrong? Getting an address error.

我沿着数组走/遍历,加载每个字符,我想将每个字符与空终止字符进行比较,以查看字符串是否已“结束”。在每次连续迭代中,我都会增加一个计数器,然后在字符串“结束”后将计数器存储在 $v0 中。但是,如何比较当前加载的字符是否为空终止字符“\0”?更具体地说,我如何表示这个空终止字符?正如我在下面所做的那样,它是零美元吗?如果是这样,那么我还做错了什么?获取地址错误。

.data
msg1:.asciiz "Please insert text (max 20 characters): "
msg2:.asciiz "\nThe length of the text is: "

newline: .asciiz "\n"

str1: .space 20
.text
.globl main
main:
addi $v0, $v0,4
la $a0,msg1
syscall #print msg1
li $v0,8
la $a0,str1
addi $a1,$zero,20
syscall   #get string 1

la $a0,str1  #pass address of str1
jal len

len: 
addi $t2, $zero, 0 # $t2 is what we want to return in the end -- the count of the length of the character array
addi $s1, $zero, 0 # Index i for array traversing | init. to 0 | i = 0

Loop:

add $s1, $s1, $a0 # Adds the location of the index to the location of the base of the array | $t1 is now the location of: array[index]
lw $t0, 0($s1)

beq $t0, $zero, exit
addi $t2, $t2, 1 # Count = Count + 1
addi $s1, $s1, 1 # i = i + 1
j Loop

exit: 
la $a0,msg2 
li $v0,4
syscall
move $a0,$t0 #output the results 
li $v0,1
syscall

li $v0,10
syscall

回答by markgz

Assuming that you are processing a string of bytes, and that you are looking for the zero byte at the end of the string, you should be using lbu $t0, 0($s1). lbumeans "load byte without sign extension". Then you can compare $t0with the $zeroregister. Your current code uses lw $t0, 0($s1)which loads 4 bytes into $t0.

假设您正在处理一串字节,并且您正在寻找字符串末尾的零字节,那么您应该使用lbu $t0, 0($s1). lbu表示“没有符号扩展的加载字节”。然后你可以$t0$zero寄存器进行比较。您当前的代码使用lw $t0, 0($s1)将 4 个字节加载到$t0.

There are some other bugs in your code, but I'll leave them to you to figure out, since this looks like homework.

您的代码中还有一些其他错误,但我会将它们留给您解决,因为这看起来像是家庭作业。

回答by Berjees

the following program computes the length of the string

以下程序计算字符串的长度

.data   
theStr: .asciiz "berjee"
.text
main:
li $s1,0
la $s0,theStr
loop:   lb $a0,0($s0)
beqz $a0,out
addi $s0,$s0,1
addi $s1,$s1,1
j loop
out:    li $v0,1
add $a0, ##代码##,$s1
syscall
li $v0,10
syscall