0

I'm currently trying to translate a do - while loop from C to MIPS and am a bit confused when it comes to branch testing.

The code I'm trying to translate from C into MIPS is as follows:

do{

      code
} while(x != 0) 

In mips I've declared

loop: 
         #code

# and down here I should be translating while(x != 0)

How do I most effectively translate while(x != 0) using the branching statements? Seeing as once x == 0, the do while loop stops.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • Not sure about MIPS, but if you already know how to do any kind of branching, here you have to do `if (x != 0) jmp loop;` – yeputons Jan 19 '21 at 16:59
  • 1
    I suggest to use a C compiler to create a list file and look at the generated code. Where is your variable `x` located? In memory? In a register? What exactly do you mean with "most effectively"? Smallest or fastest code? – Bodo Jan 19 '21 at 16:59
  • @Bodo: Since MIPS can do that in one `bnez` instruction, there isn't a tradeoff. (Assuming x is in a register, and MIPS has plenty) – Peter Cordes Jan 19 '21 at 22:41

1 Answers1

0

Each structured statement has a translation into assembly language's if-goto style.

Often the condition needs to be reversed, when the sense of the condition in high level language is the opposite of branching, i.e. it is continuation.

To see what I mean, let's look at the while loop first.

while ( condition ) 
    body

The sense of the condition is continuation with the body that comes up next.

In the if-goto style, the only thing we can do is branch to exit the loop, instead, so we must reverse the condition, i.e. ! (condition).

    if ( ! condition ) goto LoopExit;
    body
LoopExit:

(The if-goto construct needs to choose between staying in the loop and exiting the loop — we cannot "branch" to stay in the loop, so the only option is to branch to exit and fall through to stay in the loop.)

Since the sense of the do while and sense of the if-goto are the same then the test condition is the same as in C.  This is because both are changing flow of control back to the top of the loop.

For more on translating structured statements into assembly's if-goto style see this article.

https://erikeidt.github.io/Transforming%20Structured%20Statements%20into%20Assembly%20Language

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • The OP's `do{}while(x!=0)` loop is already in a form that translates totally naturally to [idiomatic asm](https://stackoverflow.com/questions/47783926/why-are-loops-always-compiled-into-do-while-style-tail-jump), with just a `bnez $reg, LoopTop` at the bottom. Like this possible duplicate ([what would be the encoding of a bnez MIPS instruction?](https://stackoverflow.com/q/22875187)). I'm surprised the OP accepted this answer because to me it doesn't seem to be answering the (trivial) question of which asm instruction corresponds to the C `}while(x!=0);` statement. – Peter Cordes Jan 19 '21 at 21:19
  • @PeterCordes, I felt that showing while would be more illuminating, since as you say the other is more trivial. – Erik Eidt Jan 19 '21 at 22:31
  • Yes, I realized. the existance of bnez a bit too late, I could have also done something with beq and the $zero register. – caSino_Mafia Jan 20 '21 at 00:00