MIPS assembly for a simple for loop

user977154 picture user977154 · Feb 6, 2012 · Viewed 81.1k times · Source

I need to translate this C code to MIPS assembly. Here is the C code:

int tmp = 0; 
for (int  j = 0; j < 15; ++j) 
     tmp = tmp * 2 + 3

This is my MIPS assembly code. Is it a correct translation? If you see any mistakes I would really like to know.

# tmp = $v0
# j = $t0

.globl main

 main:
    li $v0,0

loop:
    bgt $t0,15,exit
    addi $t0,$t0,1
    mul $t1,$v0,2
    add $v0,$t1, 3
    j loop  

exit:

Answer

MrD picture MrD · Feb 6, 2012

Your loop goes from 0 to 14, so your bgt instruction should be: bgt $t0,14,exit I think.

.