Lecture 4 Control Flow Structures (LOOPS)

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.
ICS312 Set 6 Operands. Basic Operand Types (1) Register Operands. An operand that refers to a register. MOV AX, BX ; moves contents of register BX to.
Computer Organization & Assembly Language
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
Lecture 2 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Flow Control Instructions
Topic – string – Ch. 11 [Marut] Ch. 4 [Brey] String Data Transfer Instructions – The Direction Flag – LODS Instructions – STOS Instructions – MOVS Instructions.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
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.
Lecture 5 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
Lecture 14 Basic I/O Interface Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
(Flow Control Instructions)
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#9) By Dr. Syed Noman.
Video systems. Lesson plan Review the code for the previous exercise Video systems Review for midterm exam.
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.
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.
Lecture 6 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
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.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7) By Dr. Syed Noman.
Program Structure Example for Data Segment CRLF EQU 0DH, 0AH PROMPT DB 'Enter a digit between 0 and 9', 0 VAR1 DB ? ARRAY DW 1234h, 23h, 0FF54h.
Format of Assembly language
Presentation on Real Mode Memory Addressing
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly Language Programming Part 2
Microprocessor and Assembly Language
(The Stack and Procedures)
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Introduction to Assembly Language
فصل پنجم انشعاب و حلقه.
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Chương 4: Lập trình hợp ngữ
(The Stack and Procedures)
Shift & Rotate Instructions)
Program Logic and Control
Microprocessor Lab CSL1543 0:0:2
Program Logic and Control
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
X86 Assembly Review.
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)
Jump & Loop instructions
Chapter 7 –Program Logic and Control
By Nasser Halasa Assembly Language.
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
(Array and Addressing Modes)
Part VI Looping Structures
Presentation transcript:

Lecture 4 Control Flow Structures (LOOPS) Presented By Dr. Rajesh Palit Asst. Professor, EECS, NSU Originally Prepared By Dr. Shazzad Hosain, EECS, NSU

Agenda Control Flow Structures FOR Loop WHILE Loop REPEAT-UNTIL Loop Load Effective Address (LEA) Instruction Programming with Higher Level Structures

FOR Loop Write a program to display a row of 80 stars ‘*’ FOR 80 times DO display ‘*’ END_FOR MOV CX, 80 ; number of ‘*’ to display MOV AH, 2 ; char display function MOV DL, ‘*’ ; char to display TOP: INT 21h ; display a star LOOP TOP ; repeat 80 times Example 6-8: Assembly Language Programming

WHILE Loop Write a program to count the characters in an input line Initialize count to 0 Read a character WHILE character <> carriage_return DO count = count + 1 read a character END_WHILE WHILE condition DO statements END_WHILE MOV DX, 0 ; DX counts the characters MOV AH, 1 ; read char function INT 21h ; read a char in AL WHILE_: CMP AL, 0DH ; CR? JE END_WHILE INC DX INT 21h JMP WHILE_ END_WHILE: Example 6-9: Assembly Language Programming

REPEAT Loop REPEAT statements UNTIL condition Write a program to read characters until a blank/space is read REPEAT read a character UNTIL character is a blank MOV AH, 1 ; read char function REPEAT: INT 21h ; read a char in AL CMP AL, ‘ ‘ ; a blank? JNE REPEAT ; no, keep reading Example 6-10: Assembly Language Programming

Road Map Control Flow Structures IF-THEN IF-THEN-ELSE CASE FOR Loop WHILE Loop REPEAT-UNTIL Loop Load Effective Address (LEA) Instruction Programming with Higher Level Structures

Load Effective Address The LEA instruction loads any 16 bit register with the data address as determined LEA vs. MOV

Load Effective Address Example Write a program to exchange the contents of two memory locations Example 4-3: Intel Microprocessors – by Brey

LEA vs. OFFSET Directive OFFSET functions only with simple operands such as LIST. LEA functions with complex operands such as [DI], LIST [SI] etc. OFFSET is more efficient than LEA LEA BX, LIST is costly than MOV BX, OFFSET LIST

Example Write a program to print “Hello World” .MODEL SMALL .DATA PROMPT DB ‘Hello world’, 0DH, 0AH, ‘$’ .CODE .STARTUP ; initialize DS MOV AX, @DATA MOV DS, AX ; display opening message MOV AH, 9 ; display string function LEA DX, PROMPT ; get opening message INT 21h ; display it .EXIT END

Road Map Control Flow Structures IF-THEN IF-THEN-ELSE CASE FOR Loop WHILE Loop REPEAT-UNTIL Loop Load Effective Address (LEA) Instruction Programming with Higher Level Structures

Programming with High Level Structures Problem Prompt the user to enter a line of text. On the next line, display the capital letter entered that comes first alphabetically and the one that comes last. If no capital entered, display “No capital letters”. Type a line of text: THE QUICK BROWN FOX JUMPED First capital = B Last capital = X

Top-down Program Design Divide the problem into sub-problems Display the opening message Read and process a line of text Display the results

Start the Program Type a line of text: THE QUICK BROWN FOX JUMPED First capital = B Last capital = X .MODEL SMALL .STACK 100H .DATA PROMPT DB ‘Type a line of text’, 0DH, 0AH, ‘$’ NOCAP_MSG DB 0DH, 0AH, ‘No capitals $’ CAP_MSG DB 0DH, 0AH, ‘First capital = ‘ FIRST DB ‘]’ DB ‘ Last capital = ‘ LAST DB ‘@ $’ .CODE .STARTUP Follows ‘Z’ in ASCII sequence Precedes ‘A’ in ASCII sequence @ABCDE………………………………..XYZ] FIRST LAST

Step 1. Display the opening message .DATA PROMPT DB ‘Type a line of text’, 0DH, 0AH, ‘$’ ; initialize DS MOV AX, @DATA MOV DS, AX ; display opening message MOV AH, 9 ; display string function LEA DX, PROMPT ; get opening message INT 21h ; display it

Step 2: Read and Process a Line of Text Read a character WHILE character is not carriage return DO IF character is a capital letter (*) THEN IF character precedes first capital THEN first capital = character END_IF IF character follows last capital THEN last capital = character END_WHILE Line (*) is actually an AND condition: IF (‘A’ <= character) AND (character <= ‘Z’)

Step 2: Read and Process a Line of Text Read a character WHILE character is not carriage return DO IF character is a capital letter (*) THEN IF character precedes first capital THEN first capital = character END_IF IF character follows last capital THEN last capital = character END_WHILE MOV AH, 1 INT 21h WHILE_: CMP AL, 0DH JE END_WHILE CMP AL, ‘A’ JNGE END_IF CMP AL, ‘Z’ JNLE END_IF CMP AL, FIRST ; char < FIRST or ‘]’ JNL CHECK_LAST MOV FIRST, AL CHECK_LAST: CMP AL, LAST ; char > LAST or ‘@’ JNG END_IF MOV LAST, AL END_IF: INT 21H JMP WHILE_ END_WHILE: Line (*) is actually an AND condition: IF (‘A’ <= character) AND (character <= ‘Z’) @ABCDE………………………………..XYZ] FIRST LAST

Step 3: Display The Results IF no capitals were typed THEN display “no capitals” ELSE display first capital and last capital END_ID MOV AH, 9 ; display string function CMP FIRST, ‘]’ JNE CAPS ; no, display results LEA DX, NOCAP_MSG JMP DISPLAY CAPS: LEA DX, CAP_MSG DISPLAY: INT 21H .EXIT END @ABCDE………………………………..XYZ] FIRST LAST

References Ch 6, Assembly Language Programming – by Charles Marut Section 4-3 of Intel Microprocessors – by Brey