Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CENG 311 Decisions in C/Assembly Language
Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
Branches Two branch instructions:
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Assembly Language (cont)
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
Statement Format MIPS assembly language statements have the following format label: opcode operand,operand,operand # comment Label identifies the statement.
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Ch. 8 Functions.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.
MIPS Instruction Set Advantages
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
UNIT II Decision Making And Branching Decision Making And Looping
Making Decision – Microprocessor
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
9/29: Lecture Topics Conditional branch instructions
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
CDA 3101 Spring 2016 Introduction to Computer Organization
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
MIPS Coding. Exercise – the bubble sort 7/9/2016week04-3.ppt2.
Computer Architecture & Operations I
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Switch Statement Pre-requisites: 1D array addressing Updated 7/11/2013.
CS2100 Computer Organisation
Computer Architecture & Operations I
Decision Making.
Microprocessor and Assembly Language
Control Structures.
Instructions for Making Decisions
Flow of Control.
The University of Adelaide, School of Computer Science
Pick up the handout on your way in!!
Week 6 The darkness, the loop of negative thoughts on repeat, clamours and interferes with the music I hear in my head. Lady Gaga.
Flow of Control.
Control Flow and Arrays
How to represent signed integers
Instruction encoding The ISA defines Format = Encoding
Flow of Control.
Chapter 8: More on the Repetition Structure
MIPS Coding.
The University of Adelaide, School of Computer Science
MIPS Coding.
COMS 361 Computer Organization
MIPS Assembly.
MIPS assembly.
9/27: Lecture Topics Memory Data transfer instructions
Conditional Control Structure
MIPS instructions.
Conditional Branching (beq)
Control Flow and Arrays
Presentation transcript:

Comp Sci Control structures 1 Ch. 5 Control Structures

Comp Sci Control structures 2 Control structures Assembly language implementation of Selection (if, if-else, switch) Iteration (while, do-while, for)

Comp Sci Control structures 3 Comparison High-level relational operators: >= != MIPS conditional branch instructions: blt*, ble*, beq, bgt*, bge*, bne Syntax: bxx Rsrc1, Src2, label Examples: ble $t1, $t2, foo#branch if $t1 <= $t2 bgt $t1, 8, bar#branch if $t1 > 8 register constant

Comp Sci Control structures 4 If (without else clause) if(x < y) x = 0; if1 X = 0; < >=

Comp Sci Control structures 5 If (without else clause) if(x < y) x = 0; endif: lw $t0, x lw $t1, y bge $t0, $t1, endif li $t0, 0 sw $t0, x endif: Branch if greater or equal

Comp Sci Control structures 6 More conditional branches beqz*, bnez*, bgez, bgtz, blez, bltz Syntax: bxxz Rsrc, label Examples: blez $t1, foo#branch if $t1 <= 0 bgtz $t1, bar#branch if $t1 > 0 * psuedo-instructions

Comp Sci Control structures 7 Exercise: fill in the blank if(x != 0) x++; lw $t0, x addi $t0, $t0, 1 sw $t0, x endif:

Comp Sci Control structures 8 If-else -- first attempt if(x < y) x++; else y++; lw $t0, x#1 lw $t1, y#2 blt $t0, $t1, then#3 j else#4 then: lw $t0, x#5 addi $t0, $t0, 1#6 sw $t0, x#7 j endif #8 else: lw $t1, y#9 addi $t1, $t1, 1#10 sw $t1, y#11 endif: Unconditional Jump

Comp Sci Control structures 9 If-else -- improved version fall through to then clause if(x < y) x++; else y++; lw $t0, x#1 lw $t1, y#2 bge $t0, $t1, else#3 then: lw $t0, x#4 addi $t0, $t0, 1#5 sw $t0, x#6 j endif #7 else: lw $t1, y#8 addi $t1, $t1, 1#9 sw $t1, y#10 endif:

Comp Sci Control structures 10 If-else -- code the else clause first if(x < y) x++; else y++; lw $t0, x lw $t1, y else: lw $t0, $t0, y addi $t0, $t0, 1 sw $t0, y j endif then: lw $t0, x addi $t0, $t0, 1 sw $t0, x endif: blt $t0, $t1, then

Comp Sci Control structures 11 Boolean operators and short-circuiting Stop evaluating Boolean expression as soon as possible AND: stop as soon as an operand is False OR: stop as soon as an operand is True

Comp Sci Control structures 12 Pseudo code statement if(x < y && y < z) x++; else y++; If ( x >= y) goto else xlty:if ( y >= z) goto else then:x = x + 1; goto endif else:y = y + 1; endif: Boolean Table (x < y && y < z) true && true do x++ true && false do y++ false && true do y++ false && false do y++

Comp Sci Control structures 13 Exercise: generate MIPS code if(x < y && y < z) x++; else y++; Code then clause first Code else clause first

Comp Sci Control structures 14 Another Exercise: generate MIPS code if(x < y || y < z) x++; else y++; Code then clause first Code else clause first

Comp Sci Control structures 15 Nested control structures Then-clause, else-clause, loop body may contain – If or if-else – Do or do-while Generate code for nested constructs

Comp Sci Control structures 16 Nested if-else example if1: if(x < 100) then1: print("small"); else1: else { if(x < 200) then2: print("medium"); else2: else print("large"); } if1 if2 then1 then2else2 else1

Comp Sci Control structures 17 Loops Pre-test: (while) Post-test: (do-while) Evaluate loop entry condition Conditionally execute loop body

Comp Sci Control structures 18 Pre-test loop example while(x < y) x++; endloop: Top: if ( x >= y) goto endloop; x = x + 1; goto Top: endloop: top: lw $t0, x lw $t1, y bge $t0, $t1 endloop lw $t0, x addi $t0, $t0, 1 sw $t0, x j top endloop:

Comp Sci Control structures 19 Post-test loop exercise do{ x++; }while(x < y); body:lw $t0, x addi $t0, $t0, 1 sw $t0, x eval: lw $t0, x lw $t1, y blt $t0, $t1, body

Comp Sci Control structures 20 Smarter pre-test loop code j eval body: lw $t0, x addi $t0, $t0, 1 sw $t0, x eval: lw $t0, x lw $t1, y blt $t0, $t1, body Why is it smarter?

Comp Sci Control structures 21 Initial vs Smarter Pre-Test Loop Code top: Eval:lw $t0, x lw $t1, y bge $t0, $t1, endloop Body:lw $t0, x addi $t0, $t0, 1 sw $t0, x j top endloop: j eval body:lw $t0, x addi $t0, $t0, 1 sw $t0, x eval: lw $t0, x lw $t1, y blt $t0, $t1, body

Comp Sci Control structures 22 Loop summary Always code loop body first, entry condition last Pre-test: jump to entry condition before body Post-test: no jump before body

Comp Sci Control structures 23 For statement Equivalent to Pre-test while for (int i = 0; i < 10; i++) { //body;} Int i = 0; while ( i < 10) {// body; i ++; } Endwhile: Int I = 0; Begin: if ( I >= 10) goto Endwhile; { // body i++; goto Begin; } Endwhile:

Comp Sci Control structures 24 Switch statements Can be treated as linear nested if-else Can be implemented with “jump tables” – Sometimes quicker – More complex – Better studied after arrays