Going From C to MIPS Assembly Basic Operations: Loops ...

Going From C to MIPS Assembly

Basic Operations: Loops, Conditionals

Charles Gordon (Version 1.1, September 2000)

1 Overview

At this point in the course, you should be reasonably familiar with the basic concepts of MIPS assembly. This includes registers, instruction formats, addressing, and basic arithmetic and load/store operations. It might be good to review the first four sections of chapter 3 if you are still uncomfortable with these concepts. This guide will focus on the next step in learning assembly: conditional statements and loops.

2 If/Then/Else Statements

A generic if/then/else construct from C is given in figure 2.1. It consists of a conditional check followed by two possible code blocks. One of these (the then block) is executed if the conditional is true, and the other (the else block) is executed if it is false.

if( conditional ) { then block

} else { else block

}

Figure 2.1: C if construct

The assembly language version of figure 2.1 is given in figure 2.2. Notice that the conditional is the same, but the order of the else and then blocks has been reversed. This is because the conditional statement is also a branch command. If the condition is met the code branches to the then block, otherwise it continues on to the else block.

conditional else block j end then: then block end:

Figure 2.2: Assembly if construct

More concrete examples will be given in the last section of this guide, but here is a way to think about the program flow. The conditional statement is first tested and, if it is false, program flow continues into the else block. Once the else block has finished, the

C to MIPS Assembly (Part Two)

2

jump instruction skips the then block and goes straight to the end of the if/then/else construct. If the conditional evaluates to true the else block is skipped and the then block is executed. The only tricky parts for a C programmer are remembering the jump after the else block and remembering to reverse the order of the then and else blocks.

Figure 2.3 gives a list of the C conditional operators and their corresponding assembly language instructions. Assume that a is stored in register t0 and b is in register t1. Note that all of these instructions with the exception of beq and bne are psuedoinstructions. They are created with a combination of beq, bne and slti. See your text for more information.

a == b a != b ab a = b a == 0

C Conditional Operator

MIPS Assembly Instruction beq $t0, $t1, then bne $t0, $t1, then blt $t0, $t1, then bgt $t0, $t1, then ble $t0, $t1, then bge $t0, $t1, then beqz $t0, then

Figure 2.3: C and Assembly Conditional Operators

3 Loops

There are three distinct types of loops in C: do/while, while and for. It may come as a surprise to some of you that they are all functionally identical. In other words, you can take any for loop and turn it into a while loop with a bare minimum of effort. The different forms merely capture the different uses of loops. Figure 3.1 lists a C for loop and a corresponding while loop that should make this idea clear.

int i; for( i=0;i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download