如何在MIPS中打印字符串数组? [英] How do you print an array of strings in MIPS?
本文介绍了如何在MIPS中打印字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串数组,我想将它们打印出来.这是我目前拥有的:
I'm having an array of strings and I want to print them out. Here's currently what I have:
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仅用于打印字符串.我不确定从这里下一步该怎么做...
I know this won't work because li $v0, 4 is for printing strings only. I am not sure what to do next from here...
推荐答案
这不是一个字符串数组,而是一个长字符串.您尚未在任何地方记录单独单词的起始地址.
That's not an array of strings, that's one long string. You haven't recorded the start-address of the separate words anywhere.
另一个保存地址的数组将使其可以循环遍历.
Another array holding addresses would make it possible to loop over it.
.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
.
Also note that it's .asciz
, not .asciiz
.
这篇关于如何在MIPS中打印字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文