Microprocessor and Assembly Language

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.
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
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.
Flow Control Instructions
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Conditional Processing If … then … else While … do; Repeat … until.
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.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
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.
(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.
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.
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly Language Programming Petra University Dr. Hadi Hassan Conditional Processing.
Computer Architecture CST 250
Data Transfers, Addressing, and Arithmetic
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
Microprocessor and Assembly Language
More on logical instruction and
Assembly Language Programming Part 2
Processor Processor characterized by register set (state variables)
(The Stack and Procedures)
CÁC LỆNH HỢP NGỮ.
Microprocessor and Assembly Language
Symbolic Instruction and Addressing
Introduction to Assembly Language
Computer Organization and Assembly Language
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Flags Register & Jump Instruction
فصل پنجم انشعاب و حلقه.
Microprocessor and Assembly Language
8086 Registers Module M14.2 Sections 9.2, 10.1.
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
Symbolic Instruction and Addressing
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
University of Gujrat Department of Computer Science
Microprocessor and Assembly Language
EECE.3170 Microprocessor Systems Design I
High-level language structures
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
Chapter 7 –Program Logic and Control
Part VI Looping Structures
Presentation transcript:

Microprocessor and Assembly Language Lecture-6-Control Flow Instructions Muhammad Hafeez Department of Computer Science GC University Lahore

Today’s Agenda Conditional Jump Unconditional Jump

Conditional Jump The control is transferred to another location if certain condition is met Flags in the flag register indicate the condition Syntax. Jxxx destination_label

Conditional Jump If jump condition is false, IP is not altered, thereby, execute next instruction after jump Range of conditional jump is -128-+127 relative to current IP Conditional jumps are sometimes referred to short jumps

Conditional Jump Three Categories of Conditional Jumps: Singed Jumps [singed interpretation to the result] Unsigned Jump [unsigned interpretation to the result] Single flag jump [based on the value of single flag]

CMP Instruction Single Flag jumps are taken by looking at the value of single flag change But singed and unsigned jumps are taken with a comparison based result using CMP instruction Syntax, CMP Destination, Source

CMP Instruction Works like SUB instruction and define the result by subtracting source from destination, however, Destination is not change in CMP Source and Destination must not be memory locations, destination must not be constant

Single Flag Jump Works like SUB instruction and define the result by subtracting source from destination, however, Destination is not change in CMP Source and Destination must not be memory locations, destination must not be constant

Signed Jump

Unsigned Jump

Singed and Unsigned Jumps Each singed jump has its counterpart in unsigned like JG has JA Signed and unsigned jumps may be used by programmer based on interpretation of the result When we compare character, use either of singed or unsigned jump, for Extended ASCII use unsinged AX = 7FFFH , BX=8000H CMP AX,BX JA someotherlocation Even though AX > BX in singed way, jump will not be taken, as its unsigned jump

Example of Conditional Jump

Unconditional Jump JMP instruction take an unconditional jump Syntax JMP Destination Used to get around limit problem of conditional jump

Unconditional Jump TOP:. ;body of the loop DEC CX ; decrement counter JNZ DOWN ;keep looping if CX > 0 JMP EXIT DOWN: JMP TOP EXIT: MOV AX,BX

Unconditional Jump Three Types of Unconditional Jumps NEAR JUMP SHORT JUMP FAR JUMP

Unconditional Jump SHORT JUMP, Syntax is JMP SHORT label In this jump the address of target location is within -128 to +127 bytes of memory relative to the address of current IP. Opcode is EB and operand is 1 byte in the range 00 to FF Coding SHORT makes jump more efficient

Unconditional Jump NEAR JUMP, Types of Near Jump Direct Jump, Syntax JMP NEAR label In this jump the address of target location is within -32768 to +32767 bytes of memory relative to the address of current IP, within current code segment. Register In-Direct Jump, Syntax JMP BX IP takes the value in BX register Memory In-Direct Jump, Syntax JMP [DI] IP takes the value in DI AND DI+1 FAR JUP, Syntax JMP FAR PTR label Takes control out of current code segment, CS, IP both are changed

High Level Language Equivalence in Assembly If-Then in HLL If condition then some statement End if

High Level Language Equivalence in Assembly EX: If AX contains a number less than 0 then place 41H in AX Sol: ;If AX < 0 CMP AX, 0 JNL END_IF ;THEN NEG AX INC AX END_IF:

High Level Language Equivalence in Assembly If-Then-Else in HLL If condition then Then statement for true case Else statement for false case End if

High Level Language Equivalence in Assembly EX: Suppose two numbers in AL and BH, put the largest one in CL Sol: ;If AL <= BH ; CMP AL, BH JNLE ELSE_ ;THEN MOV CL,BH ;IF AL >= BH :ELSE_ MOV CL,AL ;END_IF

High Level Language Equivalence in Assembly Multi-Branch Structure of HLL in assembly Switch Statement IF THEN ELSEIF THEN ELSE END IF

High Level Language Equivalence in Assembly EX: If AL contains a positive number, put -1 in BH, if AL contain 0 then put 0 in BH, if AL contains a positive number put +1 in BH? Sol: Use Case structure from HLL

High Level Language Equivalence in Assembly Compound Statements Using ‘AND’ ‘OR’ IF cond1 AND Condi2 THEN statements END IF IF cond1 OR Condi2 THEN

High Level Language Equivalence in Assembly Example of OR compound statement MOV AH,1 ; CHARACTER INPUT FUNCTION INT 21H ;TAKE INPUT INTO AL CMP AL,'Y' JE THEN CMP AL,'y' JMP ELSE_ THEN: MOV DL,AL MOV AH,2 INT 21H ELSE_:

High Level Language Equivalence in Assembly Example of AND compound statement MOV AH,1 INT 21H CMP AL,'A' JNGE END_IF CMP AL,'Z' JNLE END_IF MOV DL,AL MOV AH,2 END_IF:

Iterative Structure Looping/ Iterative structure is the most important in any programming language We can implement loops by using jump instructions and labels, until some specific conditions is met Besides implementing loops with jumps and labels we also have LOOP instruction in 8086/8088 assembly

While Loop While Loop While condition is true Do End While statement for true case End While

While Loop EX: Input Number of Character, count them, untill Enter is pressed Initialize count to 0 Read a Character While character <> CR do Count = count + 1 Read a character End While

While Loop – Example MOV DX,0 MOV AH,1 ;READ CARACTER FUNCTION INT 21H ;READ CHARACTER INTO AL WHILE_: CMP AL, 0DH JE END_WHILE INC DX MOV AH,2 MOV DL,AL INT 21H MOV AH,1 JMP WHILE_: END_WHILE:

Do – While Loop Do-While Loop Do statement While (condition)

Do – While loop EX: Read text while spacebar is pressed Do Read Character While character <> spacebar

Do – While Example: MOV AH,1 ;DO DO: INT 21H CMP AL,20H ;WHILE CHARACTER IS NOT A SPACEBAR JNE DO

Loop Instruction The LOOP instruction is the easiest way to repeat a block of statements a specific number of times. CX is automatically used as a counter and is decremented each time the loop repeats. Syntax: LOOP destination First loop instruction subtracts 1 from CX, then if CX is greater than zero, control is transferred to destination. The destination operand must be a short label, in range -128 to +127 bytes from the current location.

Loop Instruction - Example What if CX is zero, even before the loop begins? How to check CX value ? Figure out how to implement nested loop

Questions ??????????????????????????