Hello
I fully agree with Tancor, what he writes is certainly right! But I think that the central code remaining when the I/O piece has been dealt with could be a quite elegant demonstration of BCD arithmetics, CARRY flag and of the SBC operation. These concepts are discussed in this forum right now on other places!
Assume that after I/O one has the two sequences HH1,MM1,SS1 and HH2,MM2,SS2 BCD coded in 3 byte, respectively.
Compute first SS1 + SS2 with BCD arithmetics.
If the sum is greater or equal to 100 then carry will be set. The 60 can be subtracted and then carry again be set. This will then be added for the next sum which is MM1 + MM2.
If the sum is less then 100 carry will be unset. One then tries to subtract 60 checking if the result is positive, i.e. that carry is set. Or better leave carry unset and subtract 59 instead. If then carry is set one is set to compute MM1 + MM2 taking this carry into account. If carry is unset the sum SS1 + SS2 was less then 60 and the old value must be restored. No carry for the sum MM1 + MM2.
The complete code:
Code:
T1 = $50 ;FIRST INPUT SEQUENCE
T2 = $53 ;SECOND INPUT SEQUENCE
DT = $56 ;OUTPUT
*=$6000
SED
LDX #$02 ;LOOP 2,1,0 FOR SEC, MINUTES, HOURS
CLC
L1 LDA T1,X ;GET TIME ELEMENT
ADC T2,X ;ADD MATCHING TIME ELEMENT
BCC L2
SBC #$60 ;THE SUM WAS LARGER THEN 100
SEC ;CARRY TO NEXT TIME ELEMENT AS 60 WAS SUBTRACTED
BRA L3
L2 TAY ;SAVE OLD VALUE FOR POSSIBLE RETRIVAL
SBC #$59 ;INSTEAD OF SETTING CARRY, REDUCE 60 TO 59
BCS L3
TYA ;ORIGINAL VALUE LESS THEN 60
L3 STA DT,X ;SAVE RESULT
DEX
BPL L1
CLD
BRK