微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有一种方法可以将一系列字符串存储到在Mips中具有特定偏移量的数组中?

如何解决有没有一种方法可以将一系列字符串存储到在Mips中具有特定偏移量的数组中?

(我是Mips的新手,我知道我的某些语法和/或逻辑可能看起来很奇怪或不正确。谢谢您的理解。)

已要求我将用户的一系列字符串读入通过在程序内分配空间而创建的数组。在我的get_input函数内部,传递使用syscall 8读取字符串时的数组地址和字符缓冲区大小。

get_input函数存储输入的字符串(最多32个字符),然后将其传递给strtrunc函数,以在末尾截断带有零值的字符串。 strtrunc函数还计算单词的大小并将其值保存到$ v0。

这时,程序返回到get_input函数,并将$ v0中的值保存到lengths数组中,然后增加所用数组的偏移量。

在存储输入的单词并随后访问它时,我遇到了一系列问题—最初,我为单词数组使用了四个偏移量来存储单词,但是输入的单词长度超过四个字符会将剩余字符推到下一个偏移量。

示例(输入感谢):[nah T] [\ 0 \ 0 sk],然后在输入“再次”时循环与先前字符重叠:示例(再次输入后):[nah T] [iag A] [\ 0 \ 0 \ 0 n]这就是为什么我为strtrunc函数添加的$ zero添加了前一个单词的大小+ 1的偏移量。

我想这个问题更适合我如何选择将用户输入的字符串保存到数组中。我做错了吗?稍后我应该向后读取输入的字符串,但是,我只能正确读取输入到程序中的第一个字符串。 (我不包括功能,因为可以很容易地解决该问题)。最后,我可以正确访问lengths数组和以四个偏移量存储的值。只是我无法正确访问的字符/字符串数组。我希望字符串的地址具有四个相同的偏移量,同时仍存储整个字符串而不会被后面的单词覆盖。

[s k n a h T] [n i a g A]

0x0000 0x0004


.data
prompt: .asciiz "Please enter up to 16 strings (max 31 characters) 1 per line\nEnter a blank line to finish inputing the strings.\n"
prompt1: .asciiz "Next string: "

.align 2
strings: .space 512  #16 strings of up to 32 characters each including the null character

.align 2
lengths: .space 64 #Space for 16 values for the length

.text
main:
    # Step 1: Read user-inputted strings
    la $a0,prompt
    li $v0,4
    syscall
    #Call get_input
    la $a0,prompt1
    la $a1,strings
    li $a2,32
    jal get_input
    
    # Step 2: Calculate string lengths
    #Already done from get_input and stored into the Lengths Array
    
exit_main:
    li   $v0,10            # 10 is the exit program syscall
    syscall             # execute call


###############################################################
# Grabs the inputted strings and saves to strings array.
#$a0 - address of prompt to print
#$a1 - address to store string
#$a2 - Max string length
#$s0 - Loop counter
#$t3 - Offset to store information
#$t4 - Load the byte for exit loop
#$t5 - Lengths Array Offset
get_input:
    subi $sp,$sp,4 #Push return address onto the stack to save it
    sw $ra,($sp)
    li $s0,0   #initalize counter variable
    li $t3,0   #initalize offset variable
    li $t5,0   #Initalize offset variable
    
get_input_loop:
    beq $s0,16,end_get_input #Loop statement check
    la $a0,prompt1
    li $v0,4   #Syscall for prompt text
    syscall
    
    la $a0,strings($t3)    #$a1 has the location to store the string,move that into $a0 for the read word syscall
    move $a1,$a2       #$a2 has the maximum character buffer,move that into $a1 for the read word syscall
    li $v0,8
    syscall
                #$a0 Now has the inputted word—We want to manipulate. Trucnate and then store the word inside of the array.
    lbu $t4,($a0)      #Check to see if the inputted word is null or just a space
    beq $t4,$zero,end_get_input   #If so,exit loop
    
                #Trunc the string
    jal strtrunc
    beq $t4,0x20,end_get_input #If the inputted byte is a space,exit the loop
    
                #At this point $v0 has the length of the string and the truncatted string has been saved 
    sw $v0,lengths($t5)    #Store the length found in $v0 to lengths
    addi $t5,$t5,4    #Add 4 to the lengths offest
    add $t3,$t3,$v0   #Add whatever the length of the last string was to the word offest 
    addi $t3,1    #Add one more for the $zero we added in the strtrunc function
    addi $s0,$s0,1    #Add one to the total count of the strings array

    j get_input_loop
end_get_input:
    lw $ra,0($sp)
    addi $sp,4
    jr $ra
###############################################################
#Truncate string at newline character.
#$a0 - address of string
#Returns
#$v0 - Length of string
strtrunc:
    move $t0,$zero
    move $t1,$a0
str_loop:
    lbu $t2,($t1)          #Character at a spot in the word
    beq $t2,0x20 end_strtrunc  #If the byte is a space
    beq $t2,0xA end_strtrunc
    beq $t2,0x0 end_strtrunc
        addi $t0,$t0,1    #Length of string
        addi $t1,$t1,1    #Byte offset
    j str_loop
    
end_strtrunc:
    sb $zero,($t1)
    move $v0,$t0
    jr $ra

解决方法

Erik Eidt为我提供了一个非常简单的答案-我希望我不必一定要根据前一个字符串的大小/长度进行迭代,但是,这是我可以在用我目前的知识 由于我将所有字符串存储到数组中,因此我可以根据第一个字符串的大小+1迭代到第二个字符串。要到达第三个字符串,我必须知道第二个字符串的地址并根据第二个字符串的大小进行迭代。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。