Jump & Loop instructions

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Introduction to C Programming
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.
Computer Organization & Assembly Language
LAB Flow Control Instructions
Introduction to Computer Engineering by Richard E. Haskell Branching Instructions Module M17.2 Section 11.1.
Assembly Language for Intel-Based Computers
Flow Control Instructions
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
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.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7)
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.
Lecture 4 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Overview of Assembly Language Chapter 4 S. Dandamudi.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
(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.
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.
10H Interrupt. Option 0H – Sets video mode. Registers used: – AH = 0H – AL = Video Mode. 3H - CGA Color text of 80X25 7H - Monochrome text of 80X25 Ex:
ACOE251Sofware Constructs1 Software Constructs What they are …..
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
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.
Assembly Language Programming Petra University Dr. Hadi Hassan Conditional Processing.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7) By Dr. Syed Noman.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
Stack Operations Dr. Hadi AL Saadi.
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Microprocessor and Assembly Language
Lecture 4 Control Flow Structures (LOOPS)
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Calculator in assembly language
(The Stack and Procedures)
CÁC LỆNH HỢP NGỮ.
Symbolic Instruction and Addressing
Computer Organization and Assembly Language
فصل پنجم انشعاب و حلقه.
Introduction to Programming
Computer Science 210 Computer Organization
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Chương 4: Lập trình hợp ngữ
(The Stack and Procedures)
Shift & Rotate Instructions)
3 Control Statements:.
Program Logic and Control
Program Logic and Control
Symbolic Instruction and Addressing
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
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
(The Stack and Procedures)
Chapter 7 –Program Logic and Control
Chapter 7 –Program Logic and Control
Part VI Looping Structures
Presentation transcript:

Jump & Loop instructions Lecture 4 Jump & Loop instructions

Chapter Outline Jump Instruction IF-THEN-ELSE Loop instruction For Loop While Loop REPEAT Loop

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

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

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

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

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:

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

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:

Loop Instruction The LOOP instruction can be used to implement a for loop. Syntax: The counter for the loop is the register CX, which is initialized to loop_count. Execution of the LOOP instruction causes CX to be decremented automatically. If (CX < > 0) control transfers to destination_label else the next instruction after LOOP is done. SHORT address LOOP

Loop Instruction Using the instruction LOOP, a FOR loop can be implemented as follows: ; initialize CX to loop_count TOP: ; body of the loop LOOP TOP In for loop the loop statements are repeated a known number of times.

FOR Loop Example: Write some code to display a row of 80 stars. Solution: Pseudocode: FOR 80 times DO display '*' END_IF ; what if CX =0? MOV CX, 80 MOV AH, 2 MOV DL, '*' JCXZ SKIP ;jump if CX=0 TOP: INT 21h LOOP TOP SKIP: It can be coded as follows: MOV CX, 80 MOV AH, 2 MOV DL, '*' TOP: INT 21h LOOP TOP

WHILE Loop This loop depends on a condition. The condition is checked at the top; if true the statements are executed; if false program goes into whatever follow. Pseudocode: WHILE condition DO statements END_WHILE

WHILE Loop Example: Write some code to count the number of characters in an input line. Solution: Pseudocode: initialize count to 0 read a character WHILE character <> carriage_return DO count = count + 1 read character END_WHILE continue

WHILE Loop It can be coded as follows: END_WHILE: MOV DX, 0 ; DX counts characters MOV AH, 1 ; prepare to read INT 21h ; character in AL CMP AL, 0Dh ; CR? JE END_WHILE ; yes, exit INC DX ; not CR, increment count INT 21h ; read a character JMP WHILE_ ; loop back

REPEAT Loop This loop depends on a condition. the statements are executed then the condition is checked. If true the loop terminates; if false, control branches to the top of the loop. Pseudocode: REPEAT Statements UNTIL conditions

REPEAT Loop Example: write code to read characters until a blank is read Pseudocode: REPEAT Read character UNTIL character is blank The code is: MOV AH,1 REAPEAT: INT 21H CMP AL,’ ‘ JNE REAPEAT

WHILE Versus REPEAT Use of a WHILE loop or a REPEAT loop is a matter of personal preference. A WHILE loop can be bypasses if the terminating condition is initially false. (a REPEAT loop must be done at least once) The code for a REPEAT loop is likely to be a little shorter because there is only one jump. (WHILE loops has two jumps)