Part VI Looping Structures

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

BINARY & HEX I/O. Binary input : read in a binary number from keyboard, followed by a carriage return. character strings of 1’s & 0’ we need to convert.
Flow of Control Instruction/Control structure Looping structure Looping structure Branching structure Branching structure For assembly language program.
DOS and BIOS Interrupts DOS and BIOS interrupts are used to perform some very useful functions, such as displaying data to the monitor, reading data from.
Assembly Programming Notes for Practical 3 Munaf Sheikh
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.
Flow Control Instructions
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
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.
BIOS and DOS Programming in DOS INT 10 and 21H. Interrupts There are some extremely useful subroutines within BIOS or DOS that are available to the user.
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.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Overview of Assembly Language Chapter 4 S. Dandamudi.
(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 …..
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.
Control Structure vs. Assembly Language NASM. If-then-else If conditional then then_actions jump to endif else else_actions endif.
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.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Assembly Language Programming Petra University Dr. Hadi Hassan Conditional Processing.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7) By Dr. Syed Noman.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
Stack Operations Dr. Hadi AL Saadi.
Decision Making.
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
(The Stack and Procedures)
CÁC LỆNH HỢP NGỮ.
Symbolic Instruction and Addressing
Control Structure Senior Lecturer
فصل پنجم انشعاب و حلقه.
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)
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
Some Assembly (Part 2) set.html.
(The Stack and Procedures)
Jump & Loop instructions
Chapter 7 –Program Logic and Control
Chapter 7 –Program Logic and Control
Part I Data Representation and 8086 Microprocessors
Part IV The FLAGS Register
Presentation transcript:

Part VI Looping Structures Assembly Language Part VI Looping Structures Department of Computer Science, Faculty of Science, Chiang Mai University

204231: Computer Organization and Architecture Outline Branches with Compound Conditions Looping Structures Programming with High-Level Structures 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture AND Conditions condition_1 AND condition_2 An AND condition is true if and only if condition_1 and condition_2 are both true. If either condition is false, then the whole thing is false. 204231: Computer Organization and Architecture

Read a character, and if it’s an uppercase letter, display it. Read a character (into AL) IF (‘A’ <= character) and (character <= ‘Z’) THEN display character END_IF 204231: Computer Organization and Architecture

Read a character, and if it’s an uppercase letter, display it. ; read a character MOV AH, 1 ; prepare to read INT 21H ; char in AL ; if (‘A’ <= char) and (char <= ‘Z’) CMP AL, ‘A’ ; char >= ‘A’? JNGE END_IF ; no, exit CMP AL, ‘Z’ ; char <= ‘Z’? JNLE END_IF ; no, exit ; then display char MOV DL, AL ; get char MOV AH, 2 ; prepare to display INT 21H ; display char END_IF: 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture OR Conditions condition_1 OR condition_2 condition_1 OR condition_2 is true if at least one of the conditions is true. It is only false when both conditions are false. 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture Read a character, and if it is “y” or “Y”, display it; otherwise, terminate the program. Read a character (into AL) IF (character = ‘y’) or (character = ‘Y’) THEN display it ELSE terminate the program END_IF 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture Read a character, and if it is “y” or “Y”, display it; otherwise, terminate the program. ; read a character MOV AH, 1 ; prepare to read INT 21H ; char in AL ; if (character = ‘y’) or (character = ‘Y’) CMP AL, ‘y’ ; char = ‘y’? JE THEN ; yes, go to display it CMP AL, ‘Y’ ; char = ‘Y’? JMP ELSE_ ; no, terminate 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture Read a character, and if it is “y” or “Y”, display it; otherwise, terminate the program. THEN: MOV AH, 2 ; prepare to display MOV DL, AL ; get char INT 21H ; display it JMP END_IF ; end exit ELSE_: MOV AH, 4CH INT 21H ; DOS exit END_IF: 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture FOR LOOP FOR loop_count times DO statements END_FOR 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture The LOOP instruction ; initialize CX to loop_count TOP: ; body of the loop LOOP TOP 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture The LOOP instruction LOOP destination_label 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. If CX = 0, the next instruction after LOOP is done. destination_label must precede the LOOP instruction by no more than 126 bytes. 204231: Computer Organization and Architecture

Write a count-controlled loop to display a row of 80 stars. FOR 80 times DO display ‘*’ END_FOR MOV CX, 80 ; number of stars to display MOV AH, 2 ; display character function MOV DL, ‘*’ ; character to display TOP: INT 21h ; display a star LOOP TOP ; repeat 80 times 204231: Computer Organization and Architecture

The Instruction JCXZ (Jump If CX Is zero) JCXZ destination_label If CX contains 0 when the loop is entered, the LOOP instruction causes CX to be decremented to FFFFh, and the loop is then executed FFFFh = 65535 more times! To prevent this, the instruction JCXZ may be used before the loop. 204231: Computer Organization and Architecture

The Instruction JCXZ (Jump If CX Is zero) JCXZ SKIP TOP: ; body of the loop LOOP TOP SKIP: 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture WHILE LOOP WHILE condition DO statements END_WHILE The condition is checked at the top of the loop. If true, the statements are executed; if false, the program goes on to whatever follows. 204231: Computer Organization and Architecture

Write some code to count the number of characters in an input line. Initialize count to 0 read a character WHILE character <> carriage_return DO count = count + 1 END_WHILE 204231: Computer Organization and Architecture

Write some code to count the number of characters in an input line. MOV DX, 0 ; DX counts characters MOV AH, 1 ; prepare to read INT 21H ; character in AL WHILE_: CMP AL, 0DH ; CR? JE END_WHILE ; yes, exit INC DX ; not CR, increment count INT 21H ; read a character JMP WHILE_ ; loop back END_WHILE_: 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture REPEAT LOOP REPEAT statements UNTIL condition In a REPEAT … UNTIL loop, the statements are executed, and then the conditions is checked. If true, the loop terminates; if false, control branched to the top of the loop. 204231: Computer Organization and Architecture

Write some code to read characters until a blank is read. REPEAT read a character UNTIL character is a blank MOV AH, 1 ; prepare to read REPEAT: INT 21H ; char in AL ; until CMP AL, ‘ ‘ ; a blank? JNE REPEAT ; no, keep reading 204231: Computer Organization and Architecture

Programming with High-Level Structures Type a line of text: THE QUICK BROWN FOR JUMPED. First capital = B Last capital = W 204231: Computer Organization and Architecture

Read and Process a Line of Text Read a character WHILE character is not a carriage return DO IF character is a capital letter (‘A’ <= character AND character <= ‘Z’) THEN IF character precedes first capital THEN first capital = character END IF IF character follows last capital THEN last capital = character END_WHILE 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture Display the rResults IF no capitals were typed, THEN display “No capitals” ELSE display first capital and last capital END_IF 204231: Computer Organization and Architecture

204231: Computer Organization and Architecture Reference Ytha Yu and Charles Marut, Assembly Language Programming and Organization of the IBM PC. New York: McGraw-Hill, 1992. 204231: Computer Organization and Architecture