(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.
Computer Organization & Assembly Language
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
LAB Flow Control Instructions
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
Flow Control Instructions
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Prof. Muhammad Saeed III. 1/27/2015Computer Architecture & Assembly Language2 Assembly Language Instructions.
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.
LAB Flag Bits and Register
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.
CE302 MICROPROCESSORS Levent EREN Izmir University of Economics.
Lecture 4 ( Assembly Language).
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
Arithmetic Flags and Instructions
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
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 …..
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
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
Microprocessor MA Rahim Khan Computer Engineering and Networks Department.
Introduction to Computer Organization and Assembly Language
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 Programming Petra University Dr. Hadi Hassan Conditional Processing.
Data Transfers, Addressing, and Arithmetic
Microprocessor and Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly IA-32.
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
CÁC LỆNH HỢP NGỮ.
Introduction to Assembly Language
Lecture 4 ( Assembly Language).
Flags Register & Jump Instruction
Computer Science 210 Computer Organization
תכנות בסיסי בשפת סף פרק 5 מצגת 3.
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Shift & Rotate Instructions)
Flow Control Instructions
CNET 315 Microprocessor & Assembly Language
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
Computer Architecture and Assembly Language
Part VI Looping Structures
Part IV The FLAGS Register
Presentation transcript:

(Flow Control Instructions) Assembly Language Lecture 5 & 6 (Flow Control Instructions)

Lecture Outline Branching structures: IF-THEN IF-THEN-ELSE Unconditional Jumps Flow Control Instructions 1

Branching Structures In high-level languages, branching structures enable a program to take different paths, depending on conditions. Branching structures: IF-THEN IF-THEN-ELSE CASE High-Level Language Structures 3

Introduction Jump instructions can be used to implement branches and loops. Because the jumps are so primitive, it is difficult to code an algorithm with them without some guidelines. High-level language structures could be used as a guideline when coding in assembly language. High-Level Language Structures 2

True-branch statements IF-THEN Pseudocode: IF condition is true THEN execute true-branch statements END_IF true or false False condition True True-branch statements High-Level Language Structures 4

IF-THEN Example: replace the number in AX by its absolute value. Solution: Pseudocode: IF AX < 0 THEN replace -AX by AX END_IF It can be coded as follows: ; if AX < 0 ; then CMP AX, 0 ; AX < 0 JNL END_IF ; no, exit NEG AX ; yes, change sign END_IF: High-Level Language Structures 5

The CMP Instruction The jump condition is often provided by the CMP (compare) instruction Syntax: CMP destination, source Compares by computing destination contents minus source contents. The result is not stored, but the flags are affected. Destination may not be a constant. CMP is just like SUB, except that destination is not changed. Flow Control Instructions 10

Conditional Jumps Syntax Jxxx destination_label Example JNZ PRINT_LOOP If the condition for the jump is true, the next instruction to be executed is the one at destinaltion_label (PRINT_LOOP), 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 cindition is that the result of the previous operation is not zero. Flow Control Instructions 4

Conditional Jumps Signed Jumps: used for signed interpretations. Symbol Description Condition for Jumps JG/JNLE jump if grater than ZF = 0 & SF = OF jump if not less than or equal JGE/JNL jump if grater than or equal SF = OF jump if not less than JL/JNGE jump if less than SF <> OF jump if not greater than or equal JLE/JNG jump if less than or equal ZF = 1 or SF <> OF jump if not grater than Flow Control Instructions 7

Conditional Jumps Unsigned Jumps: used for unsigned interpretations. Symbol Description Condition for Jumps JA/JNBE jump if above CF = 0 & ZF = 0 jump if not below or equal JAE/JNB jump if above or equal CF = 0 jump if not below JB/JNAE jump if below CF = 1 jump if not above or equal JBE/JNA jump if below or equal CF = 1 or ZF = 1 jump if not above Flow Control Instructions 8

Conditional Jumps Single Flag Jumps: operates on settings of individual flags. Symbol Description Condition for Jumps JE/JZ jump if equal/ jump if equal to 0 ZF = 1 JNE/JNZ jump if not equal/ jump if not 0 ZF = 0 JC jump if carry CF = 1 JNC jump if no carry CF = 0 JO jump if overflow OF = 1 JNO jump if no overflow OF = 0 JS jump if sign negative SF = 1 JNS jump if nonnegative sign SF = 0 JP/JPE jump if parity even PF = 1 JNP/JPO jump if parity odd PF = 0 Flow Control Instructions 9

How the CPU Implements a Conditional Jump The CPU looks at the FLAGS register. If the conditions for the jump are: True: the CPU adjusts the IP to point to the destination_label, so that the instruction at this label will be done next. False: the IP is not altered; this means that the next instruction in line will be done. Flow Control Instructions 6

Unconditional Jumps - The JMP Instruction The JMP (jump) instruction causes an unconditional transfer of control (unconditional jump). Syntax: JMP destination a label in the same segment as the JMP itself Flow Control Instructions 15

Unconditional Jumps - The JMP Instruction JMP can be used to get around the range restriction of a conditional jump. Example: TOP: ; body of the loop DEC CX JNZ BOTTOM JMP EXIT BOTTOM: JMP TOP EXIT: MOV AX, BX If the loop body contains so many instructions that label top is out of range for JNZ TOP: ; body of the loop DEC CX JNZ TOP MOV AX, BX Flow Control Instructions 16

IF-THEN-ELSE Pseudocode: IF condition is true THEN execute true-branch statements ELSE execute false-branch statements END_IF False condition True False-branch statements True-branch statements High-Level Language Structures 6

IF-THEN-ELSE Example: Suppose AL and BL contain extended ASCII characters. Display the one that comes first in the character sequence. Solution: Pseudocode: IF AL <= BL THEN display the character in AL ELSE display the character in BL END_IF continue High-Level Language Structures 7

IF-THEN-ELSE It can be coded as follows: ; if AL <= BL CMP AL, BL ; AL <= BL? JNBE ELSE_ ; no, display char in BL ; AL <= BL MOV DL, AL ; move char to be displayed JMP DISPLAY ; go to display ELSE_: ; BL < AL MOV DL, BL DISPLAY: MOV AH, 2 ; prepare to display INT 21h ; display it High-Level Language Structures 8

Branches with compound Conditions Sometimes the branching condition in an IF or CASE takes the form: condition_1 AND condition_2 or condition_1 OR condition_2 where condition_1 and condition_2 are either true or false. AND condition OR condition High-Level Language Structures 14

AND Condition An AND condition is true if and only if all conditions are true. Example: Read a character, and if it’s an uppercase letter, display it. Solution: Pseudocode: Read a character (into AL) IF ('A' <= character) and (character <= 'Z') THEN display character END_IF continue High-Level Language Structures 15

AND Condition It can be coded as follows: ; read a character ; if ('A' <= char) and (char <='Z') ; then display char MOV AH,1 ; prepare to read INT 21h ; char in AL CMP AL, 'A' ; char >= 'A'? JNGE END_IF ; no, exit CMP AL, 'Z' ; char <= 'Z'? JNLE END_IF ; no, exit MOV DL, AL ; get char MOV AH, 2 ; prepare to display INT 21h ; display char END_IF: High-Level Language Structures 16

OR Condition An OR condition is true if at least one of the conditions is true. Example: Read a character. If it’s 'y' or 'Y', display it; otherwise, terminate the program. Solution: Pseudocode: Read a character (into AL) IF (character = 'y') or (character = 'Y') THEN display character ELSE terminate the program END_IF continue High-Level Language Structures 17

OR Condition It can be coded as follows: ; read a character ; if (char = 'y') or (char = 'Y') MOV AH,1 ; prepare to read INT 21h ; char in AL CMP AL, 'y' ; char = 'y'? JE THEN ; yes, go to display it CMP AL, 'Y' ; char = 'Y'? JMP ELSE_ ; no, terminate THEN: MOV DL, AL ; get char MOV AH, 2 ; prepare to display INT 21h ; display char JMP END_IF ; and exit ELSE_: MOV AH, 4Ch INT 21h ; DOS exit END_IF: High-Level Language Structures 18

Working with Characters In working with the standard ASCII character set, either signed or unsigned jumps may be used. Why? Because the sign bit of a byte containing a character code is always zero. Flow Control Instructions 13