HCS12 Arithmetic

Preview:

DESCRIPTION

HCS12 Arithmetic. Lecture 3.3. 68HC12 Arithmetic. Addition and Subtraction Shift and Rotate Instructions Multiplication Division. Addition and Subtraction. HCS12 code for 1+, 2+. ;1+ ( n -- n+1 ) ONEP LDD0,X ADDD#1 STD0,X RTS ;2+ ( n -- n+2 ) TWOP LDD0,X ADDD#2 - PowerPoint PPT Presentation

Citation preview

HCS12 Arithmetic

Lecture 3.3

68HC12 Arithmetic

• Addition and Subtraction• Shift and Rotate Instructions

• Multiplication

• Division

Addition and SubtractionTable 3.1 Addition and Subtraction InstructionsAddition Instructions:

Mnemonic FunctionABA Add B to AADDA Add memory to AADDB Add memory to BADCA Add memory with carry to AADCB Add memory with carry to BADDD Add memory to DABX Add B to XABY Add B to Y

Subtraction Instructions:

Mnemonic FunctionSBA Subtract B from ASUBA Subtract memory from ASUBB Subtract memory from BSBCA Subtract memory with borrow from ASBCB Subtract memory with borrow from BSUBD Subtract memory from D

Table 3.2 Increment and Decrement InstructionsIncrement Instructions:

Mnemonic FunctionINC Increment memory by 1INCA Increment A by 1INCB Increment B by 1INS Increment SP by 1INX Increment X by 1INY Increment Y by 1

Decrement Instructions:

Mnemonic FunctionDEC Decrement memory by 1DECA Decrement A by 1DECB Decrement B by 1DES Decrement SP by 1DEX Decrement X by 1DEY Decrement Y by 1

HCS12 code for 1+, 2+ ; 1+ ( n -- n+1 )ONEP

LDD 0,XADDD #1STD 0,XRTS

; 2+ ( n -- n+2 )TWOP

LDD 0,XADDD #2STD 0,XRTS

HCS12 code for 1-, 2- ; 1- ( n -- n-1 )ONEP

LDD 0,XSUBD #1STD 0,XRTS

; 2- ( n -- n-2 )TWOP

LDD 0,XSUBD #2STD 0,XRTS

Double Numbers

HI-word <---- top of stackLO-word

D+ ( d1 d2 -- d3 ) ( d3 = d1+d2 ) Add two double numbers leaving a double sum.

D- ( d1 d2 -- d3 ) ( d3 = d1-d2 ) Subtract two double numbers leaving a double difference.

d1

d2

d3

X X+1 X+2 X+3

X+4 X+5 X+6 X+7

; D+ ( d1 d2 -- d3 )DPLUS

LDAA 7,XADDA 3,XSTAA 7,XLDAA 6,XADCA 2,XSTAA 6,XLDAA 5,XADCA 1,XSTAA 5,XLDAA 4,XADCA 0,XSTAA 4,XLEAX 4,XRTS

Adding Double Numbers

d1

d2

d3

X X+1 X+2 X+3

X+4 X+5 X+6 X+7

Subtracting Double Numbers

; D- ( d1 d2 -- d3 )DMINUS

LDAA 7,XSUBA 3,XSTAA 7,XLDAA 6,XSBCA 2,XSTAA 6,XLDAA 5,XSBCA 1,XSTAA 5,XLDAA 4,XSBCA 0,XSTAA 4,XLEAX 4,XRTS

68HC12 Arithmetic

• Addition and Subtraction• Shift and Rotate Instructions

• Multiplication

• Division

Table 3.6 Shift and Rotate InstructionsLogical Shift Instructions:

Mnemonic FunctionLSL Logic Shift Left MemoryLSLA Logic Shift Left ALSLB Logic Shift Left BLSLD Logic Shift Left DLSR Logic Shift Right MemoryLSRA Logic Shift Right ALSRB Logic Shift Right BLSRD Logic Shift Right D

Arithmetic Shift Instructions:

Mnemonic FunctionASL Arithmetic Shift Left MemoryASLA Arithmetic Shift Left AASLB Arithmetic Shift Left BASLD Arithmetic Shift Left DASR Arithmetic Shift Right MemoryASRA Arithmetic Shift Right AASRB Arithmetic Shift Right B

Rotate Instructions:

Mnemonic FunctionROL Rotate Left Memory Through CarryROLA Rotate Left A Through CarryROLB Rotate Left B Through CarryROR Rotate Right Memory Through CarryRORA Rotate Right A Through CarryRORB Rotate Right B Through Carry

Logic Shift LeftLSL, LSLA, LSLB

01 0 1 0 0 1 0 11C 7 6 5 4 3 2 1 0Bit

1 0 1 0 0 1 0 11 0C 7 6 5 4 3 2 1 0Bit

1 0 1 0 0 1 0 1

7 6 5 4 3 2 1 0

A B

LSLD

Logic Shift RightLSR, LSRA, LSRB

1 0 1 0 0 1 0 1 10

C7 6 5 4 3 2 1 0Bit

1 0 1 0 0 1 0 1 10

C7 6 5 4 3 2 1 0Bit

1 0 1 0 0 1 0 1

7 6 5 4 3 2 1 0

A B

LSRD

Arithmetic Shift RightASR, ASRA, ASRB

1 0 1 0 0 1 0 1 1

C

Rotate LeftROL, ROLA, ROLB

1 0 1 0 0 1 0 11C 7 6 5 4 3 2 1 0Bit

Rotate RightROR, RORA, RORB

1 0 1 0 0 1 0 1 1C7 6 5 4 3 2 1 0Bit

2*, 2/, and U2/; 2* ( n -- 2*n )TWOT

ASL 1,X ;arith shift leftROL 0,XRTS

; 2/ ( n -- n/2 )TWOS

ASR 0,X ;arith shift rightROR 1,XRTS

; U2/ ( u -- u/2 )U2S

LSR 0,XROR 1,X ;logic shift rightRTS

68HC12 Arithmetic

• Addition and Subtraction• Shift and Rotate Instructions

• Multiplication

• Division

Binary Multiplication

Table 10.8Binary Multiplication Table

0 10 0 01 0 1

Binary Multiplication

13x 12 26 13 156

1101 1100 0000 0000 1101 110110011100

9 C = 156

Table 10.9Hexadecimal Multiplication Table

0 1 2 3 4 5 6 7 8 9 A B C D E F0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

Hex Multiplication

Hex MultiplicationTable 10.9

Hexadecimal Multiplication Table0 1 2 3 4 5 6 7 8 9 A B C D E F

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

61x 905490

3D x 5A 262 A x D = 82, A x 3 = 1E + 8 = 26 131 5 x D = 41, 5 x 3 = F + 4 = 13 157216 = 549010

Dec Hex

Hex Multiplication

product = $1572 is in D = A:B

; multiply 8 x 8 = 16

=00004000 ORG $4000

4000 86 3D LDAA #$3D

4002 C6 5A LDAB #$5A

4004 12 MUL

product = $1572 is in D = A:B

MultiplicationTable 3.3 Multiply Instructions

Mnemonic Function OperationEMUL 16x16 multiply (unsigned) (D) x (Y) => Y:DEMULS 16x16 multiply (signed) (D) x (Y) => Y:DMUL 8x8 multiply (unsigned) (A) x (B) => A:B

16-Bit Hex MultiplicationTable 10.9

Hexadecimal Multiplication Table0 1 2 3 4 5 6 7 8 9 A B C D E F

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

31A4 x1B2C 253B0

4 x C = 30A x C = 78 + 3 = 7B1 x C = C + 7 = 133 x C = 24 + 1 = 25

16-Bit Hex MultiplicationTable 10.9

Hexadecimal Multiplication Table0 1 2 3 4 5 6 7 8 9 A B C D E F

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

31A4 x1B2C 253B0 6348

2 x 4 = 82 x A = 141 x 2 = 2 + 1 = 32 x 3 = 6

16-Bit Hex MultiplicationTable 10.9

Hexadecimal Multiplication Table0 1 2 3 4 5 6 7 8 9 A B C D E F

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

31A4 x1B2C 253B0 6348 2220C

4 x B = 2CA x B = 6E + 2 = 701 x B = B + 7 = 123 x B = 21 + 1 = 22

16-Bit Hex MultiplicationTable 10.9

Hexadecimal Multiplication Table0 1 2 3 4 5 6 7 8 9 A B C D E F

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

31A4 x1B2C 253B0 6348 2220C 31A4

0544D430ORG $4000

4000 CC 31A4 LDD #$31A4 ;D = $31A4

4003 CD 1B2C LDY #$1B2C ;Y = $1B2C

4006 13 EMUL ;Y:D = D x Y

Y:D = 0544D430

Multiply InstructionsTable 3.3 Multiply Instructions

Mnemonic Function OperationEMUL 16x16 multiply (unsigned) (D) x (Y) => Y:DEMULS 16x16 multiply (signed) (D) x (Y) => Y:DMUL 8x8 multiply (unsigned) (A) x (B) => A:B

Note that EMUL and MUL are unsigned multiplies

EMULS is used to multiply signed numbers

Unsigned MultiplicationDec Hex

65528x 8524224

FFF8x 0008 7FFC0 52422410 = 7FFC016

FFF8x 0008 7FFC0

Unsigned MultiplicationDec Hex

65528x 8524224

FFF8x 0008 7FFC0 52422410 = 7FFC016

But FFF8 = 1111111111111000 can be a signed number 2’s comp = 0000000000001000 = $0008 = 810

Therefore, FFF8 can represent -8

Signed MultiplicationDec

-8 x 8 -64

6410 = 0000004016

= 0000 0000 0000 0000 0000 0000 0100 00002’s comp = 1111 1111 1111 1111 1111 1111 0100 0000 = $FFFFFF40

Therefore, for signed multiplication$FFF8 x $0008 = $FFFFFF40and not $7FFC0

The EMULS instruction performs SIGNED multiplication

Signed Multiplication

product = $FFFFFFC0 is in Y:D

; EMULS signed 16 x 16 = 32

=00004000 ORG $4000

4000 CC FFF8 LDD #$FFF8 ;D = $FFF8

4003 CD 0008 LDY #$0008 ;Y = $0008

4006 1813 EMULS ;Y:D = D x Y

product = $FFFFFFC0 is in Y:D

MultiplicationTable 3.3 Multiply Instructions

Mnemonic Function OperationEMUL 16x16 multiply (unsigned) (D) x (Y) => Y:DEMULS 16x16 multiply (signed) (D) x (Y) => Y:DMUL 8x8 multiply (unsigned) (A) x (B) => A:B

Note that EMUL is a 16 x 16 multiply that producesa 32-bit unsigned product.If the product fits into 16-bits, then it produces thecorrect SIGNED product.

FFF8x 0008 7FFC0

-8x 8 -64 = $FFC0

Correct 16-bitSIGNED result

MultiplicationTable 3.3 Multiply Instructions

Mnemonic Function OperationEMUL 16x16 multiply (unsigned) (D) x (Y) => Y:DEMULS 16x16 multiply (signed) (D) x (Y) => Y:DMUL 8x8 multiply (unsigned) (A) x (B) => A:B

Even MUL can be used for an8 x 8 = 8 SIGNED multiply

Note: A x B = A:BB contains the correct 8-bit SIGNED value

68HC12 Arithmetic

• Addition and Subtraction• Shift and Rotate Instructions

• Multiplication

• Division

Binary Division

1001110011001

110001111

1

110000110

0

000001100

1

11000000

DC 9C

Hex DivisionTable 10.9

Hexadecimal Multiplication Table0 1 2 3 4 5 6 7 8 9 A B C D E F

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

EE BC2FC

C x E = A8C x E = A8 + A = B2

B28 9AF

A

Hex DivisionTable 10.9

Hexadecimal Multiplication Table0 1 2 3 4 5 6 7 8 9 A B C D E F

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

EE BC2FC

A x E = 8CA x E = 8C + 8 = 94

B28 9AF

A

94C 63

Dividend = BC2FDivisor = EEQuotient = CARemainder = 63

Hex DivisionTable 10.9

Hexadecimal Multiplication Table0 1 2 3 4 5 6 7 8 9 A B C D E F

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 2 3 4 5 6 7 8 9 A B C D E F2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E3 9 C F 12 15 18 1B 1E 21 24 27 2A 2D4 10 14 18 1C 20 24 28 2C 30 34 38 3C5 19 1E 23 28 2D 32 37 3C 41 46 4B6 24 2A 30 36 3C 42 48 4E 54 5A7 31 38 3F 46 4D 54 5B 62 698 40 48 50 58 60 68 70 789 51 5A 63 6C 75 7E 87A 64 6E 78 82 8C 96B 79 84 8F 9A A5C 90 9C A8 B4D A9 B6 C3E C4 D2F E1

EE BC2FC

B28 9AF

A

94C 63

=00004000 ORG $4000

4000 CD 0000 LDY #$0000

4003 CC BC2F LDD #$BC2F

4006 CE 00EE LDX #$00EE

4009 11 EDIV ;BC2F/EE = CA rem 63

DivisionTable 3.4 Divide Instructions

Mnemonic Function OperationEDIV 32x16 divide (unsigned) (Y:D) / (X) => Y

Remainder => DEDIVS 32x16 divide (signed) (Y:D) / (X) => Y

Remainder => DFDIV 16x16 fractional divide (D) / (X) => X

Remainder => DIDIV 16x16 integer divide (unsigned) (D) / (X) => X

Remainder => DIDIVS 16x16 integer divide (signed) (D) / (X) => X

Remainder => D

Y:D/X => YRemainder in D

Divisor may be too small

EE FFBC2F11313 rem 85

Quotient does not fit in YOverflow bit, V, will be set

S X

H

I N

Z

V

C Condition code regis ter

S X

H

I N

Z

V

C Condition code regis ter

Note overflow bit, V, setN and Z are undefined

Y and D unchanged

S X

H

I N

Z

V

C Condition code regis ter

Note divide by zero setscarry bit, C

N, Z, and V are undefinedY and D unchanged

DivisionTable 3.4 Divide Instructions

Mnemonic Function OperationEDIV 32x16 divide (unsigned) (Y:D) / (X) => Y

Remainder => DEDIVS 32x16 divide (signed) (Y:D) / (X) => Y

Remainder => DFDIV 16x16 fractional divide (D) / (X) => X

Remainder => DIDIV 16x16 integer divide (unsigned) (D) / (X) => X

Remainder => DIDIVS 16x16 integer divide (signed) (D) / (X) => X

Remainder => D

Y:D/X => YRemainder in D

Note symmetric divisionTruncation toward zeroSign of remainder = sign of dividend

Y:D/X => YRemainder in D

Note symmetric divisionTruncation toward zeroSign of remainder = sign of dividend

Symetric Division (truncation toward zero)

Dividend Divisor Quotient Remainder

26 7 3 5

-26 7 -3 -5

26 -7 -3 5

-26 -7 3 -5

Floored Division (truncation toward minus infinity)Dividend Divisor Quotient Remainder

26 7 3 5

-26 7 -4 2

26 -7 -4 -2

-26 -7 3 -5

Table 3.4 Divide InstructionsMnemonic Function OperationEDIV 32x16 divide (unsigned) (Y:D) / (X) => Y

Remainder => DEDIVS 32x16 divide (signed) (Y:D) / (X) => Y

Remainder => DFDIV 16x16 fractional divide (D) / (X) => X

Remainder => DIDIV 16x16 integer divide (unsigned) (D) / (X) => X

Remainder => DIDIVS 16x16 integer divide (signed) (D) / (X) => X

Remainder => D

D = $0001X = $0002

FDIV => X = $8000 D = $0000

12

= 0.5 = 0.10000000000000002

= 0.800016

Table 3.4 Divide InstructionsMnemonic Function OperationEDIV 32x16 divide (unsigned) (Y:D) / (X) => Y

Remainder => DEDIVS 32x16 divide (signed) (Y:D) / (X) => Y

Remainder => DFDIV 16x16 fractional divide (D) / (X) => X

Remainder => DIDIV 16x16 integer divide (unsigned) (D) / (X) => X

Remainder => DIDIVS 16x16 integer divide (signed) (D) / (X) => X

Remainder => D

D = $1234X = $0010

IDIV => X = $0123 D = $0004

Table 3.4 Divide InstructionsMnemonic Function OperationEDIV 32x16 divide (unsigned) (Y:D) / (X) => Y

Remainder => DEDIVS 32x16 divide (signed) (Y:D) / (X) => Y

Remainder => DFDIV 16x16 fractional divide (D) / (X) => X

Remainder => DIDIV 16x16 integer divide (unsigned) (D) / (X) => X

Remainder => DIDIVS 16x16 integer divide (signed) (D) / (X) => X

Remainder => D

D = $FFE6X = $0007

IDIVS => X = $FFFD D = $FFFB

-26 7

= -3 remainder = -5

Table 3.4 Divide InstructionsMnemonic Function OperationEDIV 32x16 divide (unsigned) (Y:D) / (X) => Y

Remainder => DEDIVS 32x16 divide (signed) (Y:D) / (X) => Y

Remainder => DFDIV 16x16 fractional divide (D) / (X) => X

Remainder => DIDIV 16x16 integer divide (unsigned) (D) / (X) => X

Remainder => DIDIVS 16x16 integer divide (signed) (D) / (X) => X

Remainder => D

Recommended