length of array in mips
How to fi开发者_开发百科nd length of an array in mips,spim?
I wrote this for practice. I tested it, and it works well. You probably already figured this out, but if not, there it is.
.data
array1: .word 1,2,3,4,5,6,7,8,9
.text
main:
la $a0,array1
jal lenArray
move $a0,$v0
syscall $print_int
exit:
li $a0,10
syscall
lenArray: #Fn returns the number of elements in an array
addi $sp,$sp,-8
sw $ra,0($sp)
sw $a0,4($sp)
li $t1,0
laWhile:
lw $t2,0($a0)
beq $t2,$0,endLaWh
addi $t1,$t1,1
addi $a0,$a0,4
j laWhile
endLaWh:
move $v0,$t1
lw $ra,0($sp)
lw $a0,4($sp)
addi $sp,$sp,8
jr $ra
精彩评论