简体   繁体   中英

Array strings in MIPS - an issue with a loop

I am writing a code to display an array containing a number of Strings. Therefore I used a loop. However my counter doesn't work properly and I receive an error message and the first element (string) of an array. Could anybody help me, please?

'# $s0=count, $s1=total, $s3=data $s7=index

.data

Mssg1: .asciiz "Hello\\n"

Line1: .asciiz "aab\\n"

Line2: .asciiz "ggdhj\\n"

Line3: .asciiz "uio\\n"

array: .word Line1, Line2, Line3

.globl main
.text

main: li $s1, 0 # total=0 li $s0, 3 # count=3 la $s7, array # index

li $v0, 4       # print_str "Hello"
la $a0, Mssg1       
syscall

l1: lw $s3, 0($s7) # data=sum[index]

add $s1, $s1, $s3   # total=total+data

addi $s7, $s7, 4    # increment
addi $s0, $s0, -1   # decrement

bne $s0, $0, l1     # if not equal 0 go back onto the loop 


add $t0, $0, $s0    # t0=s0

li $v0, 4
move $a0, $t0
lw $a0, array       # print_str 
syscall'

$ t1的值不会在循环中更改,因此$ t7增加,您会得到不存在的数组索引:

lw $t2,0($t7)    # data=array[index]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM