Similar principle, but with some DIVs instead of shifts.
For a 10 base number you need a combination of divisions and modulus (DIV.L stores modulo in upper word, exponent in lower word). In theory, to convert the number 2348:
DIVU would fail to work properly with numbers larger than 655350 due to the result being only 16-bit, however. (DIVS would fail with larger than 327670) There's also the fact that it takes about 140 cycles for each division (whether this is an issue or not depends on the situation, for one or two small numbers in a HUD it probably isn't much of a problem).
An alternative if you need this to work with larger numbers is to check each bit individually and add them, but doing the additions in BCD, then you can just treat the BCD result as a hexadecimal number. This is faster than divisions (especially if you optimize using look-up tables to process multiple bits at once) and doesn't have issues with large numbers, it's way less obvious and trivial, however.
A solution for a fixed digit score counter was mentioned over at AssemberGames:
Store a string of '0' chars in RAM ("00000000"), and increment the bottom byte for each point awarded, then iterate each digit checking for overflow (clamp to 9 and add 1 to the next char, etc).
Obviously it's very primitive and only serves a specific purpose, but since I'm looking to create a fixed-digit score counter at some point the idea appealed to me. It's also branch heavy but I wonder how it performs against a load of DIVs. It also frees you of longword length numbers, if you want ridiculously high scores in the trillions (would need support for adding 1, 10, 100, 1000, 10000, 100000, 1000000, etc, though).
For the decimal conversion you can also check out my Phantasy Star disassemblies on RHDN. I found the algorithm on PSII and IV; they have the same logic and use an array of 1, 10, 100, etc., which are the number of digits for the conversion. Before converting, it loads the number of digits and then stores the number of digits in the register for a loop, thus it's 0-based; however you should read the PSII one since I put comments there. Hope it will help a bit. You can search for words like 'convert' or 'conversion' and you should be able to find the subroutine easily
BigEvilCorporation wrote:A solution for a fixed digit score counter was mentioned over at AssemberGames:
Store a string of '0' chars in RAM ("00000000"), and increment the bottom byte for each point awarded, then iterate each digit checking for overflow (clamp to 9 and add 1 to the next char, etc).
Obviously it's very primitive and only serves a specific purpose, but since I'm looking to create a fixed-digit score counter at some point the idea appealed to me. It's also branch heavy but I wonder how it performs against a load of DIVs. It also frees you of longword length numbers, if you want ridiculously high scores in the trillions (would need support for adding 1, 10, 100, 1000, 10000, 100000, 1000000, etc, though).
The problem isn't about how to manage the score but, how do convert it from integer to ASCII which is the way the font system works in "The Big Evil Corporation Framework"(Hope you don't mind i named your framework ). I am going to use overflows and branches to manage the score part. However if i pass an integer value to DrawTextPlaneA nothing shows up and that's because it's expecting an ASCII value. I managed to convert the hex function i was provided to display only numeric values, but sadly i am not around my development PC to test it currently. That is unless I'm misunderstanding something.....