14

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:
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user977154
  • 1,045
  • 4
  • 19
  • 39

5 Answers5

11

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

.

MrD
  • 2,423
  • 3
  • 33
  • 57
3

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

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
0

I also don't know what MIPS simulator you're running, but I know some of them don't constants and they demand you assign those to registers. So like bgt Rsrc1, Src2, label, normally if you put an integer in src2 the computer will translate that but I know for some you'll get an error doing add $v0, $t1, 3 as it won't translate add into addi. Same with mul. I know my SPIM simulator doesn't allow it.

Meech
  • 1
  • 1
0
    add $vo, $vo, $zero
    add $t0, $t0, $zero
LOOP:
    slti $t1, $t0, 15
    beq $t1, $zero, EXIT
    addi $t0, $t0, 1
    addi $t2, $zero, 2
    mul $t3, $v0, $t2
    addi $v0, $t3, 3
    j    LOOP
EXIT:
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Maria
  • 67
  • 1
  • 3
  • 7
  • 3
    Was your goal to avoid pseudo-instructions or something? SO answers should include text to explain what the point of the code is, and why it's a good answer. `$vo` isn't a register name (first instruction used that). There's no need to use the `mul` instruction at all; you could just `addu $v0, $v0, $v0` or `slli $v0, $v0, 1` to multiply it by 2. (And another optimization would be to put the loop branch at the bottom, counting down `$0` toward zero so you can just use `add $t0, $t0, -1` / `bnez $t0, LOOP` instead of spending 3 instructions on branching and comparing) – Peter Cordes Mar 10 '22 at 15:07
  • In short, while this looks like a valid answer (we're probably not going to delete it), it's not very useful as an answer. Please [edit] and explain what you are trying to do. – General Grievance Mar 11 '22 at 00:58
-1
.data
mensage: asciiz "Text Test"
newline: asciiz "\n"
.text

# tmp = $v0
# j = $t0
 
main:
    li $t0,0
    li $t1,0
    li $t3,0
loop:
    bgt $t0,15,exit
    addi $t0,$t0,1
    j loop
    mul $t1,$t1,2
    add $t3,$t1,3  
exit:
    li $v10,0
    syscall
Matias
  • 459
  • 1
  • 4
  • 14
  • Shouldn't `li $v0,0` be `li $v0, 10`? Is there really any `syscall` with value `0` in MIPS? – user366312 Mar 24 '19 at 05:39
  • Thanks for pointing it out, @user366312. I requested an edit. – Matias Feb 03 '21 at 02:21
  • The mul and add aren't inside the loop, so this doesn't match the C in the question. Also, you only need a left shift or add, not an actual multiply, for a constant `2`. – Peter Cordes Mar 10 '22 at 15:02