Addition and Subtraction Instructions

25
Introduction to Computer Engineering by Richard E. Haskell Addition and Subtraction Instructions Module M16.3 Section 10.4

description

Addition and Subtraction Instructions. Module M16.3 Section 10.4. 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. - PowerPoint PPT Presentation

Transcript of Addition and Subtraction Instructions

Page 1: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Addition and SubtractionInstructions

Module M16.3

Section 10.4

Page 2: Addition and Subtraction Instructions

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.

Page 3: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Table 10.4Addition Instructions

Mnemonic MeaningADD ac,data Add immediate data to AX or AL registerADD mem/reg, data Add immediate data to register or memory

locationADD mem/regl,mem/reg2 Add register to register, register to memory,

or memory to registerADC ac,data Add with carry immediate data to AX or AL

registerADC mem/reg,data Add with carry immediate data to register or

memory locationADC mem/reg1,mem/reg2 Add with carry register to register, register to

memory, or memory to register

Page 4: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Recall Full Adder Truth Table

0 0 0 0 00 0 1 1 00 1 0 1 00 1 1 0 11 0 0 1 01 0 1 0 11 1 0 0 11 1 1 1 1

Ci Ai Bi Si Ci+1

00 1 0 10 1 1 1

A

B

0

1

0

1

1

1

1

C

Final carry = 0

Page 5: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Binary Addition

0 0 1 1 0 1 0 10 0 0 1 1 0 0 1 0111

0

0

1

0

53+25 78

35+19 4E

Dec Hex

Binary

1001

1

0

0

Page 6: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Add 35H and 19H

0000 B0 35 MOV AL,35H0002 04 19 ADD AL,19H

sum = 4EH in AL

Page 7: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Carry and Overflow

0 0 1 1 0 1 0 10 0 0 1 1 0 0 1 0111C = 0

O = 0

0

0

1

0

53+25 78

35+19 4E

Dec Hex

Binary

1001

1

0

0Note no carry from bit 6 to bit 7and no carry from bit 7 to C.

Page 8: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Carry and Overflow

0 0 1 1 0 1 0 10 1 0 1 1 0 1 1 0000C = 0

O = 1

1

1

1

0

53+91144

35+5B 90

Dec Hex

Binary

1111

0

1

1

Thinking SIGNED we added two positive numbersand 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.

Note carry from bit 6 to bit 7but no carry from bit 7 to C.

Page 9: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Carry and Overflow

0 0 1 1 0 1 0 11 1 0 1 0 0 1 1 0001C = 1

O = 0

0

0

1

0

53- 45 8

35+D3 108

Dec Hex

Binary

1111

0

1

0

Thinking SIGNED we added a positive number to anegative 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.

Ignorecarry

Note carry from bit 6 to bit 7and carry from bit 7 to C.

Page 10: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Carry and Overflow

1 0 0 1 1 1 1 01 1 0 1 0 0 1 1 1000C = 1

O = 1

1

1

1

1

- 98 - 45- 143

9E+D3 171

Dec Hex

Binary

0110

1

0

0

Thinking SIGNED we added two negative numbersand 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.

Ignorecarry

Note no carry from bit 6 to bit 7but there is a carry from bit 7 to C.

Page 11: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Carry and Overflow Summary0000 B0 35 MOV AL,35H0002 04 19 ADD AL,19H

sum = 4EH in AL, C=0, O=00004 B0 35 MOV AL,35H0006 04 5B ADD AL,5BH

sum = 90H in AL, C=0, O=10008 B0 35 MOV AL,35H000A 04 D3 ADD AL,D3H

sum = 08H in AL, C=1, O=0000C B0 9E MOV AL,9EH000E 04 D3 ADD AL,D3H

sum = 71H in AL, C=1, O=1

Page 12: Addition and Subtraction Instructions

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.

Page 13: Addition and Subtraction Instructions

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.

Page 14: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Table 10.7Subtraction Instructions

Mnemonic MeaningSUB ac,data Subtract immediate data from AL or AX

registerSUB mem/reg, data Subtract immediate data from register or

memory locationSUB mem/regl,mem/reg2 Subtract register from register, register from

memory, or memory from registerSBB ac,data Subtract with borrow immediate data from

AL or AX registerSBB mem/reg,data Subtract with borrow immediate data from

register or memory locationSBB mem/reg1,mem/reg2 Subtract with borrow reg. from reg., reg.

from memory, or memory from reg.

Page 15: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Recall Full Subtractor Truth Table

0 0 0 0 00 0 1 1 10 1 0 1 00 1 1 0 01 0 0 1 11 0 1 0 11 1 0 0 01 1 1 1 1

Ci Ai Bi Di Ci+1

00 1 0 10 1 1 1

A

B

0

0

1

1

1

1

1

C

Final borrow = 1

5- 7 E

Hex

Page 16: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Binary Subtraction

1 0 1 1 0 1 0 10 1 1 0 1 1 1 1 0110

1

0

0

0

181- 111 70

B5 - 6F 46

Dec Hex

Binary

0110

1

1

0Final borrow = 0

Page 17: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Subtract 6FH from B5H

0000 B0 B5 MOV AL,B5H0002 2C 6F SUB AL,6FH

difference = 46H in AL

Page 18: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Binary Subtraction

1 0 1 1 0 1 0 10 1 1 0 1 1 1 1 0110C = 0

1

0

0

0

181- 111 70

B5 - 6F 46

Dec Hex

Binary

0110

1

1

0

This is the correct answer if we consider B5H to beUNSIGNED and equal to 181.But suppose you were thinking of B5H = 10110101as the 2’s complement negative number 01001011 = 4BHor -75.

Page 19: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Binary Subtraction

1 0 1 1 0 1 0 10 1 1 0 1 1 1 1 0110C = 0

O = 1

1

0

0

0

- 75- 111 -186

B5 - 6F 46

Dec Hex

Binary

0110

1

1

0

Thinking SIGNED we subtracted a positive number from anegative 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.

Page 20: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Binary Subtraction

1 0 1 1 0 1 0 11 0 0 1 0 0 0 1 0110

1Borrow = !CarryC = 0

0

0

1

0

- 75- 111 -186

B5 - 6F 46

Dec Hex

Binary

1001

1

0

0

Take the two’s complement of 6F and add.6FH = 01101111 10010001 = 91H

B5 +911 46

Ignore carry Note no carry from bit 6 to bit 7but there is a carry from bit 7 to C.Therefore, overflow, O = 1.

Page 21: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

16-Bit Addition 37FAH+82C4H BABEH

0000 B8 FA 37 MOV AX,37FAH0003 05 C4 82 ADD AX,82C4H

sum = BABEH in AX

Page 22: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

16-Bit Subtraction A1C9H-8315H 1EB4H

0000 B8 C9 A1 MOV AX,A1C9H0003 2D 15 83 SUB AX,8315H

difference = 1EB4H in AX

Page 23: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Sign extending bytes to words

5 = 00000101-5 = 11111011 = FBH16 bits-5 = 1111111111111011 = FFFBH

To add an 8-bit signed number to a 16-bit signed numberthe 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.

Page 24: Addition and Subtraction Instructions

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)

Page 25: Addition and Subtraction Instructions

Introduction to Computer Engineering by Richard E. Haskell

Add 8-bits to 16-bits (signed) FFFBH+123AH 1235H

Sign extend FBH+123AH

0000 B0 FB MOV AL,0FBH ;AL = -50002 98 CBW ;AX = -50002 05 3A 12 ADD AX,123AH AX = sum