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.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
LAB Flow Control Instructions
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
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.
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.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Arithmetic Flags and Instructions
(Flow Control Instructions)
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
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.
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.
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.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Assembly Language Wei Gao. Assembler language Instructions.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA 1 Assembly Language Programming.
Computer Architecture CST 250
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
Lecture 4 Control Flow Structures (LOOPS)
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly IA-32.
Assembly Language Lab (4).
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
Calculator in assembly language
Arithmetic Instructions
Symbolic Instruction and Addressing
CS 301 Fall 2002 Assembly Instructions
Computer Organization and Assembly Language
فصل پنجم انشعاب و حلقه.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(The Stack and Procedures)
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Symbolic Instruction and Addressing
Flow Control Instructions
X86 Assembly Review.
EECE.3170 Microprocessor Systems Design I
High-level language structures
UNIT-II Assembly Language Programs Involving Logical
Chapter 6 –Symbolic Instruction and Addressing
Assembly Language for Intel 8086 Jump Condition
Jump & Loop instructions
Chapter 7 –Program Logic and Control
Carnegie Mellon Ithaca College
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Part VI Looping Structures
Presentation transcript:

Flow Control Instructions ICS312 SET 8 Flow Control Instructions

Flow Control Instructions Flow control, or branching, instructions for jumps, loops and conditional jumps allow us to control the execution of program statements and to build high-level programming structures such as if-else statements, case statements, and loops in assembler programs

Jump Instructions (1) The unconditional jump instruction, branches to a new address in the program code.     JMP NEXT        ; jumps to address of "NEXT" Conditional jump instructions test the condition of one or two FLAGS in the flags register, then "jump" to a new location in the program based on the result of the test. Example: TOP: SUB AX, BX ;AX = AX - BX JC EXIT: ;IF AX < BX then jump to EXIT JMP TOP ;else, repeat until AX < BX EXIT: MOV AX, 4C00H INT 21H

Jump Instructions (2) CMP Instruction The CMP instruction sets the flags the same as if a subtraction had been performed, but the destination operand is NOT changed. Example (simulating the LOOP instruction): MOV CX, 5 TOP_2: CMP CX, 0 ; CX = 0 ? JE EXIT_: ; IF CX = 0, then EXIT_ SUB AX, BX ; AX = AX - BX DEC CX JMP TOP_2 ;else, repeat until CX = 0 EXIT_: MOV AX, 4C00H INT 21H Note: the above code simulates the effect of the LOOP instruction

Signed versus Unsigned Conditional Jumps Note:In this course we will only make use of signed conditional jumps. Unsigned Conditional Jumps:  JA and JB Signed Conditional Jumps: JG or JL Example 1. Will the JG instruction in the following code result in a jump? MOV AL, 80h MOV BL, 7FH CMP AL, BL JG EXIT_ ... Example 2. Will the JA instruction in the following code result in a jump? JA EXIT_

Examples of Flow Control Using Jump Instructions (1) IF-THEN-ELSE Structures START: MOV AH, 1 INT 21H ; input AL CMP AL, 'Q' ; if AL = 'Q' JE EXIT ; then, branch to EXIT MOV AH, 2 ; else output AL MOV DL, AL INT 21H JMP START ; repeat EXIT: MOV AX, 4C00H

Examples of Flow Control Using Jump Instructions (2) IF-THEN-ELSE If AL ≤ BL, display BL else display AL CMP AL, BL ; AL ≤ BL? JG ELSE_ ; no, go to "else" MOV DL, BL ; display BL JMP DISPLAY ELSE_: ; else, AL > BL MOV DL, AL ; display AL DISPLAY: ; display result MOV AH, 2 INT 21H

Examples of Flow Control Using Jump Instructions (3) CASE Structure MOV AH, 1 INT 21H CMP AL, 'A' JE CASE_A CMP AL, 'B' JE CASE_B CMP AL, 'C' JE CASE_C CMP AL, 'D' JE CASE_D CASE_A: ; code for case A goes here JMP NEXT_ CASE_B: ; code for case B goes here CASE_C: ; code for case C goes here CASE_D: ; code for case D goes here NEXT_: ; code continues here

Examples of Flow Control Using Jump Instructions (4) AND Conditions: Is AL >= 30H and <= 39H? I.e. is it the code for a decimal digit? MOV AH, 1 INT 21H CMP AL, 30H ; AL >= 30h? JL ERROR ; no, error CMP AL, 39H ; AL <= 39h? JG ERROR ; no, error ... ; continue: AL >= 30h and AL <= 39h ERROR: ... ; code for handling an error

Examples of Flow Control Using Jump Instructions (5) OR Conditions Input until the character is Q or q. START: MOV AH, 1 INT 21H CMP AL, 'Q' JE EXIT_ ; if AL = 'Q', then exit CMP AL, 'q' JNE START ; if AL != 'Q' and AL != 'q', repeat input EXIT_: ; if AL = 'Q' or AL = 'q' MOV AX, 4COOH

Examples of Flow Control Using Jump Instructions (6) FOR Loops Input the value of AL. For I = 1 to 10 output this value+I. START: MOV AH, 1 INT 21H MOV CX, 10 TOP_: JCXZ EXIT_ ; If CX = 0, exit MOV DL, AL MOV AH, 2 INC AL LOOP TOP_ ; repeats for 10 times EXIT_: MOV AX, 4C00H

Examples of Flow Control Using Jump Instructions (7) WHILE Loops input Al while AL != ‘Q’ output AL end while START: MOV AH, 1 INT 21H CMP AL, 'Q' JE EXIT_ ; repeat while AL != 'Q' MOV DL, AL MOV AH, 2 JMP start EXIT_: MOV AX, 4C00H

Examples of Flow Control Using Jump Instructions (8) REPEAT-UNTIL Loops Set Dl to ‘A’ TOP: output DL add 1 to DL repeat top until DL = ‘[‘ (ascii code 5Bh) MOV AH, 2 MOV DL, 'A' TOP_: INT 21H INC DL CMP DL, '[' JNE top_ ; repeat until DL = '[' EXIT_: MOV AX, 4C00H

Loop Instruction Used to implement "for loops" more conveniently than above. Decrements CX register and jumps to specified label as long as CX != 0 after being decremented. Example: Input a number, and display that number of asterisks .586 ; required for MOVSX or MOVZX instructions .CODE START: MOV DL, '*" MOV AH, 1 INT 21H SUB AL, 30H MOVSX CX, AL ; sign extend into CX register MOV AH, 2 TOP_: INT 21H ; display asterisks LOOP TOP_ ; repeats CX times EXIT_: MOV AX, 4C00H

MOVSX Instruction Move with sign extension. Copies a byte or word from a source operand to a register, and sign extends into the upper half of the destination. This is used to copy an 8-bit or 16-bit operand into a larger destination.

JMP Instructions For signed arithmetic J[N] {G | L | Z } [E] eg: JG, JGE, JNZ, JLE For unsigned arithmetic (not used in this course) J[N] {A | B | Z } [E] eg: JA, JAE, JNZ, JBE

Example on JE MOV BL, 0 REPEAT: MOV AH, 1 INT 21H CMP AL, 0DH JE OUT Count the number of characters entered which terminate in a CR (0DH), don’t count CR. Use BL to store that number. MOV BL, 0 REPEAT: MOV AH, 1 INT 21H CMP AL, 0DH JE OUT INC BL JMP REPEAT OUT: …

Example on JGE To put the absolute value of AX into AX. CMP AX, 0 JGE OUT ; jump if greater than or equal NEG AX OUT: …

Example on JE & JL If AX < 0, output – ; If AX = 0, output 0; If AX > 0, output + CMP AX, 0 JE zerocase JL lesscase MOV DL, ‘+’ JMP display zerocase: MOV DL, 0 lesscase: MOV DL, ‘-’ display: MOV AH, 2 INT 21H

Example on JCXZ JCXZ – Jump if cx is zero, cx used as a counter Assume that CX contains some number and you want to Output this number of asterisks JCXZ OUT place: MOV AH, 2 MOV DL, ‘*’ INT 21H LOOP place OUT: …

Textbook Reading (Jones): Sections 5.1-5.4 Homework: Ex 5.1, p. 109, nos. 1-4 Ex 5.3, p. 115, no. 1