Click to See Complete Forum and Search --> : post Assembly - How to access array cells (besides the base!)


HulioG
October 1st, 2010, 08:53 AM
My script below - will pass the base array $t0 and print it's content (which is 4). The array contains "4,6,2,3,11". I can access the first letter of an array. "4" but none of the others.

My issue - I don't know how to access the other individual array cells from my lin_search() function.

0($t0) [4]
4($t0) [6]
8($t0) [2]
16($t0) [3]
20($t0) [11]

My thoughts - I know the operation has something to do with add or mul. The local variable in lin_search is $t6. Perhaps it has something to do with 4($t0). I've spent the whole day on this with no luck.

###################################################

#Registers used:
# $t0-$t5 - used to hold the result.
# $v0, $a0 - syscall parameter.

.data
array: .space 20
.text

main:
la $t0, array #load array into base $t0.

li $t1, 4 #load 4 into register
li $t2, 6
li $t3, 2
li $t4, 3
li $t5, 11

sw $t1, 0($t0) #store 4 in array.
sw $t2, 4($t0)
sw $t3, 8($t0)
sw $t4, 12($t0)
sw $t5, 16($t0)

lw $a0, 0($t0) #$t7 = lin_search( $a0 )
jal lin_search
move $t7, $v0

move $a0, $t7 #print contents of $t7
li $v0, 1
syscall

li $v0, 10 #syscall code 10 = exit
syscall

lin_search:
move $t6, $a0 #accept parameter in $a0. store in local $t6.
ret:
move $v0, $t6 #return via $v0
jr $ra

##############################################################