microprocessor i - lecture 06draelshafee.net/fall2019/microprocessor-i---lecture-06.pdf · 80286...

13
Lecture (06) By: Dr. Ahmed ElShafee Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 1 1 The Flag Register A (Auxiliary) [half carry] C (carry): [result exceeds the size of distination] O (overflow): [something went wrong] P (Parity) [even number of ones] S (sign): [MSB =1] Z (zero): [==0] NT 14 IOP 1 13 IOP 0 12 O 11 D 10 I 9 T 8 S 7 Z 6 A 4 P 2 C 0 VIF 19 AC 18 VM 17 RF 16 31 ID 21 VIP 20 8088/8086 80286 80386 80486 Pentium

Upload: others

Post on 27-Jul-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Lecture (06)

By:

Dr. Ahmed ElShafee

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 11

The Flag Register

• A (Auxiliary) [half  carry]

• C (carry): [result exceeds the size of distination]

• O (overflow): [something went wrong]

• P (Parity) [even number of ones]

• S (sign): [MSB =1]

• Z (zero): [==0]

NT

14

IOP1

13

IOP0

12

O

11

D

10

I

9

T

8

S

7

Z

6

A

4

P

2

C

0

VIF

19

AC

18

VM

17

RF

1631

ID

21

VIP

20

8088/8086

80286

80386

80486

Pentium

Page 2: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Carry Flag

• 1. The carry flag is set if the addition of two numbers causes a carry out of the most significant (leftmost) bits added. 

• 1111 + 0001 = 0000 (carry flag is turned on) 

• 2. The carry (borrow) flag is also set if the subtraction of two numbers requires a borrow into the most significant (leftmost) bits subtracted. 

• 0000 ‐ 0001 = 1111 (carry flag is turned on) Otherwise, the carry flag is turned off (zero).

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 13

Overflow Flag

• 1. If the sum of two numbers with the sign bits off yields a result number with the sign bit on, the "overflow" flag is turned on. 

0100 + 0100 = 1000 (overflow flag is turned on) 

• 2. If the sum of two numbers with the sign bits on yields a result number with the sign bit off, the "overflow" flag is turned on. 

1000 + 1000 = 0000 

(overflow flag is turned on) Otherwise, the overflow flag is turned off.

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 14

Page 3: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 15

Arithmetic and Logic Instructions• Addition: (Flags affected: A,C,O,P,S,Z)

– ADD AL,BL ; AL AL + BL , BL unchanged

• Register addressing mode

– ADD CX,DI ; CX CX + DI , DI unchanged

• Register addressing mode

– ADD AH,45H ; AH AH + 45H

• Immediate addressing mode

– ADD [BX],AL ; [BX] [BX] + AL

• Register indirect addressing mode

– ADD CX,[BX] ; CX CX + [BX]

– ADD AL,CX ; INVALID

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 16

Page 4: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Add with Carry: (Flags affected: A,C,O,P,S,Z)

– ADC AH,BH ; AH AH + BH + Carry

• Register addressing mode

– ADC AX,CX ; AX AX + CX + Carry

• Register addressing mode

– ADC AL,[BX+SI] ; AL AL + [BX+SI] + Carry

• Register indirect addressing mode

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 17

Subtraction: (Flags affected: A,C,O,P,S,Z)

– SUB AL,BL ; AL AL - BL ;BL unchanged

• Register addressing mode

– SUB CX,DI ; CX CX - DI ;DI unchanged

• Register addressing mode

– SUB AH,45H ; AH AH - 45H

• Immediate addressing mode

– SUB BL,ARRAY ; BL BL - [ARRAY]

• Direct Addressing mode

– SUB [BX],AL ; [BX] [BX] – AL

• Register indirect addressing mode

– SUB AL,CX ; INVALIDDr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 18

Page 5: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Subtract with Borrow: (Flags affected: A,C,O,P,S,Z)

– SBB AH,BH ; AH AH - BH – Carry

• Register addressing mode

– SBB AX,CX ; AX AX - CX – Carry

• Register addressing mode

– SBB AL,[BX+SI] ; AL AL - [BX+SI] – Carry

• Register indirect addressing mode

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 19

Increment: (Flags affected: A,O,P,S,Z)

– INC AL ; AL AL + 1

– INC SP ; SP SP + 1

– INC COUNT1 ; [COUNT1] [COUNT1] + 1

– INC BYTE PTR[BX] ; [BX] [BX] + 1

– INC WORD PTR[BX] ; [BX] [BX] + 1

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 110

Page 6: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Example 01

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 111

org 100h jmp start db 0feh,00hstart:mov bx,0102hINC BYTE PTR[BX] INC BYTE PTR[BX]

ret

org 100h jmp start db 0feh,00hstart:mov bx,0102hINC BYTE PTR[BX] INC WORD PTR[BX]

ret

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 112

Page 7: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 113

Decrement: (Flags affected: A,O,P,S,Z)

– DEC AL ; AL AL - 1

– DEC SP ; SP SP - 1

– DEC COUNT1 ; [COUNT1] [COUNT1] - 1

– DEC BYTE PTR[BX] ; [BX] [BX] - 1

– DEC WORD PTR[BX] ; [BX] [BX] - 1

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 114

Page 8: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Example 02

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 115

org 100h jmp start db 001h,00hstart:mov bx,0102hdec BYTE PTR[BX] dec BYTE PTR[BX]

ret

org 100h jmp start db 01h,00hstart:mov bx,0102hdec BYTE PTR[BX] dec WORD PTR[BX]

ret

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 116

Page 9: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 117

Example 03

• What will be the values of the carry, overflow, sign and zero flags after the execution of each of the following instructions:

– MOV DX,0

– DEC DX

– MOV AX,720H

– SUB AX, 0E6H

– MOV DX,0

– DEC DX

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 118

Page 10: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 119

• Instruction AX DX Flags

H L H L C Z S OMOV DX,0

DEC DX

MOV AX,720H

SUB AX, 0E6H

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 120

• Instruction AX DX Flags

H L H L C Z S OMOV DX,0 00 00 0 0 0 0DEC DX FF FF 0 0 1 0

MOV AX,720H 07 20 ‐ ‐ ‐ ‐SUB AX, 0E6H 06 3A 0 0 0 0

Page 11: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Example 04

MOV   AX,847AH

SUB   CX,CX

MOV   BX,5CE8H

ADD   AL,BH

ADC   AH,BL

MOV   CL,AL

ADD   CH,BL

MOV   SI,0108H

SUB   BL,[SI]

SBB   BH,[SI+4]

ADD   AL,[010CH]

MOV   AX,[0106H]

INC     AL

DEC   BX

ADD   CX,[SI+4] 

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 121

Fill up the trace table given below.

CS:0108H 02CS:0106H E8CS:0107H 5CCs:010CH 8ACS:010DH C8

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 122

InstructionAX BX CX SI Flags

H L H L H L C Z S OMOV   AX,847AHSUB   CX,CXMOV   BX,5CE8HADD   AL,BHADC   AH,BLMOV   CL,ALADD   CH,BLMOV   SI,0108HSUB   BL,[SI] (02)

SBB   BH,[SI+4] (8A)

ADD   AL,[010CH](8A)

MOV   AX,[0106H] (5CE8)

INC     ALDEC   BXADD   CX,[SI+4] 

Page 12: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 123

InstructionAX BX CX SI Flags

H L H L H L C Z S OMOV   AX,847AH 84 7A ‐ ‐ ‐ ‐SUB   CX,CX 00 00 0 1 0 0MOV   BX,5CE8H 5C E8 ‐ ‐ ‐ ‐ADD   AL,BH D6 0 0 1 1ADC   AH,BL 6C 1 0 0 1MOV   CL,AL D6 ‐ ‐ ‐ ‐ADD   CH,BL E8 0 0 1 0MOV   SI,0108H 0108 ‐ ‐ ‐ ‐SUB   BL,[SI] (02) E6 0 0 1 0SBB   BH,[SI+4] (8A) D2 1 0 1 1ADD   AL,[010CH](8A)

60 1 0 1 1

MOV   AX,[0106H] (5CE8)

5C E8 ‐ ‐ ‐ ‐

INC     AL E9 ‐ 0 1 0DEC   BX D2 E5 ‐ 0 1 0ADD   CX,[SI+4] (C88A) B1 60 1 0 1 0

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 124

Page 13: Microprocessor I - Lecture 06draelshafee.net/Fall2019/microprocessor-i---lecture-06.pdf · 80286 80386 80486 Pentium. Carry Flag • 1. The carry flag is set if the addition of two

Thanks,..

Dr. Ahmed ElShafee, ACU : Fall 2019, Microprocessors 125