Hello RP,
So you have a maximum of 4000 cycles to implement the division process. I have attempted a standard division algorithm (related to the "restoring division algorithm", without the need for restoral). The basic function code shown below seems to work correctly. The dividend and the result use the same global 5-byte array variable quotient. Another global word variable is used for the remainder associated with the calculation.
// Global variables:
byte quotient[5] = {
0xFE, 0xDC, 0xBA, 0x98, 0x76 // Dividend value
};
word remainder;
void divide40( word divisor)
{
byte i;
if (divisor == 0) {
quotient[0] = 0xFF;
quotient[1] = 0xFF;
quotient[2] = 0xFF;
quotient[3] = 0xFF;
quotient[4] = 0xFF;
return;
}
remainder = 0;
for (i = 0; i < 40; i++) {
__asm {
ldhx @quotient
lsl 4,x
rol 3,x
rol 2,x
rol 1,x
rol ,x
ldhx @remainder
rol 1,x
rol ,x
}
if (remainder > divisor) {
remainder -= divisor;
quotient[4] |= 1;
}
}
}
The number of cycles required for execution will depend on the number of 1's in the calculated quotient. For the HCS08, the formula is 2719 + 33*N cycles, where N is the number of 1's. This could be borderline to your limit of 4000 cycles.
If we now assume that the divisor value is never less than 256, so that the result can never exceed 32 bits length, it is possible to reduce the number of processing loops from 40 down to 32, with subsequent fewer cycles. The modified code follows.
void divide40( word divisor)
{
byte i;
if (divisor < 256) {
quotient[0] = 0xFF;
quotient[1] = 0xFF;
quotient[2] = 0xFF;
quotient[3] = 0xFF;
quotient[4] = 0xFF;
return;
}
remainder = (word)quotient[0];
quotient[0] = quotient[1];
quotient[1] = quotient[2];
quotient[2] = quotient[3];
quotient[3] = quotient[4];
quotient[4] = 0;
for (i = 0; i < 32; i++) {
__asm {
ldhx @quotient
lsl 4,x
rol 3,x
rol 2,x
rol 1,x
rol ,x
ldhx @remainder
rol 1,x
rol ,x
}
if (remainder > divisor) {
remainder -= divisor;
quotient[4] |= 1;
}
}
}
For this version, the formula is 2217 + N*33 cycles, a saving of about 500 cycles.
Neither functions include the loading of the dividend value into the quotient array, that will require some additional cycles. With the second version, a few cycles could be saved if the remainder and quotient variables can be preloaded, to already incorporate the 8-bit shift.
A very brief description of the division algorithms is contained in the document attached.
Regards,
Mac