Welcome to Computer Organization and Assembly!

Flow, Conditionals, and Loops CS/COE 0447 Lus Oliveira Original slides by: Jarrett Billingsley Modified with bits from: Bruce Childers, David Wilkinson So far Putting numbers into registers li a0, 3 la a0, x labe l COPYing register contents move a0, t0 Unsigned

! COPYing from/to memory .data x: .word 4 These do zero extension lw/sw, lh/lhu/sh, lb/lbu/sb la t1, x lw t0, 0(t1) lw t0, x la t1, x sw t0, 0(t1) sw t0, x Do the same thing

2 In another perspective lw, lh, lhu, lb, lbu move CPU Memory Registers li, la sw, sh, sbo use t w o h d e n ar

e l ve e W a ! r o t a l u c cal Other operations add

sub mul syscall Datatypes word half byte asciiz 3 Introduction to conditions What distinguishes a computer from a calculator? It can make decisions based on values that it calculates o If the value of this register is this, do something. o Otherwise, do something else. The possible decisions make up the potential control flow of the program. o When there is no possible route to a piece of code in your

program, that is called dead code. Its like procrastination! if(false) { do_some_work() } 4 Control flow 5 With great power Control flow determines the order that your instructions run in o What kinds of control flow statements do you know of? o What about functions? In asm, the only thing you get for free is that instructions run in order You're responsible for coming up with everything else. o If you screw up your control flow, the CPU doesn't care o You'll just have a broken, malfunctioning program

And it'll be half an hour before the lab is due And you'll be sad This is like 90% of the bugs 6 Getting a little further from familiarity all control flow is done with branches and jumps o these are instructions which say "go somewhere else" for example this is an infinite loop, _main_loop: which is sometimes # clear screen useful but not too # draw one thing interesting # sleep # draw another thing # etc j stands for jump" go somewhere else j _main_loop

7 Building blocks A basic block is a chunk of code that has no control flow in it Control flow statements separate basic blocks if(x == w - 1) { do_thing() } else { other_thing() } third_thing() x == w - 1? other_thi ng do_thing third_thin g

thinking about this is REAL HELPFU 8 Essentially The way control flow works in asm is you make basic blocks o You gotta name (label) them Then, you use special instructions to choose where to go o Ask yourself Which basic block runs next?" o Select the instruction you need! Dont worry, we look into these instructions in a moment And dont forget! o Write pseudo-code (with comments) to keep track of control flow o Or make a drawing of a flow-chart! o Or any other guide you think its helpful 9 Conditionals: if and if-else 10

MIPS ISA: conditional branch instructions conditional branch instructions do one of two things: o if the condition is met, we go to the label o otherwise, nothing happens, and we go to the next instruction Instruction Meaning beq a, b, label if(a == b) { goto label } bne a, b, label if(a != b) { goto label } above, a must be a register, but b can be a register or immediate (by the powers of the pseudo-instruction) 11 How do these work? Previous instructio n

This is the branch beq t0, t1, label # branch if equal t0==t1 True False Next instructio n label: Other instructio n 12

How do these work? label: beq t0, t1, label # branch if equal Other instructio n Previous instructio n True t0==t1 This is the branch False Next instructio n 13

How to write Asm (again!) Remember: WRITE PSEUDOCODEALWAYS REALLY!!! E if(x == w - 1) { D I S N I F F U do_thing() T S E H T

E } else { NOR IG other_thing() TRANSLATE T H E CONTROL F } LOW TO ASM FIRST!!! !!! 14 Like mad libs, but for code From now on, Ill use these 'blocks' to represent the basic blocks o cause they dont matter if(some condition) { block A

} else { block B } block C 15 A simple conditional block (if) If there is no else, it's pretty simple. if(s0 == 30) { block A } block B bne s0, 30, blockB blockA: W a it , Is n t t w

blockB: ha t ha t ? Bra ??? equ nch a l? NO T O.o 16 A simple conditional block (if) If there is no else, it's pretty simple. if(s0 == 30) { block A } block B bne s0, 30, blockB In Java/C what happens in an if?

You JUMP OVER when the condition is true or false? When its FALSE!! 17 A simple conditional block (if) In MIPS you jump when the condition is TRUE if(s0 == 30) { block A } block B unique h it w p u g in com ntrol

o c e h t ll a r fo names d of a in k is ls e b la flow chore bne s0, 30, blockB blockA:

block A blockB: block B 18 An if-else with a simple condition more blocks now if(s0 == 30) { block A } else { block B } block C bne s0, 30, blockB block A j blockC blockB: block B blockC:

block C we NEED THIS the CPU doesn't see/care about your labels!! 19 The other way around Because in HLL we execute smth if and In assembly we jump over if We usually negate the condition in the assembly to skip over code o Its a preference. o You can still invert the process beq s0, 30, blockA How? if(s0 == 30) { } else { } block A

block B block C j blockElse blockA: block A j blockExit # skip the else blockElse: block B blockExit: block C 20 MIPS ISA: conditional branch instructions MIPS also supports instructions that compare to zero Instruction bltz a, label blez a, label bgtz a, label bgez a, label

Meaning if(a < 0) { goto label } if(a <= 0) { goto label } if(a > 0) { goto label } if(a >= 0) { goto label } 21 MIPS ISA: set if less than And Instruction slt b c, a, Meaning if(a < b) { c = 1 } else { c = 0 }

Set if Less Than: register c will be set to 1 if ab, ab, a

} bgt a, b, if(a > b) { goto label label } above, a must be a register, but b can be a register or bge a, b, if(a >= b) { goto label immediate label } 23 Examples Example 1: branch if a>b bgt a, b, label # Goto label if a>b Solution: branch if b

# t=1 if b

if dog_size is 3, is dog_name() called? NO! this is short circuit evaluation. for || (logical OR), if the first condition is true, the second one is skipped. (cause there's no way for the result of the OR to be false.) for && (logical AND), if the first condition is false, the second one is skipped. 26 In this code if(dog_size < 10) small(); if dog_size is 3, is this else if(dog_size < 20) condition checked? medium(); NO!

else if(dog_size < 30) large(); else enormous(); once a true condition is found, no more conditions are checked. after small(), it comes down here. 27 And-and! Block A is run if both conditions are true. o to think of it another way it's skipped if? Whats the inverse? o either condition is false if(s0 == 30 && s1 > 1) { block A } bne s0, 30, skipA ble s1, 1, skipA

block A skipA: 28 Or-or! We go to block A if either condition is true. o to think of it another way it's skipped if? Whats the inverse? o all conditions are false. if(s0 == 30 || s1 > 1) { block A } beq s0, 30, blockA ble s1, 1, skipA blockA: block A skipA: 29

Looooops o o o o o o oooo 30 Dis-assembling a for-loop How does a for loop work? What is the first thing a for does? Initialize: i=0 And??? for(i=0; i<10; i++) Check condition: { execute while block A Then i<10

} // carry on block A Finally? Increment: i++ Go back up to the 31 Looping in MIPS assembly Whats the li s0, 0 __________first thing a Lets use s0 to loop_top: for does? hold i which conditional branch? for(i=0; i<10; i++)

__________________ { block A block A } How do we // carry on increment? addi s0, s0, 1 _______________ j loop_top How do ____________ Lets start with a recipe carry_on: we go up? # carry on 32 Thats bge, actually We want to leave the loop

when the opposite of i<10 happens! li s0, 0 __________ loop_top: bge s0, 10, carry_on _____________________ s0 move a0, ___ li v0, 1 syscall In HLL we execute smth if addi s0, s0, 1 In assembly we jump over _______________ if j loop_top ____________ Thus negate the condition carry_on:

in the assembly to skip # carry on over code 33 The other way around li s0, _________ _0 loop_top blt s0, 10, : loop_code b loop_code carry_on : move a0, s li 0 ___v0, s 1yscal l

addi s0, s0, _____________ 1 __ j ___________ loop_top carry_on _ # carry : 34 While looks the same, no initialization or increment while(s2 < 10) { // stuff!! } // more stuff

loop_top: bge s2, 10,more_stuff ________ stuff: # stuff!! j loop_top more_stuff: # more stuff 35

Recently Viewed Presentations

  • Micro Economics

    Micro Economics

    Earlier economists supported the policy of 'laissez fair' meaning leave free. They advocated minimum government intervention in the economic activities. The following are the main features of a capitalist economy; Free enterprise . Private property. Freedom of Contract. Profit Motive....
  • Pelaksana Un-cbt

    Pelaksana Un-cbt

    KANWIL KEMENTERIAN AGAMA PROVINSI JAWA TIMUR SOSIALISASI KEBIJAKAN DAN PELAKSANAAN UJIAN NASIONAL TAHUN PELAJARAN 2015/2016 * PENGOLAHAN HASIL UJIAN Penambahan dari POS tahun lalu: Perguruan tinggi: Memusnahkan LJUN SMA sederajat satu tahun setelah pelaksanaan ujian disertai dengan berita acara dan...
  • Summer Reading Here are some books that can

    Summer Reading Here are some books that can

    So You Want to Talk About Race. by Ijeoma Oluo. Oluo, editor-at-large of The Establishment, addresses America's current racial landscape, including the Black Lives Matter movement, white privilege, and police brutality. "Learning to talk about race requires practice. [This book]...
  • UCSF Presentation Template

    UCSF Presentation Template

    Derek MacDavid | [email protected] SAHMS Method & Objectives. Method: Use Respondent Driven Sampling (RDS) to recruit a surveillance sample of at least 500 FSW in Johannesburg, Cape, and eThekwini metros. To estimate the size of the FSW population in each...
  • A Long Way Gone

    A Long Way Gone

    Find the phrase "a long way gone" in this chapter. What is the context for this line? What is happening? Why do you think Ishmael chose this as the title of the book? Explain it. Who is the anonymous man...
  • Character  The main character in a story is

    Character The main character in a story is

    Antagonists are usually a round character. Example: Shrek from Shrek. Dynamic Character A character who experiences an essential change in personality or attitude. Protagonists are almost always dynamic. ... Flat characters help move the plot along more quickly because the...
  • Physics of LIGO, lecture 1b

    Physics of LIGO, lecture 1b

    Within each (AB) set, the adiabatic index Gr is varied from 1.325 to 1.28 (stiff to soft), and the peak amplidue of the wave depends strongly on this parameter, as seen in the right.
  • ASSESSMENT OF TRANSVERSAL SKILLS 2020 Enhancement of students

    ASSESSMENT OF TRANSVERSAL SKILLS 2020 Enhancement of students

    Proposes a comprehensive learning model to enhance students' transversal, 21st century skills across diverse EU national curricula. Introduces teachers to new pedagogical approaches and innovative tools for the development and assessment of . ... Trainer's Nameā€¦.