Hcs12 arithmetic
Download
1 / 64

HCS12 Arithmetic - PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on

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 LDD 0,X ADDD #1 STD 0,X RTS ; 2+ ( n -- n+2 ) TWOP LDD 0,X ADDD #2

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' HCS12 Arithmetic' - gaetan


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Hcs12 arithmetic

HCS12 Arithmetic

Lecture 3.3


68hc12 arithmetic
68HC12 Arithmetic

  • Addition and Subtraction

  • Shift and Rotate Instructions

  • Multiplication

  • Division



Hcs12 code for 1 2
HCS12 code for 1+, 2+

; 1+ ( n -- n+1 )

ONEP

LDD 0,X

ADDD #1

STD 0,X

RTS

; 2+ ( n -- n+2 )

TWOP

LDD 0,X

ADDD #2

STD 0,X

RTS


Hcs12 code for 1 21
HCS12 code for 1-, 2-

; 1- ( n -- n-1 )

ONEP

LDD 0,X

SUBD #1

STD 0,X

RTS

; 2- ( n -- n-2 )

TWOP

LDD 0,X

SUBD #2

STD 0,X

RTS





68hc12 arithmetic1
68HC12 Arithmetic

  • Addition and Subtraction

  • Shift and Rotate Instructions

  • Multiplication

  • Division


Logic shift left lsl lsla lslb

C

Bit

7

6

5

4

3

2

1

0

1

1 0 1 0 0 1 0 1

Logic Shift LeftLSL, LSLA, LSLB

0

C

Bit

7

6

5

4

3

2

1

0

7

6

5

4

3

2

1

0

1

1 0 1 0 0 1 0 1

0

1 0 1 0 0 1 0 1

A

B

LSLD


Logic Shift RightLSR, LSRA, LSRB

C

Bit

7

6

5

4

3

2

1

0

0

1 0 1 0 0 1 0 1

1

C

Bit

7

6

5

4

3

2

1

0

7

6

5

4

3

2

1

0

0

1 0 1 0 0 1 0 1

1 0 1 0 0 1 0 1

1

A

B

LSRD


Arithmetic Shift RightASR, ASRA, ASRB

C

1 0 1 0 0 1 0 1

1


Rotate left rol rola rolb
Rotate LeftROL, ROLA, ROLB

C

Bit

7

6

5

4

3

2

1

0

1

1 0 1 0 0 1 0 1


Rotate RightROR, RORA, RORB

C

Bit

7

6

5

4

3

2

1

0

1 0 1 0 0 1 0 1

1



68hc12 arithmetic2
68HC12 Arithmetic

  • Addition and Subtraction

  • Shift and Rotate Instructions

  • Multiplication

  • Division



Binary multiplication1

9 C = 156

Binary Multiplication

13

x 12

26

13

156

1101

1100

0000

0000

1101

1101

10011100



Hex Multiplication

Dec

Hex

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

61

x 90

5490


Hex Multiplication

; 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




16-Bit Hex Multiplication

31A4

x1B2C

253B0

4 x C = 30

A x C = 78 + 3 = 7B

1 x C = C + 7 = 13

3 x C = 24 + 1 = 25


16-Bit Hex Multiplication

31A4

x1B2C

253B0

6348

2 x 4 = 8

2 x A = 14

1 x 2 = 2 + 1 = 3

2 x 3 = 6


16-Bit Hex Multiplication

31A4

x1B2C

253B0

6348

2220C

4 x B = 2C

A x B = 6E + 2 = 70

1 x B = B + 7 = 12

3 x B = 21 + 1 = 22


16-Bit Hex Multiplication

31A4

x1B2C

253B0

6348

2220C

31A4

0544D430

ORG $4000

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

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

4006 13 EMUL;Y:D = D x Y



Multiply instructions
Multiply Instructions

Note that EMUL and MUL are unsigned multiplies

EMULS is used to multiply signed numbers


Unsigned multiplication
Unsigned Multiplication

Hex

Dec

FFF8

x 0008

7FFC0

65528

x 8

524224

52422410 = 7FFC016


FFF8

x 0008

7FFC0


Unsigned multiplication1
Unsigned Multiplication

Hex

Dec

FFF8

x 0008

7FFC0

65528

x 8

524224

52422410 = 7FFC016

But FFF8 = 1111111111111000 can be a signed number

2’s comp = 0000000000001000 = $0008 = 810

Therefore, FFF8 can represent -8


Signed multiplication
Signed Multiplication

Dec

6410 = 0000004016

= 0000 0000 0000 0000 0000 0000 0100 0000

2’s comp = 1111 1111 1111 1111 1111 1111 0100 0000

= $FFFFFF40

-8

x 8

-64

Therefore, for signed multiplication

$FFF8 x $0008 = $FFFFFF40

and not $7FFC0

The EMULS instruction performs SIGNED multiplication


Signed multiplication1
Signed Multiplication

; 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



Multiplication1
Multiplication

Note that EMUL is a 16 x 16 multiply that produces

a 32-bit unsigned product.

If the product fits into 16-bits, then it produces the

correct SIGNED product.


-8

x 8

-64 = $FFC0

FFF8

x 0008

7FFC0

Correct 16-bit

SIGNED result


Multiplication2
Multiplication

Even MUL can be used for an

8 x 8 = 8 SIGNED multiply


Note: A x B = A:B

B contains the correct

8-bit SIGNED value


68hc12 arithmetic3
68HC12 Arithmetic

  • Addition and Subtraction

  • Shift and Rotate Instructions

  • Multiplication

  • Division


Binary division

D

C 9C

Binary Division

1

1

0

1

1100

10011100

1100

1

0111

1100

0

0011

0000

0110

0

1100

0000


Hex Division

A

C

EE BC2F

B28

9A

F

C x E = A8

C x E = A8 + A = B2


Hex Division

A

C

EE BC2F

B28

9A

F

94C

63

Dividend = BC2F

Divisor = EE

Quotient = CA

Remainder = 63

A x E = 8C

A x E = 8C + 8 = 94


Hex Division

A

C

EE BC2F

B28

9A

F

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



Y:D/X => Y

Remainder in D


Divisor may be too small

C

o

n

d

i

t

i

o

n

c

o

d

e

r

e

g

i

s

t

e

r

S

X

H

I

N

Z

V

C

Divisor may be too small

11313 rem 85

EE FFBC2F

Quotient does not fit in Y

Overflow bit, V, will be set


C

o

n

d

i

t

i

o

n

c

o

d

e

r

e

g

i

s

t

e

r

S

X

H

I

N

Z

V

C

Note overflow bit, V, set

N and Z are undefined

Y and D unchanged


C

o

n

d

i

t

i

o

n

c

o

d

e

r

e

g

i

s

t

e

r

S

X

H

I

N

Z

V

C

Note divide by zero sets

carry bit, C

N, Z, and V are undefined

Y and D unchanged



Y:D/X => Y

Remainder in D

Note symmetric division

Truncation toward zero

Sign of remainder = sign of dividend


Y:D/X => Y

Remainder in D

Note symmetric division

Truncation toward zero

Sign of remainder = sign of dividend


Symetric Division (truncation toward zero)

Floored Division (truncation toward minus infinity)


D = $0001

X = $0002

FDIV => X = $8000

D = $0000

1

2

= 0.5 = 0.1000000000000000

2

= 0.8000

16


D = $1234

X = $0010

IDIV => X = $0123

D = $0004


D = $FFE6

X = $0007

IDIVS => X = $FFFD

D = $FFFB

-26

7

= -3 remainder = -5


ad