Question

I have a problem with my code. I have an array matrix1 and my objective is to print another array matrix2. The element of the second array is matrix1[i]* i. The problem with my code is that the procedure of multiplication was only applied on the first number.

For example:

first array : 2 6 16 10
second array must be : 2 12 54 40
but it's : 2 4 6 8

Here's my code

multing:
bgt $t0,$s0,jump     #t0=i , $s0=dimention
lw $t1,0($a1)        #a1=address of first array , t1=matrix[i]
mul $t2,$t1,$t0      #t2=t1*t0
sw $t2,0($a2)        #store the element
addi $t0,$t0,1       #i=i+1
addi $a1,$a1,4       #next address in first array
addi $a2,$a2,4       #next address in second array
b multing            #repeat loop
Was it helpful?

Solution

There is nothing wrong with your algthm. It works as expected (except that 3*16 = 48). I suspect your are setting up your algthm wrong or printing out the array for verification incorrectly.

The following is a program using your algthm that works as expected and prints 2 12 48 40:

.data
array1: .word 2,6,16,10
array2: .word 0,0,0,0

.text
main:

la $a1 array1
la $a2 array2
li $t0 1
li $s0 4

multing:
bgt $t0,$s0,jump     #t0=i , $s0=dimention
lw $t1,0($a1)        #a1=address of first array , t1=matrix[i]
mul $t2,$t1,$t0      #t2=t1*t0
sw $t2,0($a2)        #store the element
addi $t0,$t0,1       #i=i+1
addi $a1,$a1,4       #next address in first array
addi $a2,$a2,4       #next address in second array
b multing            #repeat loop

jump:

move $t0 $zero
la $a1 array2
li $t1 4

printLoop:
    lw $a0 0($a1)
    li $v0 1
    syscall

    li $a0 ' '
    li $v0 11
    syscall

    addi $a1 4
    addi $t0 1

    blt $t0 $t1 printLoop

jr $ra
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top