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!
Unsigned 24-bit integer division in 6502 assembler
- Negative Charge
- Posts: 93
- Joined: Sat Jan 16, 2021 1:35 pm
- Contact:
Unsigned 24-bit integer division in 6502 assembler
Website: https://negativecharge.github.io/
Twitter: @Charge_Negative (https://mobile.twitter.com/charge_negative)
YouTube: https://youtube.com/channel/UCy7-RCobl0KsVJVTeO82roA
GitHub: https://github.com/NegativeCharge/
Twitter: @Charge_Negative (https://mobile.twitter.com/charge_negative)
YouTube: https://youtube.com/channel/UCy7-RCobl0KsVJVTeO82roA
GitHub: https://github.com/NegativeCharge/
Re: Unsigned 24-bit integer division in 6502 assembler
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.)
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.)
- TobyLobster
- Posts: 618
- Joined: Sat Aug 31, 2019 7:58 am
- Contact:
Re: Unsigned 24-bit integer division in 6502 assembler
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
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
- Negative Charge
- Posts: 93
- Joined: Sat Jan 16, 2021 1:35 pm
- Contact:
Re: Unsigned 24-bit integer division in 6502 assembler
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.
Website: https://negativecharge.github.io/
Twitter: @Charge_Negative (https://mobile.twitter.com/charge_negative)
YouTube: https://youtube.com/channel/UCy7-RCobl0KsVJVTeO82roA
GitHub: https://github.com/NegativeCharge/
Twitter: @Charge_Negative (https://mobile.twitter.com/charge_negative)
YouTube: https://youtube.com/channel/UCy7-RCobl0KsVJVTeO82roA
GitHub: https://github.com/NegativeCharge/
Re: Unsigned 24-bit integer division in 6502 assembler
The fractional part would be LSB and 31 of that is what you meant
- Negative Charge
- Posts: 93
- Joined: Sat Jan 16, 2021 1:35 pm
- Contact:
Re: Unsigned 24-bit integer division in 6502 assembler
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).
Website: https://negativecharge.github.io/
Twitter: @Charge_Negative (https://mobile.twitter.com/charge_negative)
YouTube: https://youtube.com/channel/UCy7-RCobl0KsVJVTeO82roA
GitHub: https://github.com/NegativeCharge/
Twitter: @Charge_Negative (https://mobile.twitter.com/charge_negative)
YouTube: https://youtube.com/channel/UCy7-RCobl0KsVJVTeO82roA
GitHub: https://github.com/NegativeCharge/