While Loop in Assembly MIPS

Asked

Viewed 2,118 times

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 to 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 managed to make 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’t determine the part that comes in... Can someone please help me? Thank you very much!

1 answer

2


Assuming

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

The code would look 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.