我需要帮助创建两个函数。第一个函数需要找到单词的长度。对于输入,该函数获取单词第一个字符的地址并返回单词的长度(单词位于数组中)。
第二个函数需要删除数组中字符数多于平均值的单词(从数组中的所有字符计算),因此假设单词的平均值为 4 个字符,则所有超过 4 个字符的单词都需要删除待删除。
更新:prntscr.com/ezjylq
我给你两个示例代码希望对你有所帮助。
此代码查找任意 ASCII 字符串的长度
给定字符串:
"Hello\n"
输出:
6
代码:
.data
message: .asciiz "Hello\n"
.text
main:
li $t1,0
la $t0,message #load message to t0
loop:
lb $a0,0($t0) #load one byte of t0 to a0
beqz $a0,done #branch if a0 = 0
addi $t0,$t0,1 #increment t0
addi $t1,$t1,1 #increment the counter t1
j loop
done:
li $v0,1 #print an integer
add $a0, $0,$t1 #add the counter to a0
syscall
li $v0,10 #exit program
syscall
这是一个打印“Hello World”的函数。
.data
message: .asciiz "Hello World.\n"
.text
main:
jal displayMessage
li $v0,10 #exit function
syscall
displayMessage:
li $v0,4 #printing string
la $a0,message #save the message to argument $a0
syscall
jr $ra