0% found this document useful (0 votes)
29 views25 pages

Module 4B

Here are the key points about the overflow and carry flags from a hardware viewpoint: - The carry flag (CF) indicates a carry or borrow between the least significant bits of the operands. It gets set on an unsigned overflow. - The overflow flag (OF) indicates a signed overflow, which occurs when incorrect results are produced due to loss of sign information. It gets set when an ADD produces a carry into the sign bit or a SUB borrows from the sign bit. - Both flags provide information about arithmetic overflow conditions from an internal binary perspective, which is important for correctly implementing signed and unsigned arithmetic.

Uploaded by

Godwin Ekanem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views25 pages

Module 4B

Here are the key points about the overflow and carry flags from a hardware viewpoint: - The carry flag (CF) indicates a carry or borrow between the least significant bits of the operands. It gets set on an unsigned overflow. - The overflow flag (OF) indicates a signed overflow, which occurs when incorrect results are produced due to loss of sign information. It gets set when an ADD produces a carry into the sign bit or a SUB borrows from the sign bit. - Both flags provide information about arithmetic overflow conditions from an internal binary perspective, which is important for correctly implementing signed and unsigned arithmetic.

Uploaded by

Godwin Ekanem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Evaluate this . . .

• We want to write a program that adds the following three bytes:


.data
myBytes BYTE 80h,66h,0A5h

• What is your evaluation of the following code?


mov al,myBytes
add al,[myBytes+1]
add al,[myBytes+2]

• What is your evaluation of the following code?


mov ax,myBytes
add ax,[myBytes+1]
add ax,[myBytes+2]

• Any other possibilities?

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
1
Evaluate this . . . (contd.)
.data
myBytes BYTE 80h,66h,0A5h

• How about the following code. Is anything missing?

movzx ax,myBytes
mov bl,[myBytes+1]
add ax,bx
mov bl,[myBytes+2]
add ax,bx ; AX = sum

Yes: Move zero to BX before the MOV instruction.

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
2
What's Next

 Addition and Subtraction

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
3
Addition and Subtraction

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
4
INC and DEC Instructions




• The Overflow, Sign, Zero, Auxiliary Carry, and Parity flags are changed
according to the value of the destination operand.
• The INC and DEC instructions do not affect the Carry flag.

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
5
INC and DEC Examples
.data
myWord WORD 1000h
myDword DWORD 10000000h
.code
inc myWord ; 1001h
dec myWord ; 1000h
inc myDword ; 10000001h

mov ax,00FFh
inc ax ; AX = 0100h
mov ax,00FFh
inc al ; AX = 0000h

What are CF and ZF?

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
6
Your turn...

.data
myByte BYTE 0FFh, 0
.code
mov al,myByte ; AL = FFh
mov ah,[myByte+1] ; AH = 00h
dec ah ; AH = FFh
inc al ; AL = 00h
dec ax ; AX = FEFF

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
7
ADD and SUB Instructions
• ADD destination, source
• Logic: destination  destination + source
• SUB destination, source
• Logic: destination  destination – source
• Same operand rules as for the MOV instruction

• The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed
according to the value that is placed in the destination operand.

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
8
ADD and SUB Examples
.data
var1 DWORD 10000h
var2 DWORD 20000h
.code ; ---EAX---
mov eax,var1 ; 00010000h
add eax,var2 ; 00030000h
add ax,0FFFFh ; 0003FFFFh
add eax,1 ; 00040000h
sub ax,1 ; 0004FFFFh

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
9
NEG (negate) Instruction
Reverses the sign of an operand. Operand can be a register or memory operand.
(Like converting to its Two’s Complement)

.data
valB BYTE -1
valW WORD +32767
.code
mov al,valB ; AL = -1
neg al ; AL = +1
neg valW ; valW = -32767

Suppose AX contains –32,768 and we apply NEG to it. Will the result be valid?

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
10
NEG Instruction and the Flags
The processor implements NEG using the following internal operation:
SUB 0,operand

Any nonzero operand causes the Carry flag to be set.

.data
valB BYTE 1,0
valC SBYTE -128
.code
neg valB ; CF = 1, OF = 0
neg [valB + 1] ; CF = 0, OF = 0
neg valC ; CF = 1, OF = 1

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
11
Implementing Arithmetic Expressions
The ADD, SUB, and NEG instructions will aid the implementation of arithmetic
expressions involving addition, subtraction, and negation in assembly language; the
same way HLL compilers translate mathematical expressions.
Rval = -Xval + (Yval – Zval)

Rval DWORD ?
Xval DWORD 26
Yval DWORD 30
Zval DWORD 40
.code
mov eax,Xval
neg eax ; EAX = -26
mov ebx,Yval
sub ebx,Zval ; EBX = -10
add eax,ebx
mov Rval,eax ; -36

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
12
Your turn...
Translate the following expression into assembly language.
Do not permit Xval, Yval, or Zval to be modified:
Rval = Xval - (-Yval + Zval)

Assume that all values are signed doublewords.

mov ebx,Yval
neg ebx
add ebx,Zval
mov eax,Xval
sub eax,ebx
mov Rval,eax

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
13
Flags Affected by Arithmetic Instructions

 MOV instruction never affects the flags

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
14
Concept Map CPU

part of executes

executes
ALU
conditional jumps
arithmetic & bitwise
operations attached to used by provide

affect
status flags
branching logic

You can use diagrams such as these to express the relationships between assembly language concepts.

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
15
Zero Flag (ZF)
The Zero flag is set when the result of an operation produces zero in the destination
operand.

mov cx,1
sub cx,1 ; CX = 0, ZF = 1
mov ax,0FFFFh
inc ax ; AX = 0, ZF = 1
inc ax ; AX = 1, ZF = 0

Remember...
• A flag is set when it equals 1.
• A flag is clear when it equals 0.

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
16
Sign Flag (SF)
The Sign flag is set when the destination operand is negative. The flag is clear when
the destination is positive.
mov cx,0
sub cx,1 ; CX = -1, SF = 1
add cx,2 ; CX = 1, SF = 0

The sign flag is a copy of the destination’s highest bit:

mov al,0
sub al,1 ; AL = 11111111b, SF = 1
add al,2 ; AL = 00000001b, SF = 0

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
17
Signed and Unsigned Integers
A Hardware Viewpoint

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
18
Overflow and Carry Flags
A Hardware Viewpoint
 ADD

carry into the MSB)

 SUB

carry into the MSB)

MSB = Most Significant Bit (high-order bit)


XOR = eXclusive-OR operation
NEG = Negate (same as SUB 0,operand )

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
6/E, 2010.
19
Carry Flag (CF)
unsigned

mov al,0FFh
add al,1 ; CF = 1, AL = 00

; Try to go below zero:

mov al,0
sub al,1 ; CF = 1, AL = FF

How about this, Why?


mov al,2
sub al,1 ; CF = ?, AL = ?

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
20
Your turn . . .
For each of the following marked entries, show the values of the destination operand
and the Sign, Zero, and Carry flags:

mov ax,00FFh
add ax,1 ; AX= 0100h SF= 0 ZF= 0 CF= 0
sub ax,1 ; AX= 00FFh SF= 0 ZF= 0 CF= 0
add al,1 ; AL= 00h SF= 0 ZF= 1 CF= 1
mov bh,6Ch
add bh,95h ; BH= 01h SF= 0 ZF= 0 CF= 1

mov al,2
sub al,3 ; AL= FFh SF= 1 ZF= 0 CF= 1

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
21
Overflow Flag (OF)

; Example 1
mov al,+127
add al,1 ; OF = 1, AL = ??

; Example 2
mov al,7Fh ; OF = 1, AL = 80h
add al,1

The two examples are identical at the binary level because 7Fh equals +127. To
determine the value of the destination operand, it is often easier to calculate in
hexadecimal.

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
22

A Rule of Thumb

What will be the values of the Overflow flag?


mov al,80h
add al,92h ; OF = 1

mov al,-2
add al,+127 ; OF = 0

Q: How about CF? Notice: -2 is 1111 1110 (254)

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
23
Your turn . . .
What will be the values of the given flags after each operation?

mov al,-128
neg al ; CF = 1 OF = 1

mov ax,8000h
add ax,2 ; CF = 0 OF = 0

mov ax,0
sub ax,2 ; CF = 1 OF = 0

mov al,-5
sub al,+125 ; CF = 0 OF = 1

IRVINE, KIP R.
ASSEMBLY LANGUAGE
FOR X86 PROCESSORS
7/E, 2015.
24
Your turn . . .
Is there any difference between these?

mov al, -5 ; 1111 1011


sub al, 125 ; 0111 1101 For unsigned,
; al =7E, OF =1, CF =0 -5 is 251

mov bl, -5 ; 1111 1011


add bl, -125 ; 1000 0011
; bl =7E, OF =1, CF =1

ADD 1111 1011 and 1000 0011?

251d + 131d = 382d which is 126d(7Eh) mod 256

ADDED BY ZUOLIU
DING 5/E, 2007. 25

You might also like