### BCD division by 2

Posted:

**Tue Oct 14, 2008 5:06 pm**Whats the simplest way to divide a single BCD number by 2 or 4 ?

BCD is a special mode in the 6502 that holds decimal numbers in Bytes as two 4 bit digits. So that maximum value is 99 and minimum is 00.

In BCD mode only ADC and SBC behave appropriately like setting carry if the result of the ADC takes the number over 99.

I can't use LSR since it doesn't operate in the right way. For example 99/2 in BCD mode using LSR would give me 4C which is clearly wrong, and should be 49.

I tried using a simple loop like this preloading A with the BCD number..

But it fails because whilst the accumulated number in X is correct, it is not BCD encoded.

The only way i can see is this..

But this seems extremely long winded just to divide the BCD number by 2 even in its optimised form, any thoughts?

BCD is a special mode in the 6502 that holds decimal numbers in Bytes as two 4 bit digits. So that maximum value is 99 and minimum is 00.

In BCD mode only ADC and SBC behave appropriately like setting carry if the result of the ADC takes the number over 99.

I can't use LSR since it doesn't operate in the right way. For example 99/2 in BCD mode using LSR would give me 4C which is clearly wrong, and should be 49.

I tried using a simple loop like this preloading A with the BCD number..

Code: Select all

```
LDX #00
SED
SEC
loop1 INX
SBC #02
BCS loop1
CLD
```

The only way i can see is this..

Code: Select all

```
STA BCDNumber
SED
LDA #00
STA Result
CLC
loop1 LDA Result
ADC #00
STA Result
LDA BCDNumber
SEC
SBC #02
STA BCDNumber
BCS loop1
CLD
```