Page 1 of 1

Is this info correct?

Posted: Wed Sep 21, 2016 7:52 am
by Denine
I was looking at good practices on the wiki when I spotted Use Jump tables with RTS instruction instead of JMP indirect instruction incorrectly page and it says.
Savings : 4 bytes, 1 cycle.
Now, isnt that wrong? 4 bytes is correct, but there is no saving in cycles.
In fact, the alternate piece of code is 1 cycle slower, not faster. If thats the case, this optimisation should be in Optimise code size at the expense of cycles part of the page.

Sorry if this is in wrong subforum.

Re: Is this info correct?

Posted: Wed Sep 21, 2016 8:24 am
by tepples
To report problems with the text of a single article, I recommend editing the article's talk page. I've replied there.

Re: Is this info correct?

Posted: Wed Sep 21, 2016 9:10 am
by koitsu
Assuming all the variables are absolute addresses (i.e. not in ZP):

Code: Select all

  ldx JumpEntry        ; aeXXXX   / 4 cycles
  lda PointerTableL,X  ; bdXXXX   / 4 cycles (5 if crosses page)
  sta Temp             ; 8dXXXX   / 4 cycles
  lda PointerTableH,X  ; bdXXXX   / 4 cycles (5 if crosses page)
  sta Temp+1           ; 8dXXXX   / 4 cycles
  jmp [Temp]           ; 6cXXXX   / 5 cycles
                       ; ===================
                       ; 18 bytes / 25 to 27 cycles

Code: Select all

  ldx JumpEntry        ; aeXXXX   / 4 cycles
  lda PointerTableH,X  ; bdXXXX   / 4 cycles (5 if crosses page)
  pha                  ; 48       / 3 cycles
  lda PointerTableL,X  ; bdXXXX   / 4 cycles (5 if crosses page)
  pha                  ; 48       / 3 cycles
  rts                  ; 60       / 6 cycles
                       ; ===================
                       ; 12 bytes / 24 to 26 cycles
The situation changes if Temp is in ZP. The odds of JumpEntry or PointerTable{H,L} being in ZP is extremely low given the entire point of the routine/goal (this is all going to be in ROM :-) ).

Re: Is this info correct?

Posted: Wed Sep 21, 2016 9:40 am
by Denine
@tepples
I'm sorry. Next time, i'll do it there.

@koitsu
Ahh, I see. I always have temps in ZP, and didn't think about having these in non-ZP.

Sorry for the commotion, will use talk page next time.

Re: Is this info correct?

Posted: Wed Sep 21, 2016 10:36 am
by rainwarrior
!

Ha ha wow I feel like I've been lied to. All this time I've been using RTS for jump tables because I thought it was universally better, even though I hate building tables with the -1 (feels like obfuscation).

I'm surprised though; I thought I'd counted out the difference at some point, but I guess I really hadn't.

Re: Is this info correct?

Posted: Wed Sep 21, 2016 11:12 am
by tepples
You may have counted out the cycle difference, except you assumed the jump target variable was outside zero page.

Less likely, it was between the RTS trick and moving one of your arrays out of zero page to make room for the jump target variables for main, NMI, and/or IRQ.

Re: Is this info correct?

Posted: Wed Sep 21, 2016 11:28 am
by rainwarrior
No, it was none of those things.

Re: Is this info correct?

Posted: Wed Sep 21, 2016 12:25 pm
by tokumaru
I tend not to use RTS calling trick by default, since I already noticed It's 1 cycle slower, but I do use it when there's something to gain elsewhere. I've used it for calling VRAM update routines, for example, so that each routine could directly call the next one as fast as possible, due to the addresses being set up beforehand (I don't use this method for VRAM updates anymore though). Another case where calling with RTS makes a lot of sense is when the address was obtained from the stack after a JSR, since it's already "-1 adjusted".