6809 8-bit Division

Hardware Hacking, Programming and Game Solutions/Cheats
Post Reply
Rink
Posts: 236
Joined: Mon Sep 05, 2011 7:01 pm

6809 8-bit Division

Post 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.
Sarah
Posts: 177
Joined: Wed Apr 13, 2011 3:36 pm
Contact:

Re: 6809 8-bit Division

Post 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!
Rink
Posts: 236
Joined: Mon Sep 05, 2011 7:01 pm

Re: 6809 8-bit Division

Post 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.
Sarah
Posts: 177
Joined: Wed Apr 13, 2011 3:36 pm
Contact:

Re: 6809 8-bit Division

Post 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+
Rink
Posts: 236
Joined: Mon Sep 05, 2011 7:01 pm

Re: 6809 8-bit Division

Post 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.
Sarah
Posts: 177
Joined: Wed Apr 13, 2011 3:36 pm
Contact:

Re: 6809 8-bit Division

Post by Sarah »

LOL!

You're welcome! :D
sixxie
Posts: 1346
Joined: Fri Jul 18, 2008 8:36 am
Location: Hertfordshire
Contact:

Re: 6809 8-bit Division

Post 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.
User avatar
snarkhunter
Posts: 241
Joined: Fri Apr 03, 2009 7:16 pm
Location: France

Re: 6809 8-bit Division

Post 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
Post Reply