Comparing and Branching ifs and loops Part B. LOOPS.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Introduction to C Programming
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
True or false A variable of type char can hold the value 301. ( F )
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
Comparing and Branching ifs and loops part A. JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?
CONTROLLING PROGRAM FLOW
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
1 ICS 51 Introductory Computer Organization Fall 2009.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Chapter 4 – C Program Control
Chapter 7: Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Boolean Expressions & the ‘if’ Statement
Control Structures – Selection
Looping and Repetition
Relational Operators Operator Meaning < Less than > Greater than
More about Numerical Computation
Coding Concepts (Basics)
The System.exit() Method
Expressions.
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Conditionals and Loops
Controlling Program Flow
Boolean Expressions September 1, 2019 ICS102: The course.
The boolean type and boolean operators
Looping and Repetition
Presentation transcript:

Comparing and Branching ifs and loops Part B

LOOPS

Recall the forever loop for ( ; ; ) { … } lp: … jmplp

while loop while (i < 100) { … } How can we do this in Assembler?

while loop while (i < 100) { … } lp: cmpeax, 100 jgedone;or jnl … jmplp done:

while loop i = 0; while (i < 100) { … ++i; } How can we do this in Assembler?

while loop i = 0; while (i < 100) { … ++i; } moveax, 0 lp: cmpeax, 100 jgedone;or jnl … inceax jmplp done:

for loop for (int i=0; i<100; i++) { … } How can we accomplish this in Assembler? But first, when is the condition evaluated?

for loop for (int i=0; i<100; i++) { … } How can we accomplish this in Assembler? But first, when is the condition evaluated? Before we enter the body of the loop.

for loop for (int i=0; i<100; i++) { … } ;same as the last while loop! moveax, 0 lp: cmpeax, 100 jgedone;or jnl … inceax jmplp done: i = 0; while (i < 100) { … ++i; }

for loop for (int i=10; i>0; i--) { … } How can we accomplish this in Assembler?

for loop for (int i=10; i>0; i--) { … } moveax, 10 lp: cmpeax, 0 jledone;or jng … deceax jmplp done:

do-while loop int i = 7; do { … i--; while (i > 2); moveax, 7 lp: … deceax cmpeax, 2 jglp They are the same, for a change!

LOOP instruction Decrements ecx. Loops (branches) if ecx is not equal to zero.

LOOP instruction int i = 7; do { … i--; while (i != 0); ;MUST be ecx movecx, 7 lp: … looplp

LOOP instruction The LOOP instruction is NOT a direct replacement for the for loop. for (int i=7; i>0; i--) { … } How is it different from the for loop? ;MUST be ecx movecx, 7 lp: … looplp

LOOP instruction The LOOP instruction is NOT a direct replacement for the for loop. for (int i=7; i>0; i--) { … } How is it different from the for loop? 1.Test is at end. 2.Test is ecx != 0. ;MUST be ecx movecx, 7 lp: … looplp

LOOP instruction How can we do this in Assembler w/out the loop instruction? movecx, 7 lp: … looplp

LOOP instruction movecx, 7 lp: … dececx jnzlp How can we do this in Assembler w/out the loop instruction? movecx, 7 lp: … looplp What do you think is faster?

LOOP instruction movecx, 7 lp: … dececx jnzlp How can we do this in Assembler w/out the loop instruction? movecx, 7 lp: … looplp What do you think is faster? Let’s see!

utilities The utilities.asm file (on the course’s software web page) contains the dump procedure and two other procedures that can be used to time instructions: – call resetTime resets the timer to zero and starts the timer running – call reportTime reports the elapsed time in milliseconds – Only one timer can be active at a time (no nested timers).

LOOP vs. DEC/JG instruction timing nop, nop, dec – repeat above via LOOP (2,000,000,000 times) required 12,110 milliseconds total ~6 ns each – repeat above via DEC/JG (2,000,000,000 times) required 4,047 milliseconds total ~2 ns each – Conclusion: DEC/JG is 3x faster than LOOP!

Single NOP instruction timing nop, nop, dec – repeat above via DEC/JG (2,000,000,000 times) required 4,047 milliseconds total ~2 ns each nop, nop, dec – repeat above via DEC/JG (2,000,000,000 times) required 2,890 milliseconds total ~1.5ns each – Conclusion: Each nop requires ~0.5ns.

A note regarding code line labels It becomes onerous to continually make up unique line labels. So... – Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous label, and label2 is either end of code or the next label. and – Refers to the location of the previous label – Refers to the location of the next label (forward).

A note regarding code line labels – Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous label, and label2 is either end of code or the next label. and – Refers to the location of the previous – Refers to the location of the next label. movecx, 7 lp: … dececx jnzlp How can we accomplish this w/out lp?

A note regarding code line labels – Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous label, and label2 is either end of code or the next label. and – Refers to the location of the previous – Refers to the location of the next label. movecx, 7 lp: … dececx jnzlp We can accomplish this w/out lp: movecx, 7 … dececx

A note regarding code line labels – Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous label, and label2 is either end of code or the next label. and – Refers to the location of the previous – Refers to the location of the next label. ; for (int i=0; i<100; i++) { moveax, 0 lp: cmpeax, 100 jgedone … inceax jmplp done: How can we accomplish this w/out lp and done?

A note regarding code line labels – Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous label, and label2 is either end of code or the next label. and – The location of the previous – The location of the next label. ; for (int i=0; i<100; i++) { moveax, 0 lp: cmpeax, 100 jgedone … inceax jmplp done: We can accomplish this w/out lp and done: moveax, 0 cmpeax, 100 … inceax

Quiz: Nested for loops #1 Write the following in Assembler. for (int i=0; i<100; i++) { for (int j=1; j<=10; j++) { … }

Nested for loops #2 Write the following in Assembler. for (int i=0; i<100; i++) { for (int j=10; j>=1; j--) { … }

Nested for loops #3 Write the following in Assembler. for (int i=0; i<100; i++) { for (int j=1; j<=i; j+=3) { … }

Java expressions #4 Convert the following to Assembler: int i = 100; int j = * i; You must define i and j.

Disjunction #5 Convert the following to Assembler: if ( f() || g() || h() ) { //disjuncts (short circuits) a = 10; } else { b = 99; } Note(s): Assume a and b are 32-bit integers and f, g, and h are boolean functions that are already defined for you. f, g, and h return values of 1 in eax to indicate true, 0 to indicate false.

Disjunction #6 Convert the following to Assembler: if ( f() & g() & a<100 ) { //does not disjunct (short circuit) a = 10; } else { b = 99; } Note(s): Assume a and b are 32-bit integers and f, g, and h are boolean functions that are already defined for you. f, g, and h return values of 1 in eax to indicate true, 0 to indicate false.

Operator precedence The closer to the top of the table an operator appears, the higher its precedence. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left. tutorial/java/nutsandbolts/operators.html tutorial/java/nutsandbolts/operators.html