Flags Register & Jump Instruction

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 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.
Ch. 5 from Yu & Marut. Registers 14(16-bit) registers: 1.Data reg. – to hold data for an op. 2.Address reg – to hold addr of an instruction or data.
Unit-1 PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE Advance Processor.
Lecture 3 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Types of Registers (8086 Microprocessor Based)
LAB Flag Bits and Register
Lecture 4 ( Assembly Language).
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Arithmetic Flags and Instructions
(Flow Control Instructions)
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
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.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
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.
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
Microprocessor MA Rahim Khan Computer Engineering and Networks Department.
Introduction to Microprocessors Chapter 3. Programming Model (8086)  Shows the various internal registers that are accessible to the programmer.
Introduction to Computer Organization and Assembly Language
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.
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,
Chapter 12 Processor Structure and Function. Central Processing Unit CPU architecture, Register organization, Instruction formats and addressing modes(Intel.
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
8086 Microprocessor.
ICS312 SET 7 Flags.
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
The FLAGS Register An x bit means an unidentified value 9/12/2018
Assembly Language for Intel-Based Computers, 5th Edition
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
Assembly IA-32.
Assembly Language Lab (4).
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
ECE 353 Introduction to Microprocessor Systems
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
CÁC LỆNH HỢP NGỮ.
Introduction to Assembly Language
Lecture 4 ( Assembly Language).
Lecture 1 Instruction set of 8086 Лектор: Люличева И.А. 1.
תכנות בסיסי בשפת סף פרק 5 מצגת 3.
Shift & Rotate Instructions)
ECE 3430 – Intro to Microcomputer Systems
Program Logic and Control
University of Gujrat Department of Computer Science
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Shift & Rotate Instructions)
Flow Control Instructions
EECE.3170 Microprocessor Systems Design I
Computer Architecture and System Programming Laboratory
Assembly Language for Intel 8086 Jump Condition
Chapter 7 –Program Logic and Control
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Ch. 5 – Intel 8086 – study details from Yu & Marut
Part IV The FLAGS Register
Presentation transcript:

Flags Register & Jump Instruction Lecture 3 Flags Register & Jump Instruction

Outline The FLAGS Register Overflow How instruction affect the flags Jump Instruction

The FLAGS Register - Introduction One important feature that distinguishes a computer from other machines is the computer’s ability to make decisions. Decision are made based on the current state of the processor. In the 8086 processor, the processor state is implemented as nine individual bits called flags. Each decision made by the 8086 is based on the values of these flags. The flags are placed in the FLAGS register.

The FLAGS Register Two kinds of flags: Status Flags: reflect the result of an instruction executed by the processor (ex. ZF ,PF ,SF ,OF) Control Flags: enable or disable certain operations of the processor (ex. If the IF “Interrupt Flag” is set to 0, inputs from the keyboard are ignored by the processor).

The FLAGS Register 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 OF DF IF TF SF ZF AF PF CF Status Flags: (0,2,4,6,7, and 11) Carry Flag (CF) Parity Flag (PF) Auxiliary carry Flag (AF) Zero Flag (ZF) Sign Flag (SF) Overflow Flag (OF) Control Flags: (8,9,10) Trap Flag (TF) Interrupt Flag (IF) Direction Flag (DF)

The FLAGS Register - Status Flags Carry Flag (CF): CF = 1 if there is a carry out from the msb on addition. CF = 1 if there is a borrow into the msb on subtraction. CF = 0 otherwise. CF is also affected by shift and rotate instructions. Parity Flag (PF): PF = 1 if the low byte of a result has an even number of one bits PF = 0 if the low byte has odd parity (ex. if a result is FFFE PF = 0). 1111 1111 1111 1101

The FLAGS Register - Status Flags Auxiliary carry Flag (AF): AF = 1 if there is a carry out from bit 3 on addition. AF = 1 if there is a borrow into bit 3 on subtraction. AF = 0 otherwise. AF is used in BCD (binary coded decimal)operations. Zero Flag (ZF): ZF = 1 for a zero result. ZF = 0 for nonzero result.

The FLAGS Register - Status Flags Sign Flag (SF): SF = 1 if the msb of a result is 1. (i.e. Negative if signed interpretation is used). SF = 0 if the msb is 0. Overflow Flag (OF): OF = 1 if signed overflow occurred. OF = 0 otherwise.

Overflow The phenomenon of overflow is associated with the fact that the range of numbers that can be represented in a computer is limited. If the result of an operation falls outside the range, overflow occurs and the truncated result that is saved will be incorrect.

How the Processor Determines that Overflow Occurred? Signed Overflow On addition of numbers with the same sign, signed overflow occurs when the sum has a different sign. Subtraction of numbers with different signs is like adding numbers of the same sign (ex. A - (-B) = A + B). Signed overflow occurs if the result has a different sign than expected. In addition of numbers with different signs, overflow is impossible. Subtraction of numbers with the same sign cannot give overflow.

How Instructions Affect the Flags In general, each time the processor executes an instruction, the flags are altered to reflect the result. However, some instructions don’t affect any of the flags, affects only some of them, or may leave them undefined. Instruction Affects flags MOV/XCHG none ADD/SUB all INC/DEC all except CF

How Instructions Affect the Flags Example: ADD AX, BX where AX contains FFFFh, BX contains FFFFh FFFFh (1111 1111 1111 1111) +FFFFh (1111 1111 1111 1111) 1 FFFEh = 1111 1111 1111 1110b SF = 1 because the msb is 1. PF = 0 because there are 7 (odd number) of 1 bits in the low byte. ZF = 0 because the result is nonzero. CF = 1 because there is a carry out of the msb on addition. OF = 0 because the sign of the stored result is the same as that of the numbers being added.

How Instructions Affect the Flags Example: ADD AL, BL where AL contains 80h, BL contains 80h 80h +80h 100h SF = 0 because the msb is 0. PF = 1 because all the bits in the result are zero. ZF = 1 because the result is 0. CF = 1 because there is a carry out of the msb on addition. OF = 1 because the numbers being added are both negative, but the result is 0.

How Instructions Affect the Flags Example: SUB AX, BX where AX contains 8000h, BX contains 0001h 8000h - 0001h 7FFFh = 0111 1111 1111 1111b SF = 0 because the msb is 0. PF = 1 because there are 8 (even number) one bits in the low byte. ZF = 0 because the result is nonzero. CF = 0 because a smaller unsigned number is being subtracted from a larger one. OF = 1 because a positive number is being subtracted from a negative one (like adding two negatives), and the result is positive.

How Instructions Affect the Flags Example: MOV AX, -5 None of the flags are affected by MOV.

The JMP Instruction - Unconditional Jumps The JMP (jump) instruction causes an unconditional transfer of control (unconditional jump). Syntax: JMP destination Example JMP L10 …….. L10: INC CX SHORT/NEAR/FAR address JMP

Short,near,and far addresses 1- A short address, limited to a distance of -128 to 127 bytes 2- A near address, limited to a distance of -32,768 to 32,767 bytes 3- A far address, which may be within the same segment at a distance over 32K or in other segment

The JMP Instruction - Unconditional Jumps Backward and Forward jumps Backward: L10: ……. JMP L10 Forward:

The CMP Instruction The jump condition is often provided by the CMP (compare) instruction Syntax: CMP destination, source Compares by computing destination contents minus source contents. The result is not stored, but the flags are affected. Destination may not be a constant. CMP is just like SUB, except that destination is not changed.

Conditional Jumps Syntax Jxxx destination_label Example JNZ PRINT_LOOP If the condition for the jump is true, the next instruction to be executed is the one at destinaltion_label (PRINT_LOOP), which may precede or follow the jump instruction itself. If the condition is false, the instruction immediately following the jump is done next. For JNZ, the condition is that the result of the previous operation is not zero.

Conditional Jumps Signed Jumps: used for signed interpretations. Symbol Description Condition for Jumps JG/JNLE jump if grater than ZF = 0 & SF = OF jump if not less than or equal JGE/JNL jump if grater than or equal SF = OF jump if not less than JL/JNGE jump if less than SF <> OF jump if not greater than or equal JLE/JNG jump if less than or equal ZF = 1 or SF <> OF jump if not grater than

Conditional Jumps Unsigned Jumps: used for unsigned interpretations. Symbol Description Condition for Jumps JA/JNBE jump if above CF = 0 & ZF = 0 jump if not below or equal JAE/JNB jump if above or equal CF = 0 jump if not below JB/JNAE jump if below CF = 1 jump if not above or equal JBE/JNA jump if below or equal CF = 1 or ZF = 1 jump if not above

Conditional Jumps Single Flag Jumps: operates on settings of individual flags. Symbol Description Condition for Jumps JE/JZ jump if equal/ jump if equal to 0 ZF = 1 JNE/JNZ jump if not equal/ jump if not 0 ZF = 0 JC jump if carry CF = 1 JNC jump if no carry CF = 0 JO jump if overflow OF = 1 JNO jump if no overflow OF = 0 JS jump if sign negative SF = 1 JNS jump if nonnegative sign SF = 0 JP/JPE jump if parity even PF = 1 JNP/JPO jump if parity odd PF = 0

Examples In the following example, the JL instruction jumps to label L1 because AX is less than 6: mov ax,5 cmp ax,6 jl L1 ; jump if less In the following example, the jump is taken because AX is greater than 4: mov ax,5 cmp ax,4 jg L1 ; jump if greater