Assignment #4
Name :
Rizwan Ali
Roll No:
17-Arid-1395
Class:
BSCS 6c
Submitted to :
Sir Usman
Problem Descriptions
1. For each of the following instructions, give the new destination contents and
the new settings of CF, SF, ZF, PF, and OF. Suppose that the flags are initially 0 in each part of
this question.
a. ADD AX,BX where AX contains 7FFFh and BX contains 000lh
b. SUB AL,BL where AL contains 01h and BL contains FFh
c. DEC AL where AL contains 00h
d. NEG AL where AL contains 7Fh
e. XCHG AX,BX where AX contains 1ABCh and BX contains 712Ah
f. ADD AL,BL where AL contains 80h and BL contains FFh
g. SUB AX,BX where AX contains 0000h and BX contains 8000h
h. NEG AX where AX contains 0001h
Solution:
ADD AX,BX ; AX=7FFFh and BX=0001h
7=0111,F=1111
0111111111111111
+0000000000000001
1000000000000000
AL=8000h
CF=0(No carry out)
SF=1
ZF=0
PF=1
OF=1(Carry in but no carry out)
SUB AL,BL ; AL=01h and BL=FFh
00000001
-11111111
00000010
AL=02h
CF=1(borrow into MSB)
SF=0
ZF=0
PF=0
OF=0(Borrow in and borrow out)
DEC AL ; AL=00h
00000000
-1
11111111
AL=FFh
CF=no effect(increment/decrement do not effect CF)
SF=1
ZF=0
PF=1
OF=0(Borrow in and borrow out)
NEG AL ; AL=7Fh
7=0111,F=1111
1’scompliment=10000000
10000000
+1
10000001
AL=81h
CF=1(result is not zero)
SF=1
ZF=0
PF=1
OF=0
XCHG AX,BX ; AX=1ABCh,BX=712Ah
AX=712Ah
BX=1ABCh
No effect at any flag
ADDAL,BL ;AL=80h,BL=FFh
10000000
+11111111
101111111
AL=17Fh
CF=1
SF=0
ZF=0
PF=0
OF=1
SUB AX,BX ;AX=0000h,BX=8000h
0000000000000000
- 1000000000000000
1000000000000000
AX=8000h
CF=1
SF=1
ZF=0
PF=1
OF=1(borrow in but not borrow out)
NEG AX ; AX=0001h
0000000000000001
1’scompliment=1111111111111110
1111111111111110
+1
1111111111111111
AX=FFFFh
CF=1(result is not 0)
SF=1
ZF=0
PF=1
OF=0
2. a. Suppose that AX and BX both contain positive numbers, and ADD
AX,BX is executed. Show that there is a carry into the msb but no carry
out of the msb if, and only if, signed overflow occurs.
Solution:
As we know signed overflow occurs if and if:
There is a carry into the MSB but no carry out of the MSB.(for addition)
There is a carry out of the MSB but no carry into the MSB.(for addition)
There is a borrow into the MSB but no borrow into the MSB.(for subtraction)
There is a borrow out of the MSB but no borrow into the MSB.(for
subtraction)
So;
Here signed flow is occurred
because of a carry into the MSB but no carry out of the MSB.
Also in part e where there is no carry out and no carry in; no signed overflow
occurred.
.
b Suppose AX and BX both contain negative numbers, and ADD AX,BX is
executed. Show that there is a carry out of the msb but no carry into the msb if,
and only if, signed overflow occurs.
Solution:
Let’s take an example 8Fh+C9 ;
1000 1111
+1100 1001
1 0101 1000
We can see there is a carry out of the MSB but no carry into the MSB and a
signed overflow occurred.
Remember:
Whenever there is ‘but’ in carry of MSB or borrow of MSB there is a signed overflow. And
Whenever there is an ‘and’ in carry of MSB or borrow of MSB there is no signed overflow.