L08-ac-decisions2-proc1-6up

# L08-ac-decisions2-proc1-6up - inst.eecs.berkeley.edu/~cs61c...

This preview shows pages 1–3. Sign up to view the full content.

CS61C L8 Decisions(2), Procedures(1) (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #8 – Decisions (cont), Procedures 2008-07-03 CS61C L8 Decisions(2), Procedures(1) (2) Chae, Summer 2008 © UCB Review Memory transfer: lw, sw, lb, sb A Decision allows us to decide what to execute at run-time rather than compile- time. C Decisions are made using conditional statements within if , while , do while , for . MIPS Decision making instructions are the conditional branches : beq and bne . Unsigned add/sub don’t cause overflow New MIPS Instructions: beq, bne, j, sll, srl addu, addiu, subu, CS61C L8 Decisions(2), Procedures(1) (3) Chae, Summer 2008 © UCB Loops Review Key Concept : Though there are multiple ways of writing a loop in MIPS, the key to decision making is conditional branch do { g = g + A [ i ]; i = i + j ; } while ( i != h ); Loop: g = g + A [ i ]; i = i + j ; if ( i != h ) goto Loop ; CS61C L8 Decisions(2), Procedures(1) (4) Chae, Summer 2008 © UCB Inequalities in MIPS (1/3) Until now, we’ve only tested equalities ( == and != in C). General programs need to test < and > as well. Create a MIPS Inequality Instruction: “Set on Less Than” Syntax: slt reg1,reg2,reg3 Meaning: if (reg2 < reg3) reg1 = 1; else reg1 = 0; In computereeze, “set” means “set to 1”, “reset” means “set to 0”. reg1 = (reg2 < reg3); Same thing… CS61C L8 Decisions(2), Procedures(1) (5) Chae, Summer 2008 © UCB Inequalities in MIPS (2/3) How do we use this? Compile by hand: if (g < h) goto Less; #g:\$s0 , h:\$s1 Answer: compiled MIPS code… slt \$t0,\$s0,\$s1 # \$t0 = 1 if g<h bne \$t0,\$0,Less # goto Less # if \$t0!=0 # (if (g<h)) Less: Branch if \$t0 != 0 b ( g < h ) Register \$0 always contains the value 0 , so bne and beq often use it for comparison after an slt instruction. A slt b bne pair means if(… < …)goto… CS61C L8 Decisions(2), Procedures(1) (6) Chae, Summer 2008 © UCB Inequalities in MIPS (3/3) Now, we can implement < , but how do we implement > , and ? We could add 3 more instructions, but: MIPS goal: Simpler is Better Can we implement in one or more instructions using just slt and the branches? What about > ? What about ?

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

View Full Document
CS61C L8 Decisions(2), Procedures(1) (7) Chae, Summer 2008 © UCB Immediates in Inequalities There is also an immediate version of slt to test against constants: slti Helpful in for loops if (g >= 1) goto Loop Loop: . . . slti \$t0,\$s0,1 # \$t0 = 1 if # \$s0<1 (g<1) beq \$t0,\$0, Loop # goto Loop # if \$t0==0 # (if (g>=1)) C M I P S An slt b beq pair means if(… …)goto… CS61C L8 Decisions(2), Procedures(1) (8) Chae, Summer 2008 © UCB What about unsigned numbers? Also
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/06/2009 for the course CS cs61c taught by Professor Cory during the Summer '08 term at Berkeley College.

### Page1 / 6

L08-ac-decisions2-proc1-6up - inst.eecs.berkeley.edu/~cs61c...

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

View Full Document
Ask a homework question - tutors are online