Unsigned 24-bit integer division in 6502 assembler

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
User avatar
Negative Charge
Posts: 93
Joined: Sat Jan 16, 2021 1:35 pm
Contact:

Unsigned 24-bit integer division in 6502 assembler

Post by Negative Charge »

Hi,

Would anyone have example 6502 assembler code for dividing a 24-bit unsigned number by an 8-bit unsigned number (which can be a power of two if that makes it easier - for my purposes a value of 32 would work and I’m not too worried about remainders)?

I’ve seen examples of 16-bit division by an 8-bit unsigned number which seem fairly straightforward but can’t get my head around how to extend this to 24-bits and I’ve yet to locate a comprehensive example.

Any help would be most appreciated. Thanks!
User avatar
BigEd
Posts: 6261
Joined: Sun Jan 24, 2010 10:24 am
Location: West Country
Contact:

Re: Unsigned 24-bit integer division in 6502 assembler

Post by BigEd »

Dividing by a power of two is spectacularly easy: it's just a series of right-shifts. If you want a rounded result, I think you just increment the number before the final shift.

To right-shift a multibyte value, you LSR the leftmost (most significant) byte and then ROR the bytes one by one down to the rightmost byte.

To increment a multibyte value, set the carry and then ADC #0 to the rightmost (least significant) byte, and then the next byte up, and so on to the leftmost. (Or something a little more complicated which involves checking each time and only incrementing further bytes if you got zero.)
User avatar
TobyLobster
Posts: 618
Joined: Sat Aug 31, 2019 7:58 am
Contact:

Re: Unsigned 24-bit integer division in 6502 assembler

Post by TobyLobster »

This divides 24 bit number in a,b,c (a=LSB) by 32 (in the way BigEd suggests):

https://bbcmic.ro/#%7B%22v%22%3A1%2C%22 ... %5Cn%22%7D

Shifting the other way is a little quicker I think:
https://bbcmic.ro/#%7B%22v%22%3A1%2C%22 ... %5Cn%22%7D

Edit: If you need a generic 24 bit divide then this is 24 bit / 24 bit:

https://www.codebase64.org/doku.php?id= ... bit_result
User avatar
Negative Charge
Posts: 93
Joined: Sat Jan 16, 2021 1:35 pm
Contact:

Re: Unsigned 24-bit integer division in 6502 assembler

Post by Negative Charge »

Thank you both! That’s perfect. Speed isn’t an issue for the purpose I have in mind as it’s for a one time operation to retrieve and store the fractional part which I’ll then reuse.
User avatar
tricky
Posts: 7699
Joined: Tue Jun 21, 2011 9:25 am
Contact:

Re: Unsigned 24-bit integer division in 6502 assembler

Post by tricky »

The fractional part would be LSB and 31 of that is what you meant :)
User avatar
Negative Charge
Posts: 93
Joined: Sat Jan 16, 2021 1:35 pm
Contact:

Re: Unsigned 24-bit integer division in 6502 assembler

Post by Negative Charge »

tricky wrote: Thu Jan 04, 2024 2:29 pm The fractional part would be LSB and 31 of that is what you meant :)
Yes, sorry - wasn’t very well worded. I simply meant it’s only the answer from the division I’m interested in using (1/32th of the unsigned 24-bit input).
Post Reply

Return to “programming”