LC-3 Control Structures

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Java Control Statements
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Introduction to C Programming
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Comp Sci Control structures 1 Ch. 5 Control Structures.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
LC-3 and Assembly code (2).  Minh Nguyen,  Tutorials:  Office hours:
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.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Python Control Flow statements There are three control flow statements in Python - if, for and while.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization”
Slide 1 PHP Operators and Control Structures ITWA 133.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CSI 3125, Preliminaries, page 1 Control Statements.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Computer Science 210 tutorial 5
ITEC 2600 Introduction to Analytical Programming
© 2016, Mike Murach & Associates, Inc.
Computer Science 210 Computer Organization
Selection Learning Objective: to be able to design algorithms that use selection.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Unit-1 Introduction to Java
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Chapter 12 Variables and Operators
Decision Making.
Branching Constructs Review
ITM 352 Flow-Control: Loops
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Chapter 13 Control Structures
FLOW OF CONTROL.
IF if (condition) { Process… }
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Selection Learning Objective: to be able to design algorithms that use selection.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Chapter 7 Assembly Language
Homework Any Questions?.
Control Structures Part 3
Conditional Statements
Loop Construct.
Program Flow.
‘break’ break ends execution of the current for, foreach, while, do-while or switch structure break accepts an optional numeric argument which tells it.
Chapter 3: Selection Structures: Making Decisions
Selections and Loops By Sarah, Melody, Teresa.
PHP CONDITIONAL STATEMENTS
How to allow the program to know when to stop a loop.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Location Counter (LC) = 0 while line of code <> END if ORG
Presentation transcript:

LC-3 Control Structures if, else if, while, for, switch

If statement Other cases ;if (R1 == R2) ; becomes ;if (R1 < R2) ; if (R1 - R2 != 0) skip code block if true IFLABEL NOT R3, R2 ;2’s complement ADD R3, R3, #1 ;2’s complement ADD R3, R1, R3 ; evaluate expression BR?? ENDIFLABEL ; test condition ;{ ; if code block ;} ENDIFLABEL Other cases ;if (R1 < R2) ; if (R1 - R2 ? 0) skip code block if true; BR?? ENDIFLABEL ; evaluate NZP bits ;what is the BRxxx to evaluate NZP condition? ;if (R1 > R2) ; if (R1 - R2 ? 0) skip code block if true BR?? ENDIFLABEL ; evaluate NZP bits; what does the “?” do in the statement “R1 - R2 ? 0”

IF - ELSEIF C Code: if (R1 == R2) { //if code block } else if ( R1 > R2 ) { // else if code block else { // else code block Assembly Code: IF ; evaluate R3 <- R2 - R1 to set condition BR?? ELIF ;{ if code block } BRNZP ENDIF ELIF BR?? ELSE ;{ else if code block } ELSE ;{ else code block } ENDIF What if the evaluation statements are unrelated? i.e. if (R1 == R2) ; do something elseif (R1== R3) ; do something different else ; do something else endif

WHILE LOOP C code: GETC; while ( R0 != ‘\r’) { //loop code block; } Assembly Code: GETC ;start of while loop WHILE ADD R1, R0, #-10 ; R1 = R0 + #-10 BR? ENDWHILE ; test loop condition ;{ start of loop body ;loop code block ;} end of loop body BRnzp WHILE ENDWHILE

DO WHILE LOOP C code: do { // loop code block GETC; } while ( R0 != ‘\r’); Assembly Code: DO ;{ start of loop ;loop code block GETC ;} end of loop AND R1, R0, #-10 ; set condition BR?? DO ;test loop condition ENDLOOP ;label not needed

FOR LOOP YOUR TURN C code: //for (init, test, next) for (R1 = -10, R1 > 0, R1--) { //start of loop body //loop code block } //end of loop body Assembly Code: YOUR TURN

FOR LOOP C code: //for (init, test, next) for (R1 = 10, R1 > 0, R1--) { //start of loop body //loop code block } //end of loop body Assembly Code: FOR AND R1, R1, #0 ; clear R1 ADD R1, R1, #10 ; init statement FORLOOP BR? ENDFOR ; test loop condition ;{ start of loop body ;loop code block ;} end of loop body ADD R1, R1, #-1 ; decrement loop index BRnzp FORLOOP ENDFOR

SWITCH STATEMENT YOUR TURN C Code: switch (R1) { CASE ‘0’: ; case 0 code block ;break CASE ‘1’: ; case 1 code block CASE ‘2’: ; case 2 code block Default: ; default case code block } Assembly Code: YOUR TURN

SWITCH STATEMENT Solution #1 Assembly Code: SWITCH ;Evaluate Test condition Case 0 BRz CASE_0 ;Evaluate Test condition Case 1 BRz CASE_1 ;Evaluate Test condition Case 2 BRz CASE_2 BRnzp DEFAULT CASE_0 ;Code block ;Code block BRnzp ENDSW CASE_1 CASE_2 DEFAULT ENDSW NOT A SOLUTION!!! WHY WONT THIS WORK?

SWITCH STATEMENT Solution #2 Assembly Code: SWITCH CASE0 ; ;Evaluate Test condition Case 0 BRnp CASE1 ;Code block BRnzp ENDSW CASE1 ; ;Evaluate Test condition Case 1 BRnp CASE2 CASE2 ; ;Evaluate Test condition Case 2 BRnp DEFAULT DEFAULT ENDSW