While Loop in MIPS Assembly

1

Good morning guys, I'm new to assembly and I'm in need of a little help if possible. I need to translate the following code into assembly:

// Sendo i,total e n os registradors $s1, $s2, $s3
i=0;
total=0;
while( i < n ) {
 total=total+X[i];
 i=i+1;
}

I've already been able to do the following commands:

 add $s1 , zero , zero
 add $s2 , zero , zero
Loop: slt $t0, $s1, $s3
 beq $t0, $zero, Exit
 add $t1, $s1, $s1
 ...
 addi $s1, $s1, 1
 j Loop

I can not determine which part comes in the '...' Could someone please help me? Thank you!

    
asked by anonymous 18.04.2016 / 14:31

1 answer

1

Assuming that

i     = $s1
total = $s2
n     = $s3

The code looks like this:

.text
    add $s1 , zero , zero  
    add $s2 , zero , zero  
Loop: 
    slt $t0, $s1, $s3  
    beq $t0, $zero, Exit  

    la $t1, endereco_de_X       # Pegar o endereço de X e passar para $t1
    add $t2, $t1, $s1           # Somar com i resultando no endereço de X[i]
    lw $t3, 0($t2)              # Pegar da memória $t3 = X[i]
    add $s2, $s2, $t3           # Somar $t3 no total $s2

    addi $s1, $s1, 1  
    j Loop
Exit:

.data
    endereço_de_X: .word 2, 4, 1, 6, 7, 9, 1, 3
    
20.04.2016 / 03:11