coding girl
coding girl

Reputation: 9

while loop translate on mips

So, i have an exercise on which i have to translate this while code

"i=0; while (table[i] != value) { i = i+1; }"

on mips language! This is what i did so far :

add  $s3, $0, $0    # i=0;

Loop: sll  $t1, $s3, 2    # t1 = 4 * i
      add  $t1, $t1, $s6  # t1 = addres of table[i]
      lw   $t0, 0($t1)    # t0 = table[i]
      beq  $t0, $s5, Exit # if table[i] == value goto Exit
     addi $s3, $s3, 1    # i = i+1
      j    Loop           # repeat: goto Loop
Exit: ....                # the rest of the code "

I just have a question! Is it posible to have only one brach or only one jump? for example only beq or only jump? Just because i want to be my programm more quick!

Upvotes: 0

Views: 751

Answers (2)

Jim Buck
Jim Buck

Reputation: 20726

Yes. If you initialize your "i" to -1, increment "i" after Loop but before your test, and use a bne to Loop for your test, you can have a single jump/branch.

Upvotes: 1

user555045
user555045

Reputation: 64904

It is possible to only dynamically execute one branch per iteration (plus a constant overhead), but you'd still have two control flow instructions in the program.

In assembly there's a shortcut to do loop inversion without repeating the condition:

    j loop_test
loop_body:
    ...
loop_test:
    ...
    bcond loop_body

Upvotes: 0

Related Questions