简体   繁体   中英

Checking if a number is not equal to zero in MIPS

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.

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, ie 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 , ie ! (condition) ! (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM