MIPS2 - Control flow in high-level languages The...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
24 ± The instructions in a program usually execute one after another, but it’s often necessary to alter the normal control flow. ± Conditional statements execute only if some test expression is true. // Find the absolute value of *a0 v0 = *a0; if (v0 < 0) v0 = -v0; // This might not be executed v1 = v0 + v0; ± Loops cause some statements to be executed many times. // Sum the elements of a five-element array a0 v0 = 0; t0 = 0; while (t0 < 5) { v0 = v0 + a0[t0]; // These statements will t0++; // be executed five times } Control flow in high-level languages
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
25 ± In this lecture, we introduced some of MIPS’s control-flow instructions j immediate // for unconditional jumps jr $r1 // jump to address stored in $r1 bne and beq $r1, $r2, label // for conditional branches slt and slti $rd, $rs, $rt // set if less than (w/ and w/o an immediate) $rs, $rt, imm ± And how to implement loops ± Today, we’ll talk about MIPS’s pseudo branches if/else case/switch MIPS control instructions
Background image of page 2
26 ± The MIPS processor only supports two branch instructions, beq and bne , but to simplify your life the assembler provides the following other branches: blt $t0, $t1, L1 // Branch if $t0 < $t1 ble $t0, $t1, L2 // Branch if $t0 <= $t1 bgt $t0, $t1, L3 // Branch if $t0 > $t1 bge $t0, $t1, L4 // Branch if $t0 >= $t1 ± Later this term we’ll see how supporting just beq and bne simplifies the processor design. Pseudo-branches
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
27 ± Most pseudo-branches are implemented using slt. For example, a branch-if-less-than instruction blt $a0, $a1, Label is translated into the following. slt $at, $a0, $a1 // $at = 1 if $a0 < $a1 bne $at, $0, Label // Branch if $at != 0 ± All of the pseudo-branches need a register to save the result of slt , even though it’s not needed afterwards. MIPS assemblers use register $1 , or $at , for temporary storage. You should be careful in using $at in your own programs, as it may be overwritten by assembler-generated code. Implementing pseudo-branches
Background image of page 4
28 Translating an if-then statement ± We can use branch instructions to translate if-then statements into MIPS assembly code. v0 = *a0; lw $t0, 0($a0) if (v0 < 0) bge $t0, $zero, label v0 = -v0; sub $t0, $zero, $t0 v1 = v0 + v0; label: add $t1, $t0, $t0 ± Sometimes it’s easier to invert the original condition. In this case, we changed “continue if v0 < 0 ” to “skip if v0 >= 0 ”. This saves a few instructions in the resulting assembly code.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
29 ± If there is an else clause, it is the target of the conditional branch And the then clause needs a jump over the else clause // increase the magnitude of v0 by one if (v0 < 0) bge $v0, $0, E v0 --; sub $v0, $v0, 1 jL else v0 ++; E: add $v0, $v0, 1 v1 = v0; L: move $v1, $v0 ± Dealing with else-if code is similar, but the target of the first branch will be another if statement. Drawing the control-flow graph can help you out. Translating an if-then-else statements
Background image of page 6
30 Example of a Loop Structure for (i=1000; i>0; i--) x[i] = x[i] + h; Assume: addresses of x[1000] and x[0] are in $s1 and $s5 respectively; h is in $s2; Loop: lw $s0, 0($s1) ;$s1=x[1000] add $s3, $s0, $s2 ;$s2=h sw $s3, 0($s1) addi $s1, $s1, # - 4 bne $s1, $s5, Loop ;$s5=x[0]
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
31 Case/Switch statement ± Many high-level languages support multi-way branches , e.g.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/01/2009 for the course COE 0142 taught by Professor Staff during the Summer '08 term at Pittsburgh.

Page1 / 31

MIPS2 - Control flow in high-level languages The...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online