{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L08-ac-decisions2-proc1-6up

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

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

View Full Document Right Arrow Icon
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 barb4right ( g < h ) Register $0 always contains the value 0 , so bne and beq often use it for comparison after an slt instruction. A slt barb4right 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 ?
Image of page 1

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

View Full Document Right Arrow Icon
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 barb4right beq pair means if(… …)goto… CS61C L8 Decisions(2), Procedures(1) (8) Chae, Summer 2008 © UCB What about unsigned numbers? Also unsigned inequality instructions: sltu , sltiu …which sets result to 1 or 0 depending on unsigned comparisons What is value of $t0 , $t1 ? ( $s0 = FFFF FFFA hex , $s1 = 0000 FFFA hex ) slt $t0, $s0, $s1 sltu $t1, $s0, $s1 CS61C L8 Decisions(2), Procedures(1) (9) Chae, Summer 2008 © UCB MIPS Signed vs. Unsigned – diff meanings!
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern