[Coco] Divide by 10 on a Motorola MC6809(E)

johnmarkmelanie at gmail.com johnmarkmelanie at gmail.com
Tue Mar 19 23:52:41 EDT 2024


All,

 

I saw a trick on how to multiply by 10 on a microcontroller without a
multiply instruction.

Results=a*2+a*8

Results=a<<1+a<<3 

Where "<<" is the shift left operator.

Ex: 10*10=10*8+10*2=80+20=100

Ok! So far so good, but how do we divide by 10?

 

Steps on how to do an unsigned divide by 10:

Step 1) Take a binary number and convert it to (packed or non-packed) Binary
Coded Decimal (BCD) or base 10.

Step 2) Shift left by one digit. Save the remainder if you like.

Step 3) Convert it back to base 2.

 

Q) So how do you convert to base 10 without the usual divide and modulus
commands?

A) Use the BCD add method.

 

Ex 100 = 0x64 = 0b01100100

Bit0=0 so don't do a BCD add of 1

Bit1=0 so don't do a BCD add of 2

Bit2=1 so do a BCD add of 4

Bit3=0 so don't do a BCD add of 8

Bit4=0 so don't do a BCD add of 16

Bit5=1 so do a BCD add of 32

Bit6=1 so do a BCD add of 64

Bit0=7 so don't do a BCD add of 128

 

BCD add 4, 32, 64 = 100 = 0x0100 or 0x01 0x00 0x00

To convert to ASC 0x31 0x30 0x30 = "100" just add (or) or 0x30 to each byte.

 

Q) So how do you convert base 10 to base 2?

A) See below

 

Conver 100 to binary

Results = digit0 + digit1*10 + digit2*100

 

So now you know how to do an unsigned divide by 10 on the MC6809(E)

 

So how about signed numbers?

Idea 1: Use the two's complement method or the neg command on the binary
number.

Idea 2: Use the ten's complement method on the base 10 number.

 

Note 1: Two's complement = one's complement + 1

Note 2: Ten's complement = nine's complement + 1

 

Note: BCD math is not needed to do a nine's complement.

Given: X=0 to 9

Nine's complement = 9-X

0 => 9

1 => 8

2 => 7

3 => 6

4 => 5

5 => 4

6 => 3

7 => 2

8 => 1

9 => 0

 

Ex: Make 01 negative.

Do a nine's complement: 01 => 98

Now add 1: 98 + 01 = 99

So, 99 can be the same as -1 in BCD.

 

John Mark Mobley

(847) 409-8604

 <mailto:johnmarkmelanie at gmail.com> johnmarkmelanie at gmail.com

LinkedIn profile:   <https://www.linkedin.com/in/john-mark-mobley>
https://www.linkedin.com/in/john-mark-mobley

Skype:  john.mark.mobley

Personal website:   <https://johnmarkmobley.wixsite.com/johnmarkmobley>
https://johnmarkmobley.wixsite.com/johnmarkmobley

 



More information about the Coco mailing list