Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

length of array in mips

Tags:

arrays

mips

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

like image 968
Yanh Huan Avatar asked Nov 29 '25 21:11

Yanh Huan


1 Answers

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!