## Multiplying 8-bit number by n/10ths

5 posts / 0 new
Author
Message

You may recall brum103's excellent post in:
https://www.avrfreaks.net/index.p...
...where a divide 8 bit number by 10 algorithm is given as:

```; the input number must be in register "input"
; value/10 is in register "result"
; register temp1 and R1:R0 are clobbered
div10:
ldi temp1, 205
mul temp1, input
lsr R1
lsr R1
lsr R1
mov result, R1
ret ```

I've been trying and failing to extend this to multiplying an 8 bit number by n/10ths eg 178 * 9/10, or 36 * 2/10 or whatever.

I need:
- good accuracy - being out by 'a few' is OK, but spot on would be better!
- very high speed - at the expense of code size if required

My head's hurting - any ideas fellow Freaks?

There's a less accurate, but potentially more useful algorithm in that same thread where HWMNBM proposes:

```; the input number must be in register "input"
; value/10 is in register "result"
; register temp1 and R1:R0 are clobbered
div10:
ldi temp1, 26
mul temp1, input
mov result, R1
ret ```

I think it's potentially more useful because the answer in r1:r0 can be multiplied by (up to) 9 without overflowing r1:r0...
...but you need a speedy 12-bit by 4-bit algorithm to do that...
..so I'm not sure if that helps or not!

Quote:
ldi temp1, 26

you can multiply the "26" by 0..9 without overflow.

You can also use a table: 0; 26; 51; 77; 102 .... to get better precision

Klaus
********************************
Look at: www.megausb.de (German)
********************************

Thanks Klaus - it was 0100 this morning when I posted at the end of a hard night's developing/debugging and my brain must have been fried!

I just needed the clear thinking - I should go to bed earlier :)

It works fine, of course!

Thanks, Martin