On 6/14/2022 at 3:27 AM, Greg King said:
ASL instead of ROL!
Yes, that would save one byte and 2 CPU cycles. Since ASL always shifts 0 into the low bit, I don't actually need the CLC first. Next time I need to do this, I'll probably use the ASL and save the extra byte.
That's some of the fun about Assembly language: being able to sift through routines to save one or two bytes on a routine that's 18 bytes long. That one change is more than a 5% decrease in size and execution time... imagine if software engineers took that kind of care writing modern applications and operating systems.
In fact, now that I think about it, I could make this even smaller by removing the branch and rotating the Carry bit directly into $502. Let me see what that does...
... and here's the new routine:
STZ $0500
LDA $9F35
ASL
STA $0501
LDA #$08 ; Increment value. This gets shifted left to $1x in the next step.
ROL
STA $0502
BRK
This saves 7 bytes, although it's slightly less readable.
Also, it fixes a bug in the first example. I was setting the increment byte differently based on which bank the result landed in. This would have created unexpected behavior on R41 systems, assuming I was expecting an increment of 1.
In this case, note that I pre-load A with $08 before rotating in the Carry bit. Since we actually want the final result to set the increment to 1 (so $10 or $11), I pre-loaded the value with the increment one to the right of where I expect it to land. I could have also accomplished this with
LDA #0
ROL
ORA #$10
STA $0502
But, as you can see, this adds 2 extra bytes. However, this approach is more readable, and it might be preferable in a situation where speed isn't as much of a concern. At the very least, I'd add a comment so the next person knows what I'm doing.
So here are the test results:
Test on R41: I expect 00 B0 11
.:0500 00 B0 11 00 00 00 00 00
This is correct. R41 sets the Map base to $1:B000
Test on R38: I expect 00 00 10
.:0500 00 00 10 00 00 00 00 00