Error in assembly deviation (MIPS)

2

I need to transform the following c code into MIPS assembly:

int i;
int vetor[8];
  for(i=0; i<8; i++) {

    if(i%2==0)
    vetor[i] = i * 2;

    else
    vetor[i] = vetor[i] + vetor[i-1];
}

Doing this I came up with the following code:

.data
    .space 32 #reserva o espaço necessário pro vetor

.text
    #t0 = endereço inicial da memória de dados
    #t1 = divisor e multiplicador
    #t2 = contador
    #t3 = limite do contador
    #t4 = resto da divisão
    #t5 = resultado da multiplicação de i por 2
    #t6 = recupera o elemento da anterior da meméria para fazer a soma no else 

    lui $t0, 0x1001

    ori $t1, $zero, 2
    ori $t2, $zero, 0
    ori $t3, $zero, 8

    loop:       #faz o loop até 8(t3)
    beq $t2, $t3, exit

    div $t2, $t1        #divide i por 2
    mfhi $t4        #faz a a comparação do resto com $zero
    bne $t4 $zero, else #e pula para else caso seja impar

    mult $t1, $t2
    mfhi $t5 
    sw $t5, 0($t0)
    addi $t2, $t2, 1    #incrementa o contador
    addi $t0, $t0, 4    #incrementa o "ponteiro" para o vetor

    j loop

    else:

    lw $t6, 0($t0)
    add $t5, $t5, $t6   #reutiliza $t5 que está com o valor anterior e soma com a posição atual da memória 
    sw $t5, 0($t0)
    addi $t2, $t2, 1    #incrementa o contador
    addi $t0, $t0, 4    #incrementa o "ponteiro" para o vetor
    j loop

    exit:

But the loop itself only works for the first two iterations, after which it simply ignores the jump inside the else label. But if I comment (delete) the lines in the else , and leave only the loop counter uncommented it runs through for to the end.

else:

#lw $t6, 0($t0)
    #add $t5, $t5, $t6  #reutiliza $t5 que está com o valor anterior e soma com a posição atual da memória 
    #sw $t5, 0($t0)
    addi $t2, $t2, 1    #incrementa o contador
    #addi $t0, $t0, 4   #incrementa o "ponteiro" para o vetor
    j loop

But I did not understand why I'm not changing the loop-related variables anywhere ($ t2 and $ t3), except in the enhancer. Can anyone identify where I'm going wrong?

    
asked by anonymous 20.04.2015 / 04:39

1 answer

1
  • In the MIPS pipeline architecture, the bypass instructions change the PC in a moment where the next instruction has already been searched and is about to be executed (step 3 of the Machine Cycle);

  • Therefore, even if there is a jump or branch, the instruction a follow will be executed!
  • SOLUTION: Always use a nop (no operation) statement after statements of deviation.
20.04.2015 / 05:43