Just have enough time to squeeze in Part 3 (supposedly the most difficult part) before I have to go and do something meaningful with my life...
This next subroutine takes our previously calculated Linear DAYS, and together with our HOURS, MINUTES and SECONDS builds a 32 bit number. It's the PBP mathematical equivallent of...
X=(DAYS*86400)+(HOURS*3600)+(MINUTES*60)+SECONDS
...but using simple 16-bit integer math only. It would be interesting to compare which method uses less codespace.
Before you look at the subroutine, I'd like to point out, that again I'm re-using variables previously defined earlier. Don't get confused with the names of the WORD variables such as TempHOURS, TempMINUTES and TempSECONDS. They have been previously defined in our program and I'm just using them as convenient WORD-sized storage.
Naturally we can extract our four 8-Bit Bytes (ByteA, ByteB etc) from the two 16-Bit variables (TempA and TempB) holding our 32-Bit result, and use that to directly set our iButton.Code:' ' Subroutine Calculates Linear Seconds (as 32-Bit Number) ' ------------------------------------------------------- CalculateLinearSeconds: TempA=0 ' Holds the HighWord (Bits 16-31) ie ByteA and ByteB TempB=0 ' Holds the LowWord (Bits 0-15) ie ByteC and ByteD IF HOURS > 11 then TempB=$A8C0 HOURS=HOURS-12 endif TempSeconds=(HOURS*3600)+(MINUTES*60)+SECONDS TempB=TempB+TempSeconds If TempB < TempSeconds then TempA=TempA+1 TempMINUTES=$A8C0:TempSeconds=$0000 TempHOURS=$8000 If DAYS=0 then CalculateLinearSecondsExit While DAYS > 0 If DAYS = > TempHOURS then TempB=TempB+TempSECONDS If TempB < TempSECONDS then TempA=TempA+1 TempA=TempA+TempMINUTES DAYS=DAYS-TempHOURS endif TempSECONDS=TempSECONDS>>1 If TempMINUTES.0=1 then TempSECONDS.15=1 TempMINUTES=TempMINUTES>>1 TempHOURS=TempHOURS>>1 Wend CalculateLinearSecondsExit: Return
What the above program does is a very simple Binary Recursive Addition to add together up to a maximum of 65535 days worth of seconds in sixteen (or less) itterations - which makes this a fast solution. We start off with TempMinutes and TempSeconds together holding a 32-Bit number ($A8C0:0000) which is the equivallent of 32768 days worth of Seconds. TempHOURS contains our DAY count-down, starting with 32768 days. Each time we travel through the WHILE-WEND loop, we halve the number of days (and so halving the number of seconds), until we run out of days and the loop is exited.
Actually we can only calculate around 49710 days worth of seconds, before we spill out of 32 bits, so there's no point in calculating any dates beyond about 135 years anyway.
I'll give you a pretty Data-entry routine to tie it all together later when I've some more time.
Once again. for all the skeptics complaining about the lack of big-number math in PBP, this demonstrates that it CAN be done.
I've not actually tested this routine, but it's simple enough that I'm confident it'll hold water.




Bookmarks