Incrementing a 2-byte number

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
Naomasa298
Posts: 386
Joined: Sat Feb 16, 2013 12:49 pm
Contact:

Incrementing a 2-byte number

Post by Naomasa298 » Wed May 27, 2020 3:37 am

Can someone tell me if I'm missing something here:

This:
INC &70
BNE skiphigh
INC &71
.skiphigh

is faster than:
CLC
LDA &70
ADC #1
STA &70
LDA #0
ADC &71
STA &71

The first takes 12 cycles if the high byte needs to be incremented, 8 cycles if not.

The second takes 18 cycles.

Or have I got this completely wrong?

User avatar
jgharston
Posts: 4039
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: Incrementing a 2-byte number

Post by jgharston » Wed May 27, 2020 5:18 am

Naomasa298 wrote:
Wed May 27, 2020 3:37 am
Or have I got this completely wrong?
Nope, perfectly correct, and a standard programming paradigm for a multi-byte increment.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.32
(C) Copyright J.G.Harston 1989,2005-2020
>_

cmorley
Posts: 1278
Joined: Sat Jul 30, 2016 8:11 pm
Location: Oxford
Contact:

Re: Incrementing a 2-byte number

Post by cmorley » Wed May 27, 2020 7:37 am

Worth noting too that the first is 6 bytes and the second is 13 bytes so a big code space saving.

Naomasa298
Posts: 386
Joined: Sat Feb 16, 2013 12:49 pm
Contact:

Re: Incrementing a 2-byte number

Post by Naomasa298 » Wed May 27, 2020 11:21 am

Thanks to you both!
jgharston wrote:
Wed May 27, 2020 5:18 am
Nope, perfectly correct, and a standard programming paradigm for a multi-byte increment.
Oh, I didn't know that. I thought I'd just been really clever there for a minute. :lol:

VectorEyes
Posts: 345
Joined: Fri Apr 13, 2018 2:48 pm
Contact:

Re: Incrementing a 2-byte number

Post by VectorEyes » Wed May 27, 2020 12:18 pm

This is a great example of the various trade-offs you have to make when optimising code for space vs. speed, but there are other considerations as well: side-effects (or lack thereof) and cycle timing predictability.

INC &70
BNE skiphigh
INC &71
.skiphigh

Takes: Either 8 or 12 cycles. (No good if you need it always to take the same number of cycles).
Doesn't affect A.
N and Z flags set to either the result of incrementing the low byte, or the high.
Size: 6 bytes.

CLC
LDA &70
ADC #1
STA &70
LDA #0
ADC &71
STA &71

Takes: 18 cycles (good if you need consistent timings)
Sets A to high byte of result (which you might want to immediately use).
N, V, Z and C flags set to result of incrementing the high byte.
Size: 13 bytes.

User avatar
kieranhj
Posts: 896
Joined: Sat Sep 19, 2015 11:11 pm
Location: Farnham, Surrey, UK
Contact:

Re: Incrementing a 2-byte number

Post by kieranhj » Wed May 27, 2020 2:06 pm

The more masochistic cycle-counter might do...

Code: Select all

INC &70         ; 5c
BNE skiphigh
; not taken     ; 2c
INC &71         ; 5c
BNE done        ; 3c (unlikely to be incrementing &FFFF or use BRA)
.skiphigh
; branch taken  ; 3c
NOP             ; 2c
NOP             ; 2c
BNE done        ; 3c (unlikely to be incrementing &FFFF or use BRA)
.done
; constant time ; 15c
; total         ; 12 bytes
:wink:
Bitshifters Collective | Retro Code & Demos for BBC Micro & Acorn computers | https://bitshifters.github.io/

User avatar
0xC0DE
Posts: 633
Joined: Tue Mar 19, 2019 7:52 pm
Location: The Netherlands
Contact:

Re: Incrementing a 2-byte number

Post by 0xC0DE » Wed May 27, 2020 3:20 pm

Did somebody call me? :lol:
0xC0DE
"I program my home computer / Beam myself into the future"
:arrow: Follow me on Twitter
:arrow: Visit my YouTube channel featuring my demos for Acorn Electron and BBC Micro

dp11
Posts: 1158
Joined: Sun Aug 12, 2012 9:47 pm
Contact:

Re: Incrementing a 2-byte number

Post by dp11 » Wed May 27, 2020 4:31 pm

kieranhj wrote:
Wed May 27, 2020 2:06 pm
The more masochistic cycle-counter might do...

Code: Select all

........ cut ....................
; constant time ; 15c
; total         ; 12 bytes
:wink:
I'll raise you this code :

Code: Select all

INC &70         ; 5c
BEQ inchigh   ; 2/3c
BNE skip1	     ;3c
.skip1
BNE done     ;3c
.inchigh
INC &71         ; 5c
.done
; constant time ; 13c
; total         ; 10 bytes
if I have my maths right

User avatar
kieranhj
Posts: 896
Joined: Sat Sep 19, 2015 11:11 pm
Location: Farnham, Surrey, UK
Contact:

Re: Incrementing a 2-byte number

Post by kieranhj » Wed May 27, 2020 5:34 pm

dp11 wrote:
Wed May 27, 2020 4:31 pm
I'll raise you this code :

Code: Select all

INC &70         ; 5c
BEQ inchigh   ; 2/3c
BNE skip1	     ;3c
.skip1
BNE done     ;3c
.inchigh
INC &71         ; 5c
.done
; constant time ; 13c
; total         ; 10 bytes
if I have my maths right
=D> Very nice! There's always another cycle to be squeezed when you need it most. [-o<
Bitshifters Collective | Retro Code & Demos for BBC Micro & Acorn computers | https://bitshifters.github.io/

Naomasa298
Posts: 386
Joined: Sat Feb 16, 2013 12:49 pm
Contact:

Re: Incrementing a 2-byte number

Post by Naomasa298 » Wed May 27, 2020 5:39 pm

Dear god, you people...

:wink: =D>

VectorEyes
Posts: 345
Joined: Fri Apr 13, 2018 2:48 pm
Contact:

Re: Incrementing a 2-byte number

Post by VectorEyes » Wed May 27, 2020 5:56 pm

Now try doing this when the preceding and following code is writing to the CRTC registers, which are 1MHz, so you have to take account of whether each CPU cycle is on an even or odd 1MHz tick and adjust the timings for STA <register> accordingly because of cycle-stretching... :D

User avatar
sweh
Posts: 2191
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: Incrementing a 2-byte number

Post by sweh » Thu May 28, 2020 12:55 am

You should see the work MartinB originally did for UPURS and then Dave (hoglet)'s updates. To bit-bang the User Port at 115,200 baud you only have 8.68us per bit, on average! And part of that is spent at 1Mhz 'cos of slow-bus access... Amazing work by both of them!
Rgds
Stephen

User avatar
tricky
Posts: 4461
Joined: Tue Jun 21, 2011 9:25 am
Contact:

Re: Incrementing a 2-byte number

Post by tricky » Thu May 28, 2020 6:25 am

Also, watch out for crossing a page boundary with the branches! Iirc

User avatar
jgharston
Posts: 4039
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: Incrementing a 2-byte number

Post by jgharston » Thu May 28, 2020 10:04 pm

Naomasa298 wrote:
Wed May 27, 2020 11:21 am
jgharston wrote:
Wed May 27, 2020 5:18 am
Nope, perfectly correct, and a standard programming paradigm for a multi-byte increment.
Oh, I didn't know that. I thought I'd just been really clever there for a minute. :lol:
I worked out a multi-byte decrement, and was really pleased with myself, then turned over a page in Zaks, and there it was! :)

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.32
(C) Copyright J.G.Harston 1989,2005-2020
>_

User avatar
MartinB
Posts: 5290
Joined: Mon Mar 31, 2008 10:04 pm
Location: Obscurity
Contact:

Re: Incrementing a 2-byte number

Post by MartinB » Thu May 28, 2020 11:31 pm

sweh wrote:You should see the work MartinB originally did for UPURS and then Dave (hoglet)'s updates. To bit-bang the User Port at 115,200 baud you only have 8.68us per bit, on average! And part of that is spent at 1Mhz 'cos of slow-bus access....

Thank-you Stephen, you are very kind sir! 8)

I’m certainly no games programmer and have always admired what folks achieve by chasing the screen around at cycle level. I did actually have one such foray with a game called Clowns over on RS (as it was) and I learned a lot from our own RTW but in the end I think I just burned out.

Then later, when I conceived UPURS, I was suddenly thrown in at the deep end again and I was forced to spend countless hours sharpening my skills at cycle juggling because it was either right and working to a cycle or fatally busted - there was no middle ground. Mixing this with variable-speed hardware was an additional challenge but ultimately very satisfying.

I certainly appreciate why threads like this have a curious magnetism for us retro-programmers..... :)

Post Reply

Return to “programming”