Page 1 of 1

6809 8-bit Division

Posted: Wed Aug 08, 2012 1:19 pm
by Rink
Does anyone happen to have 6809 assembly language code for a simple 8-bit division routine?

It's been a very long time since I had to write one; and to be honest, it just doesn't sound like a fun way to spend the afternoon. Besides, there no sense re-inventing the wheel after all.

Cheers guys.

Re: 6809 8-bit Division

Posted: Wed Aug 08, 2012 6:16 pm
by Sarah
Do you need a floating point solution?

If not, with integers you could simply do multiple SUB instructions, counting the iterations until the result is less than the divisor. Of course if the divisor is 2, 4, 8, 16 etc then it's dead easy as you just need to do one or more right shifts (ASR/LSR) instead.

Maybe something like this to divide A by B (leaving the result in B)?*

PSHS D
CLRB
LOOP SUBA 1,S
BCS END
INCB
BRA LOOP
END LDA ,S++

*Just a quick stab, not tested!

Re: 6809 8-bit Division

Posted: Wed Aug 08, 2012 6:31 pm
by Rink
Oops. Forgot to mention that yes, i only need integer division and that i need to extract the remainder.

Good thinking with SUBS though. A routine like that (with remainder calculation at the end) will be pretty quick to implement. i'm not overly concerned about performance; it won't be used much.

Cheers Sarah.

Re: 6809 8-bit Division

Posted: Wed Aug 08, 2012 6:44 pm
by Sarah
I guess if you change the last line to ADDA 1,S then you've got the remainder in A and just need to adjust the stack appropriately (LEAS 2,S) afterwards.

Or more simply:

PSHS B
CLRB
LOOP SUBA ,S
BCS END
INCB
BRA LOOP
END ADDA ,S+

Re: 6809 8-bit Division

Posted: Wed Aug 08, 2012 8:26 pm
by Rink
That works an absolute treat.

Who are you!?! Everytime I (or anyone else for that matter) pose a fairly difficult question, you're there with an informed answer or elegant solutions!!!

Thank you very much. You've saved me a lot of hassle - and even when i'd got to the end of my own routine, it wouldn't have been so concise.

Re: 6809 8-bit Division

Posted: Wed Aug 08, 2012 10:26 pm
by Sarah
LOL!

You're welcome! :D

Re: 6809 8-bit Division

Posted: Thu Aug 09, 2012 10:17 am
by sixxie
Not what you asked for, but I remembered adapting a specific unsigned divide-16-bit-by-10 here:

http://www.6809.org.uk/dragon/asm/printd.s

Could definitely do with more (any) comments, because I now have no clue how it works! But it was a fairly straightforward non-iterative algorithm found on the web somewhere, whittled down to make (best?) use of 6809 ops.

Re: 6809 8-bit Division

Posted: Mon Aug 13, 2012 7:50 pm
by snarkhunter
I just checked my old "Zaks/Labiaks" and, as I remembered, it does include some routines for 8/8 and 16/16 bit divisions. The former is a bit longer than what has already been proposed here since it uses ASLB/ROLA instructions, but I'm not sure about the actual speed of the code vs looped SUBs.

If you're at all interested, I may re-type this here within the next couple of days.

I owe so much to that book, which is the one that actually got me started with m/c programming, circa 1984...

Lionel