LAB Flow Control Instructions

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Flow of Control Instruction/Control structure Looping structure Looping structure Branching structure Branching structure For assembly language program.
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
Flow Control Instructions
1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt.
Topics Control Flow Structure – Conditional Jump – Unconditional Jump Control Flow Structures – IF-THEN – IF-THEN-ELSE – CASE Branches with Compound Conditions.
Lecture 3 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Chapter 4 - Implementing Standard Program Structures in 8086 Assembly Language from Microprocessors and Interfacing by Douglas Hall.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
(Flow Control Instructions)
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
1 ICS 51 Introductory Computer Organization Fall 2009.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
ACOE251Sofware Constructs1 Software Constructs What they are …..
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
Conditional Loop Instructions, Conditional Structures
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
Comparison Instructions Test instruction –Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
Assembly Language Programming Petra University Dr. Hadi Hassan Conditional Processing.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Microprocessor and Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
Computer Organization and Assembly Language
Flags Register & Jump Instruction
Computer Science 210 Computer Organization
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
University of Gujrat Department of Computer Science
EECE.3170 Microprocessor Systems Design I
High-level language structures
Assembly Language for Intel 8086 Jump Condition
Jump & Loop instructions
Chapter 7 –Program Logic and Control
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Part VI Looping Structures
Presentation transcript:

LAB Flow Control Instructions CHAPTER 3 LAB Flow Control Instructions

Overview For assembly language programs to carry out useful tasks, there must be a way to make decisions and repeat sections of code –> jump and loop instructions. The jump and loop instructions transfer control to another part of the program. This transfer can be unconditional or can depend on a particular combination of status flag settings.

1. CONDITIONAL JUMPS JNZ is an example of a Conditional Jumps Instruction. The syntax is: Jxxx dest_label If the condition for the jump is true, the next instruction to be executed is the one at destination_label, which may precede or follow the jump instruction itself. If the condition is false, the instruction immediately following the jump is done next. For JNZ, the condition is that the result of the previous operation is not zero.

How the CPU Implements a Conditional Jumps The CPU looks at the FLAGS register. If the conditions for the jump (expressed as a combination of status Flag setting) are true, the CPU adjusts the IP to point to the destination label, so that the instruction at this label will be done next. If the jump condition is false, then IP is not altered; this mean that the next instruction in line will de done.

The CMP instruction The jump condition is often provided by the CMP (compare) instruction . CMP dest, source This instruction compared destination and source by computing destination contents minus source content. The result is not stored, but the flags are affected. The operand of CMP may not both be memory location. Destination may not be a constant. Note: CMP is just like SUB, except that destination is not changed.

The CMP instruction Eg: CMP AX, BX JG BELOW Where AX = 7FFFh and BX = 0001h. Result of CMP AX, BX is 7FFFh – 0001h = 7FFEh. AX is greater than BX (in signed sense), then JG (jump if greater than) transfer to BELOW. From table 6.1 shows that the jump condition for JG is satisfied -> ZF=SF=OF = 0, so control transfer to label BELOW.

Signed VS Unsigned Jumps Eg: AX = 7FFFh and BX = 8000h, and execute CMP AX, BX JA BELOW even though 7FFFh > 8000h in signed sense, program does not jump to BELOW. Reason: 7FFFh < 8000h in unsigned sense, and we are using the unsigned jump JA.

2. The JMP Instruction The JMP (jump) instruction causes an unconditional transfer of control (unconditional jump). The syntax is JMP destination Where destination is usually a label in the same segment as the JMP itself. Eg: TOP: ; body of the loop MOV CX, 5 DEC CX ; decrement counter JNZ BOTTOM ; keep looping if CX>0 JMP EXIT BOTTOM: JMP TOP EXIT: MOV AX, BX

3. HIGH-LEVEL LANGUAGE STRUCTURES BRANCHING STRUCTURES – enable a program to take different paths, depending on conditions. We’ll look at 3 structures: IF-THEN In pseudocode IF condition is true THEN execute true-branch statements END_IF The condition is an expression that is true or false. If true, the true-branch statements are executed. If false, nothing is done, and the program goes on to whatever follows.

Example: IF-THEN Replace the number in AX by its absolute value. Sol: A pseudocode algorithm is IF AX < 0 THEN replace AX by –AX END IF It can be coded as follows: ; if AX < 0 CMP AX, 0 ; AX < 0? JNL END_IF ; no, exit ;then NEG AX ; yes, change sign END_IF Note: JNL (jump if not less), that is, if AX not less than 0, jump to label END_IF.

IF-THEN-ELSE IF condition is true THEN execute true-branch statements ELSE execute false-branch statements END_IF If condition is true, the true-branch statements are executed. If condition is false, the false-branch statements are executed

Example: IF-THEN-ELSE Suppose AL and BL contain extended ASCII characters. Display the one that comes first in the character sequence. Sol: IF AL <= BL THEN display the character in AL ELSE display the character in BL END_IF

MOV AH, 2 ; prepare to display It can be coded as follows: MOV AH, 2 ; prepare to display ;if AL <= BL CMP AL, BL ; AL <= BL? JNBE ELSE_ ; no, display char in BL ; AL <= BL ;then MOV DL, AL ; move char to be displayed JMP DISPLAY ; go to display ; BL < AL ELSE_: MOV DL, BL DISPLAY: INT 21h ; display it END_IF Note: the label ELSE_ is used because ELSE is a reserved word. The condition AL <= BL is expressed by CMP AL, BL.

CASE A CASE is multiway branch structure that tests a register, variable, or expression for a particular values or a range of values. The general form is as follows: CASE expression value_1: statements_1 value_2: statements_2 . value_n: statements_n END CASE. In this structure, expression is tested; if its value is a member of the set values_i, then statements_i are executed. We assume that sets values-1,…., values_n are disjoint.

Example: CASE If AX contains a negative number, put -1 in BX; if AX contains 0, put 0 in BX; if AX contains a positive number, put 1 in BX. Sol: CASE AX < 0: put -1 in BX = 0: put 0 in BX > 0: put 1 in BX END CASE

It can be coded as follows: ; case AX CMP AX, 0 ; test AX JL NEGATIVE ; AX < 0 JE ZERO ; AX = 0 JG POSITIVE ; AX > 0 NEGATIVE: MOV BX, -1 ; put -1 in BX JMP END_CASE ; and exit ZERO: MOV BX, 0 ; put 0 in BX POSITIVE: MOV BX, 1 ; put 1 in BX END_CASE: Note: only one CMP is needed, because jump instruction do not affect the flags.

Exercise: Sketch the flowchart for IF-THEN, IF-THEN-ELSE and CASE structure. Sketch the flowchart for each previous example. Prob: If AL contains 1 or 3, display “o”; if AL contains 2 or 4, display “e”. Write the high level language pseudocode and code in Assembly language.