Alan Adams

2020-02-18 11:55:52 UTC

My head is hurting.

I need a BASIC routine to subtract one 5-byte time from another and return

a correctly signed result.

Complications arise at the point where the low word goes from &7fffffff to

&80000000, and where the high byte increases.

BASIC doesn't allow access to the carry or borrow from arithmetic

operations.

I've developed the following which *seems* to work correctly. However I'm

not sure about all the edge cases.

This is part of a timing system, Next February the high byte of times will

change from &57 to &58, and I would like this code to survive working

across this boundary. I need to reliably detect negative results, which

can occur if a spurious finish time pulse arrives before a start time.

Subtract stamp2 from stamp1 and store the result in resultbuf.

( Although the result is 5 bytes. the elapsed time will always be

contained in the low 4 bytes. This will only overflow after 1 year, 5

months and 2 hours 27 minutes. The system doesn't meed to run for that

long. )

PROCsubstamp(resultbuf%,stamp1%,stamp2%)

LOCAL I%,J%,K%,L%,borrow%

borrow%=0

FOR I%=0 TO3

J%=stamp1%?I%

K%=stamp2%?I%

L%=J%-K%-borrow%

resultbuf%?I%=L%AND&FF

IF L% < 0 THEN borrow%=1 ELSE borrow%=0

NEXT

I%=4

J%=stamp1%?I%

K%=stamp2%?I%

L%=J%-K%-borrow%

IF L% < 0 THEN

resultbuf%!0 = -(resultbuf%!0)

ENDIF

resultbuf%?I%=L%AND&FF

ENDPROC

I need a BASIC routine to subtract one 5-byte time from another and return

a correctly signed result.

Complications arise at the point where the low word goes from &7fffffff to

&80000000, and where the high byte increases.

BASIC doesn't allow access to the carry or borrow from arithmetic

operations.

I've developed the following which *seems* to work correctly. However I'm

not sure about all the edge cases.

This is part of a timing system, Next February the high byte of times will

change from &57 to &58, and I would like this code to survive working

across this boundary. I need to reliably detect negative results, which

can occur if a spurious finish time pulse arrives before a start time.

Subtract stamp2 from stamp1 and store the result in resultbuf.

( Although the result is 5 bytes. the elapsed time will always be

contained in the low 4 bytes. This will only overflow after 1 year, 5

months and 2 hours 27 minutes. The system doesn't meed to run for that

long. )

PROCsubstamp(resultbuf%,stamp1%,stamp2%)

LOCAL I%,J%,K%,L%,borrow%

borrow%=0

FOR I%=0 TO3

J%=stamp1%?I%

K%=stamp2%?I%

L%=J%-K%-borrow%

resultbuf%?I%=L%AND&FF

IF L% < 0 THEN borrow%=1 ELSE borrow%=0

NEXT

I%=4

J%=stamp1%?I%

K%=stamp2%?I%

L%=J%-K%-borrow%

IF L% < 0 THEN

resultbuf%!0 = -(resultbuf%!0)

ENDIF

resultbuf%?I%=L%AND&FF

ENDPROC

--

Alan Adams, from Northamptonshire

***@adamshome.org.uk

http://www.nckc.org.uk/

Alan Adams, from Northamptonshire

***@adamshome.org.uk

http://www.nckc.org.uk/