2014-10-05 56 views
0

我有一個字符串數組,我想打印出來。這裏是目前我有什麼:如何在MIPS中打印字符串數組?

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僅適用於打印字符串。我不知道下一步該怎麼做...

+0

這不是一個字符串數組,這是一個長字符串。您沒有在任何地方記錄單獨單詞的起始地址。 – 2016-11-03 02:48:53

回答

-1

嘗試使用.ascii指令,因此它不會在您的字符串末尾放置空字符。

+0

雖然這實際上有幫助嗎?它仍然是一個巨大的字符串,沒有指向單獨的單詞的指針。並且這個MIPS模擬器中的字符串打印系統調用不是使用0結尾的字符串而是使用指針+長度嗎?在這種情況下,即使你有指向它們的指示,將所有沒有分隔符的單詞打包在一起也是不可用的。 – 2016-11-03 03:08:56

0

這不是一個字符串數組,這是一個長字符串。您沒有在任何地方記錄單獨單詞的起始地址。

另一個存放地址的數組可以讓它循環。

.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