Addition and Subtraction Instructions Module M16.3 Section 10.4

Download Report

Transcript Addition and Subtraction Instructions Module M16.3 Section 10.4

Addition and Subtraction
Instructions
Module M16.3
Section 10.4
Introduction to Computer Engineering by Richard E. Haskell
Binary Addition
• Can add immediate data to a register or
memory.
• Can add data from a register to a register.
• Can add data from a register to memory.
• Can add data from memory to a register.
• Can NOT add data directly from one
memory location to another.
Introduction to Computer Engineering by Richard E. Haskell
Table 10.4
Addition Instructions
Mnemonic
ADD ac,data
ADD mem/reg, data
ADD mem/regl,mem/reg2
ADC ac,data
ADC mem/reg,data
ADC mem/reg1,mem/reg2
Meaning
Add immediate data to AX or AL register
Add immediate data to register or memory
location
Add register to register, register to memory,
or memory to register
Add with carry immediate data to AX or AL
register
Add with carry immediate data to register or
memory location
Add with carry register to register, register to
memory, or memory to register
Introduction to Computer Engineering by Richard E. Haskell
Recall Full Adder Truth Table
Ci Ai Bi Si Ci+1
0
0
0
0
1
1
1
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
0
1
1
0
1
0
0
1
0
0
0
1
0
1
1
1
C
A
B
1
0
0
1
1
1
1
1
1
0
1
0
Final carry = 0
Introduction to Computer Engineering by Richard E. Haskell
0
1
1
0
Binary Addition
Binary
Dec
Hex
53
+25
78
35
+19
4E
0
0
0
0
1
0
0
1
1
1
0
0
0
1
1
0
0
0
1
1
0
1
0
1
Introduction to Computer Engineering by Richard E. Haskell
1
0 1
0 1
1 0
Add 35H and 19H
0000
0002
B0 35
04 19
MOV
ADD
AL,35H
AL,19H
sum = 4EH in AL
Introduction to Computer Engineering by Richard E. Haskell
Carry and Overflow
Binary
Dec
53
+25
78
0
0
35
0
+19
4E C = 0 0
O=0
Hex
1
0
0
1
1
1
0
0
0
1
1
0
0
0
1
1
0
1
0
1
1
0 1
0 1
1 0
Note no carry from bit 6 to bit 7
and no carry from bit 7 to C.
Introduction to Computer Engineering by Richard E. Haskell
Carry and Overflow
Binary
Dec
53
+91
144
1
0
35
0
+5B
90 C = 0 1
O=1
Hex
1
0
1
0
1
1
0
0
1
1
1
1
1
0
1
0
1
1
0
0
1
0 1
1 1
0 0
Note carry from bit 6 to bit 7
but no carry from bit 7 to C.
Thinking SIGNED we added two positive numbers
and got a negative result. This can’t be correct!
Therefore, the OVERFLOW bit, O, is set to 1.
Correct answer (144) is outside the range -128 to +127.
Introduction to Computer Engineering by Richard E. Haskell
Carry and Overflow
Binary
Dec
1
0
35
1
+D3
108 C = 1 0
O=0
Hex
53
- 45
8
Ignore
carry
1
0
1
0
1
1
0
0
0
1
1
0
1
0
0
1
1
1
0
0
1
0 1
1 1
0 0
Note carry from bit 6 to bit 7
and carry from bit 7 to C.
Thinking SIGNED we added a positive number to a
negative number and got the correct positive answer.
Therefore, the OVERFLOW bit, O, is cleared to 0.
Correct answer (8) is inside the range -128 to +127.
Introduction to Computer Engineering by Richard E. Haskell
Carry and Overflow
Binary
0 0 1 1 1 1 0
1 0 0 1 1 1 1 0
9E
- 98
1 1 0 1 0 0 1 1
- 45 +D3
- 143 171 C = 1 0 1 1 1 0 0 0 1
Ignore
Note no carry from bit 6 to bit 7
O
=
1
carry
Dec
Hex
but there is a carry from bit 7 to C.
Thinking SIGNED we added two negative numbers
and got a positive answer. This must be wrong!
Therefore, the OVERFLOW bit, O, is set to 1.
Correct answer (-143) is outside the range -128 to +127.
Introduction to Computer Engineering by Richard E. Haskell
Carry and Overflow Summary
0000
0002
B0 35
04 19
0004
0006
B0 35
04 5B
0008
000A
B0 35
04 D3
000C
000E
B0 9E
04 D3
MOV AL,35H
ADD AL,19H
sum = 4EH in AL, C=0, O=0
MOV AL,35H
ADD AL,5BH
sum = 90H in AL, C=0, O=1
MOV AL,35H
ADD AL,D3H
sum = 08H in AL, C=1, O=0
MOV AL,9EH
ADD AL,D3H
sum = 71H in AL, C=1, O=1
Introduction to Computer Engineering by Richard E. Haskell
Overflow
• Note that the overflow bit was set whenever
we had a carry from bit 6 to bit 7, but no
carry from bit 7 to C.
• It was also set when we had a carry from bit
7 to C, but no carry from bit 6 to bit 7.
• Upshot: The overflow bit is the
EXCLUSIVE-OR of a carry from bit 6 to
bit 7 and a carry from bit 7 to C.
Introduction to Computer Engineering by Richard E. Haskell
Binary Subtraction
• Can subtract immediate data from a register
or memory.
• Can subtract a register from a register.
• Can subtract a register from memory.
• Can subtract memory from a register.
• Can NOT subtract data in one memory
location from that in another memory
location.
Introduction to Computer Engineering by Richard E. Haskell
Table 10.7
Subtraction Instructions
Mnemonic
SUB ac,data
SUB mem/reg, data
SUB mem/regl,mem/reg2
SBB ac,data
SBB mem/reg,data
SBB mem/reg1,mem/reg2
Meaning
Subtract immediate data from AL or AX
register
Subtract immediate data from register or
memory location
Subtract register from register, register from
memory, or memory from register
Subtract with borrow immediate data from
AL or AX register
Subtract with borrow immediate data from
register or memory location
Subtract with borrow reg. from reg., reg.
from memory, or memory from reg.
Introduction to Computer Engineering by Richard E. Haskell
Recall Full Subtractor Truth Table
Ci Ai Bi Di Ci+1
0
0
0
0
1
1
1
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
0
1
1
0
1
0
0
1
0
1
0
0
1
1
0
1
C
A
B
1
0
0
1
1
1
1
1
0
0
1
1
Final borrow = 1
Introduction to Computer Engineering by Richard E. Haskell
0
1
1
0
Hex
5
-7
E
Binary Subtraction
Binary
Dec
181
- 111
70
Hex
B5
- 6F
46
1
1
0
0
0
0
1
1
0
1
1
0
1
1
0
0
1
0
1
0
1
1
1
1
Final borrow = 0
Introduction to Computer Engineering by Richard E. Haskell
0
0 1
1 1
1 0
Subtract 6FH from B5H
0000
0002
B0 B5
2C 6F
MOV
SUB
AL,B5H
AL,6FH
difference = 46H in AL
Introduction to Computer Engineering by Richard E. Haskell
Binary Subtraction
Binary
Dec
1
1
B5
0
- 6F
46 C = 0 0
Hex
181
- 111
70
0
0
1
1
0
1
1
0
1
1
0
0
1
0
1
0
1
1
1
1
0
0 1
1 1
1 0
This is the correct answer if we consider B5H to be
UNSIGNED and equal to 181.
But suppose you were thinking of B5H = 10110101
as the 2’s complement negative number 01001011 = 4BH
or -75.
Introduction to Computer Engineering by Richard E. Haskell
Binary Subtraction
Binary
Dec
- 75
- 111
-186
1
1
B5
0
- 6F
46 C = 0 0
O=1
Hex
0
0
1
1
0
1
1
0
1
1
0
0
1
0
1
0
1
1
1
1
0
0 1
1 1
1 0
Thinking SIGNED we subtracted a positive number from a
negative number and got a positive answer. This must be wrong!
Therefore, the OVERFLOW bit, O, is set to 1.
Correct answer (-186) is outside the range -128 to +127.
Introduction to Computer Engineering by Richard E. Haskell
Binary Subtraction
Binary
Dec
Hex
- 75
- 111
-186
B5 B5
- 6F +91
46 1 46
0
1
1
0
1
0
0
1
1
1
0
0
0
1
1
0
0
0
0
0
0
1
0
1
1
0 1
0 1
1 0
1
Note no carry from bit 6 to bit 7
Borrow = !Carry but there is a carry from bit 7 to C.
C=0
Therefore, overflow, O = 1.
Ignore carry
Take the two’s complement of 6F and add.
6FH = 01101111
10010001 = 91H
Introduction to Computer Engineering by Richard E. Haskell
16-Bit Addition
37FAH
+82C4H
BABEH
0000
0003
B8 FA 37
05 C4 82
MOV
ADD
AX,37FAH
AX,82C4H
sum = BABEH in AX
Introduction to Computer Engineering by Richard E. Haskell
16-Bit Subtraction
A1C9H
-8315H
1EB4H
0000
0003
B8 C9 A1
2D 15 83
MOV
SUB
AX,A1C9H
AX,8315H
difference = 1EB4H in AX
Introduction to Computer Engineering by Richard E. Haskell
Sign extending bytes to words
5
-5
16
-5
= 00000101
= 11111011 = FBH
bits
= 1111111111111011 = FFFBH
To add an 8-bit signed number to a 16-bit signed number
the 8-bit number must be sign extended:
If bit 7 is 1, make bits 8 - 15 one.
If bit 7 is 0, make bits 8 - 15 zero.
Introduction to Computer Engineering by Richard E. Haskell
CBW (Convert Byte to Word)
• The 8086 instruction CBW extends the sign
bit of AL into AH.
• Similar words for other microprocessors:
• 68000: EXT (sign EXTend)
• 6809: SEX (Sign EXtend)
Introduction to Computer Engineering by Richard E. Haskell
Add 8-bits to 16-bits (signed)
FBH
+123AH
0000
0002
0002
B0 FB
98
05 3A 12
Sign extend
FFFBH
+123AH
1235H
MOV AL,0FBH ;AL = -5
CBW
;AX = -5
ADD AX,123AH AX = sum
Introduction to Computer Engineering by Richard E. Haskell