【问题标题】:How do you print an array of strings in MIPS?如何在 MIPS 中打印字符串数组?
【发布时间】:2014-10-05 00:32:36
【问题描述】:

我有一个字符串数组,我想将它们打印出来。这是我目前拥有的:

data: .asciiz "foo", "bar", "hello", "elephant"...(16 of these strings)
size: .word 16


    move $s0, $zero # i = 0
    la $s1, data # load array
    la $s2, size #load size
print_array:


bge $s0, $s2, exit # i >= size -> exit

la $a0, 0($s1) #load the string into a0
li $v0, 4 #print string
syscall

addi $s0, $s0, 1 # i++
addi $s1, $s1, 4

j print_array

exit:
    jr $ra

我知道这行不通,因为 li $v0, 4 仅用于打印字符串。我不知道接下来该做什么......

【问题讨论】:

  • 这不是字符串数组,而是一个长字符串。您还没有在任何地方记录单独单词的起始地址。

标签: arrays assembly printing mips


【解决方案1】:

那不是一个字符串数组,那是一个长字符串。您没有在任何地方记录单独单词的起始地址。

另一个保存地址的数组可以循环遍历它。

.section .rodata

data1: .asciz "foo"
data2: .asciz "bar"
data3: .asciz "hello"
data4: .asciz "elephant"
# ...(16 of these strings)

array_of_strings:
    .word data1, data2, data3, data4, ...
    .word 0      // NULL-terminate the list if you want, instead of using the end-address
array_of_strings_end:

# Or calculate the size at assemble time (/4 to scale by the word size, so it's an element count not a byte count)
# storing the size in memory is pointless, though; make it an assemble-time constant with .equ

.equ size, (array_of_strings_end - array_of_strings)/4

还要注意它是.asciz,而不是.asciiz。

【讨论】:

    【解决方案2】:

    尝试使用.ascii 指令,这样它就不会在字符串末尾放置空字符。

    【讨论】:

    • 这实际上有什么帮助呢?它仍然是一个巨大的字符串,没有指向单独单词的指针。这个 MIPS 模拟器中的 string-print 系统调用不是采用 0 结尾的字符串而不是指针+长度吗?在这种情况下,即使您有指向它们的指针,将所有单词打包在一起而没有分隔符也是不可用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多