High-level language structures

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.
Assembly Programming Notes for Practical 3 Munaf Sheikh
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
LAB Flow Control Instructions
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
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.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
(Flow Control Instructions)
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
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
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.
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.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7) By Dr. Syed Noman.
Stack Operations Dr. Hadi AL Saadi.
Assembly Language for Intel-Based Computers, 5th Edition
Computer Architecture CST 250
Introduction to assembly programmıng language
Instruksi Set Prosesor 8088
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Ữ.
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Computer Organization and Assembly Language
فصل پنجم انشعاب و حلقه.
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
Microprocessor Lab CSL1543 0:0:2
Program Logic and Control
Symbolic Instruction and Addressing
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
X86 Assembly Review.
EECE.3170 Microprocessor Systems Design I
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 7 –Program Logic and Control
Computer Architecture and Assembly Language
Part VI Looping Structures
Presentation transcript:

High-level language structures 23/04/2019 CAP221

Branching structures IF-THEN IF condition is true THEN execute true-branch statements End_if 23/04/2019 CAP221

Branching structures IF-THEN condition false true True branch statements 23/04/2019 CAP221

example Replace the number in AX by its absolute value. Pseudo code algorithm is IF AX <0 Then replace AX by –AX END_IF 23/04/2019 CAP221

example Code: CMP AX,0 ;AX<0? JNL END_IF ;no, exit , JGE NEG AX ;yes, change sign END_IF: 23/04/2019 CAP221

IF_THEN_ELSE IF condition is true THEN execute true-branch statements execute FALSE-branch statements End_IF 23/04/2019 CAP221

IF_THEN_ELSE FALSE TRUE CONDITION FALSE BRANCH TRUE BRANCH STATEMENTS 23/04/2019 CAP221

EXAMPLE Suppose AL and BL contains extended ASCII characters. Display the one that comes first in the character sequence. 23/04/2019 CAP221

example Pseudo code algorithm is IF AL < BL THEN display the char in AL ELSE display the char in BL END_IF 23/04/2019 CAP221

EXAMPLE Code: MOV AH,2 CMP AL,BL ELSE_: MOV DL,BL JA ELSE_ DISPLAY: MOV DL,AL JMP DISPLAY ELSE_: MOV DL,BL DISPLAY: INT 21H 23/04/2019 CAP221

We use the unsigned jump because we are comparing extended characters. JMP (unconditional jump) is needed to skip the false branch. 23/04/2019 CAP221

CASE A case is a multiway branch structure that tests a register, variable, or expression for particular values or range of values. The general form is as follows: case expression values_1: statements_1 values_2: statements_2 ………….. values_n: statements_n END_CASE 23/04/2019 CAP221

CASE EXPRESSION VALUE_1 VALUE_N VALUE_2 ……. STATEMENTS_1 STATEMENTS_2 STATEMENTS_N 23/04/2019 CAP221

EXAMPLE IF AX contains a negative number, put -1 in BX; if AX contains 0, put 0 in BX; if AX contains a positive number, put 1 in BX. 23/04/2019 CAP221

example Pseudo code algorithm is Case AX < 0:put -1 in BX END_CASE 23/04/2019 CAP221

Example Code: CMP AX,0 ZERO: JL NEGATIVE MOV BX,0 JE ZERO JMP END_CASE JG POSITIVE NEGATIVE: MOV BX,-1 JMP END_CASE ZERO: MOV BX,0 JMP END_CASE POSITIVE: MOV BX,1 END_CASE NOTE: only one CMP is needed, because jump instructions do not affect the flags. 23/04/2019 CAP221

example If AL contains 1 or 3, display “o”; if AL contains 2 or 4, display “e”. Pseudo code algorithm is Case AL 1, 3: display “o” 2, 4: display “e” End_case 23/04/2019 CAP221

example Code: CMP AL,1 JE ODD ODD: CMP AL,3 MOV DL, ’o’ JMP DIS_ JE EVEN CMP AL,4 JE EVEN JMP END_ ODD: MOV DL, ’o’ JMP DIS_ EVEN: MOV DL, ’e’ DIS_: MOV AH,2 INT 21H END_: 23/04/2019 CAP221

Branches with compound conditions Sometimes the branching condition in an IF or CASE takes the form Condition_1 and condition_2 Condition_1 or condition_2 23/04/2019 CAP221

AND conditions An AND condition is true if and only if condition_1 and condition_2 are both true. Likewise, if either condition is false, then the whole thing is false. 23/04/2019 CAP221

Example Read a character, and if it’s an uppercase letter, display it. Pseudo code algorithm is read a character (into AL) If (‘A’ <= character) and (character <=‘Z’) Then Display character End_if 23/04/2019 CAP221

example Code: MOV ah,1 INT 21h MOV DL,AL CMP al,’A’ MOV AH,2 JL end_if CMP al,’Z’ JG end_if MOV DL,AL MOV AH,2 INT 21H End_if: 23/04/2019 CAP221

OR conditions condition_1 OR condition_2 is true if at least one of the conditions is true. if both conditions are false, then the whole thing is false. 23/04/2019 CAP221

example Read a character. If it’s “y” or “Y”, display it; otherwise, terminate the program. Pseudo code algorithm is Read a character (into AL) if (character = “y” or character= “Y”) Then Display character Else Terminate the program End_if 23/04/2019 CAP221

example THEN: MOV AH,1 MOV AH,2 INT 21h MOV DL,AL CMP AL, ’y’ INT 21H JMP END_IF ELSE_: MOV AH,4CH END_IF: MOV AH,1 INT 21h CMP AL, ’y’ JE THEN CMP AL,’Y’ JMP ELSE_ 23/04/2019 CAP221

Looping structures A loop is a sequence of instructions that are repeated. For loop: this is a loop structure in which the loop statements are repeated a known number of times (a count-controlled loop). 23/04/2019 CAP221

For loop_count times do statements end_for Initialize count statements For loop_count times do statements end_for Count = count - 1 Count=0 False True 23/04/2019 CAP221

LOOP instruction The LOOP instruction can be used to implement a FOR loop. General form: LOOP des_label The counter for the loop is the register CX. 23/04/2019 CAP221

LOOP instruction CX is initialized to loop_count. Execution of the LOOP instruction causes CX to be decremented automatically. If CX is not 0, control transfers to des_label. If CX =0, the next instruction after LOOP is done. Des_label must precede the LOOP instruction by no more than 126 bytes. 23/04/2019 CAP221

LOOP instruction So, a FOR loop can be implemented as follows. ; initialize CX to loop_count Des_label: ; body of the loop loop des_label 23/04/2019 CAP221

example Write a count-controlled loop to display a row of 80 stars. Pseudo code algorithm is For 80 times do display ‘*’ end_for 23/04/2019 CAP221

example MOV CX,80 MOV AH,2 MOV DL,’*’ TOP: INT 21H LOOP TOP 23/04/2019 CAP221

LOOP instruction A loop instruction is executed at least once. If CX = 0 when the loop is entered, the loop instruction causes CX to be decremented to FFFFH. So, the loop is executed 65535 more times. 23/04/2019 CAP221

LOOP instruction Solution: use the instruction JCXZ (jump if CX is zero) before the loop. JCXZ des_label 23/04/2019 CAP221

LOOP instruction A loop implemented as follows is bypassed if CX is 0. JCXZ skip top: ;body of the loop loop top Skip: 23/04/2019 CAP221

While Loop This loop depends on a condition. Pseudo code while condition do statements end_while 23/04/2019 CAP221

While Loop condition True statements False 23/04/2019 CAP221

example Write some code to count the number of characters in an input line. Pseudo code initialize count to 0. read a character while character <> carriage_return do count=count + 1 end_while 23/04/2019 CAP221

example MOV DX,0 ; or bx, cx, ax?? MOV AH,1 INT 21H WHILE_: CMP AL,0DH JE END_ INC DX JMP WHILE_ END_ : 23/04/2019 CAP221

Repeat loop Another conditional loop is the repeat loop. Pseudo code statements until condition 23/04/2019 CAP221

Repeat loop statements condition False True 23/04/2019 CAP221

example Write some code to read characters until a blank is read. Pseudo code Repeat read a character until character is a blank 23/04/2019 CAP221

example MOV AH,1 REPEAT: INT 21H CMP AL,’ ’ JNE REPEAT 23/04/2019 CAP221

WHILE versus REPEAT In a while loop, the loop can be bypassed if the terminating condition is initially false. The statements in a repeat must be done at least once. Code for a repeat loop is likely to be shorter because there is only a conditional jump at the end, but a while loop has two jumps: a conditional jump at the top and a jmp at the bottom. 23/04/2019 CAP221

Programming with high-level structures Top-down program design: The original problem is solved by solving a series of sub problems, each of which is easier to solve than the original problem. 23/04/2019 CAP221

example 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 letters are entered, display “No capital letter”. sample execution: type a line of text: THE QUICK BROWN FOX JUMPED. first capital = B last capital = X 23/04/2019 CAP221

Top down program design First refinement: Display the opening message. Read and process a line of text. Display the results. 23/04/2019 CAP221

example TITLE PGM6_2 : FIRST LAST CAPITALS .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 '@ $' 23/04/2019 CAP221

Step 1 .CODE MAIN PROC ; initialize DS MOV AX,@DATA MOV DS, AX ; first step display opening message MOV AH,9 LEA DX,PROMPT INT 21H 23/04/2019 CAP221

Step 2: read and process a line of text Read a character. While character is not a carriage return do If character is a capital letter Then If character precedes first capital First capital = character End_if If character follows last capital Last capital = character Read a character End_while 23/04/2019 CAP221

;read and process a line of text MOV AH,1 INT 21H WHILE_: ; while character is not carriage ;return do CMP AL,0dH JE END_WHILE ; if character is a capital letter CMP AL,'A' JNGE END_IF CMP AL,'Z' JNLE END_IF ; then ; if character precedes FIRST ;capital CMP AL,FIRST JNL CHECK_LAST ; then FIRST CAPITAL = ;character MOV FIRST,AL ; end_if CHECK_LAST : ; if character follows last capital CMP AL,LAST JNG END_IF ; then LAST CAPITAL = character MOV LAST,AL ;end_if END_IF: ; read a character INT 21H JMP WHILE_ END_WHILE : A=65, Z=90, s91=[, @=64 23/04/2019 CAP221

Step 3: display the results if no capitals were typed then display “no capitals” Else display first capital and last capital End_if 23/04/2019 CAP221

; if no capital were typed CMP FIRST, '[' JNE CAPS ; then ; display results MOV AH,9 ; if no capital were typed CMP FIRST, '[' JNE CAPS ; then LEA DX,NOCAP_MSG JMP DISPLAY CAPS : LEA DX,CAP_MSG DISPLAY : INT 21H ;end_if ; DOS exit MOV AH,4CH MAIN ENDP END MAIN 23/04/2019 CAP221