Displaying 24, 32, 48 and 64-bit hexadecimal, signed and unsigned decimal values
While greater than 16-bit maths can be relatively straightforward with the right code, showing multi-word numbers as decimal values can be a little complicated.
These routines show how that can be done with a brute-force approach rather than by creating a complicated algorithm. This uses more program memory but keeps the principles simple and minimises the number of variables which need to be used.
The basic principle used here is to use a number of words which each hold four digits of the decimal number. Then one adds to the words values which represent each bit value of the hexadecimal number, keeping them as four digits. Overflow, when a word in the decimal number becomes larger than 9999 is easily handled.
The lower 16-bits can be handled directly which minimises the iterations needed to perform the additions to just 8 for 24-bit, 16 for 32-bit, 32 for 48-bit, but a hefty 48 for 64-bit..
Being a brute force approach there is nothing particularly complicated about the code. It handles integer numbers and two's complement signed numbers. Negation is achieved by inverting and adding one, with any overflow then handled.
Take a look at the 24-bit code first, and you should how the 32-bit and 48-bit versions are just extensions of what is used there.
You will probably notice that the values added to the decimal words halve for each bit. And that's the principle behind a traditional algorithm; set the value for the highest bit, halve it for the next bit, while looping until all bit values have been added.
It would be possible to extend the principle here to larger bit-size numbers using the same principles. It would just require more words to store the decimal value and more program memory to do the additions for the individual bits.
Memory use does increase with bit-size and the 48-bit code won't fit in an M2's memory, and the 64-bit code won't even fit in an X2. There is probably some scope for optimisation but the goal was something 'straight forward' which worked.
Apart from that; this code should work on any M2, X1 and X2 PICAXE and will also run within the PE6 simulator.
If there are any questions; please do ask. If you spot any errors or bugs; please let me know.
While greater than 16-bit maths can be relatively straightforward with the right code, showing multi-word numbers as decimal values can be a little complicated.
These routines show how that can be done with a brute-force approach rather than by creating a complicated algorithm. This uses more program memory but keeps the principles simple and minimises the number of variables which need to be used.
The basic principle used here is to use a number of words which each hold four digits of the decimal number. Then one adds to the words values which represent each bit value of the hexadecimal number, keeping them as four digits. Overflow, when a word in the decimal number becomes larger than 9999 is easily handled.
The lower 16-bits can be handled directly which minimises the iterations needed to perform the additions to just 8 for 24-bit, 16 for 32-bit, 32 for 48-bit, but a hefty 48 for 64-bit..
Being a brute force approach there is nothing particularly complicated about the code. It handles integer numbers and two's complement signed numbers. Negation is achieved by inverting and adding one, with any overflow then handled.
Take a look at the 24-bit code first, and you should how the 32-bit and 48-bit versions are just extensions of what is used there.
You will probably notice that the values added to the decimal words halve for each bit. And that's the principle behind a traditional algorithm; set the value for the highest bit, halve it for the next bit, while looping until all bit values have been added.
It would be possible to extend the principle here to larger bit-size numbers using the same principles. It would just require more words to store the decimal value and more program memory to do the additions for the individual bits.
Memory use does increase with bit-size and the 48-bit code won't fit in an M2's memory, and the 64-bit code won't even fit in an X2. There is probably some scope for optimisation but the goal was something 'straight forward' which worked.
Apart from that; this code should work on any M2, X1 and X2 PICAXE and will also run within the PE6 simulator.
If there are any questions; please do ask. If you spot any errors or bugs; please let me know.