Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIPS assembly for a simple for loop

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:
like image 572
user977154 Avatar asked Feb 06 '12 03:02

user977154


People also ask

How do you write a loop code in MIPS?

# 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: loops.

What is J loop in MIPS?

j. Loop # goto Loop. The label Loop lets us identify which assembly instruction should be executed after the branch. The label could be anything, as long as the MIPS assembler doeesn't misinterpret it as an instruction.

Which register always contains the value 0 and can never have its contents changed?

%g0 is unique: It always contains 0 and can never be changed. %g1 to %g4 are volatile across calls; they may be used between calls. %l0 to %l7 May be freely used; they are unaffected by deeper calls. These are also the caller's out registers; they are unaffected by deeper calls.


1 Answers

You don't set j ($t0) to zero before the loop.

like image 168
Richard Pennington Avatar answered Sep 20 '22 18:09

Richard Pennington