Yanh Huan
Yanh Huan

Reputation: 163

length of array in mips

How to find length of an array in mips,spim?

Upvotes: 3

Views: 20953

Answers (1)

user916171
user916171

Reputation:

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

Upvotes: 5

Related Questions