Why TXA STA temp ?

baardbi

Well-known member
I noticed that there are a ton of places in the code where this is used:

TXA
STA temp

I'm just wondering if there is a reason for this. It would be much more efficient to use this instead:

STX temp

Since that particular bit of code is used so often I would think you would save a lot of bytes if STX was used instead.
 

kevin81

Well-known member
Indeed, the only reason to use TXA/STA instead of STX is if you don't know STX exists.

Using STX in these cases saves not only a byte per instance, but two cycles every time it is executed, which may add up quick within loops.
So not only would it save ROM space; it also improves performance. Only slightly maybe, but every cycle counts!

EDIT: Before you go and auto-search-and-replace all instances of TXA/STA with STX, make sure there's no hidden PHA instruction somewhere down the road :)
 
Last edited:

baardbi

Well-known member
Indeed, the only reason to use TXA/STA instead of STX is if you don't know STX exists.

Using STX in these cases saves not only a byte per instance, but two cycles every time it is executed, which may add up quick within loops.
So not only would it save ROM space; it also improves performance. Only slightly maybe, but every cycle counts!

EDIT: Before you go and auto-search-and-replace all instances of TXA/STA with STX, make sure there's no hidden PHA instruction somewhere down the road :)
Yes. That's exactly what I was thinking. Thanks for confirming it.
 

baardbi

Well-known member
I have just finished a pretty big experiment. I went through all the Assembly code in NESmaker (yes every line) to look for instances of TXA STA temp or TYA STA temp or similar pieces of code. For a few years I have wondered how much NESmaker could be improved, both in terms of speed and memory usage if code snippets like TXA STA temp were replaced with STX temp, or similarly TYA STA ... STY. Well, now I have the answer. It's hard to give a 100% correct answer because there are several modules and different ways to set up games, but throughout the code there are 184 instances of this code snippet (that I could find). I re-wrote the code slightly to change every instance I could find to STX or STY. Unfortunately the result isn't mind blowing or anything, but since I love to optimize my code I think this is important. I noticed in some modules you can save 1% in bank $1F. The more important thing is the number of cycles saved. Again, it's nothing mind blowing, but it seems like you can save somewhere between 200 to 400 cycles by doing this. That means a tiny bit of improvement in speed.

PS! I have obviously not touched the instances for pushing to and pulling from the stack, or any other instance where TXA or TYA is appropriate.

To make a long story short, use this

Code:
STX temp

Instead of this

Code:
TXA
STA temp

whenever possible. Or the equivalent with TYA STA / STY. In the long run you will save some bytes and cycles, and as we know, every little bit helps when it comes to performance. This code snippet can be found in most default input scripts in NESmaker.

This isn't a tutorial or anything like that. I just wanted to share what I found. If you're not comfortable with assembly language and the NESmaker code base, you should be very careful about changing these files, and always remember to backup before doing something like this. I have done tests on games with every module (using a default NESmaker setup). As far as I can see I haven't made any mistakes, and everything is working like normal. As I said, this isn't a tutorial. It's more like a lab experiment. I just wanted to report what I found. I have attached a text file which shows all the files (and line numbers) where I found and changed this code snippet. The line numbers indicate where something needs to be changed. I have also attached a zip file with pictures that shows the comparison between a ROM file with and without this change. Don't expect anything amazing. Like I said, the improvements are tiny.

Anyway. I hope this was somewhat interesting or useful.
 

Attachments

  • TXA_Fixes.txt
    10.8 KB · Views: 6
  • ROM_Size_Comparison.zip
    387.1 KB · Views: 4
Last edited:

mileco

Active member
I have just finished a pretty big experiment. I went through all the Assembly code in NESmaker (yes every line) to look for instances of TXA STA temp or TYA STA temp or similar pieces of code. For a few years I have wondered how much NESmaker could be improved, both in terms of speed and memory usage if code snippets like TXA STA temp were replaced with STX temp, or similarly TYA STA ... STY. Well, now I have the answer. It's hard to give a 100% correct answer because there are several modules and different ways to set up games, but throughout the code there are 184 instances of this code snippet (that I could find). I re-wrote the code slightly to change every instance I could find to STX or STY. Unfortunately the result isn't mind blowing or anything, but since I love to optimize my code I think this is important. I noticed in some modules you can save 1% in bank $1F. The more important thing is the number of cycles saved. Again, it's nothing mind blowing, but it seems like you can save somewhere between 200 to 400 cycles by doing this. That means a tiny bit of improvement in speed.

PS! I have obviously not touched the instances for pushing to and pulling from the stack, or any other instance where TXA or TYA is appropriate.

To make a long story short, use this

Code:
STX temp

Instead of this

Code:
TXA
STA temp

whenever possible. Or the equivalent with TYA STA / STY. In the long run you will save some bytes and cycles, and as we know, every little bit helps when it comes to performance. This code snippet can be found in most default input scripts in NESmaker.

This isn't a tutorial or anything like that. I just wanted to share what I found. If you're not comfortable with assembly language and the NESmaker code base, you should be very careful about changing these files, and always remember to backup before doing something like this. I have done tests on games with every module (using a default NESmaker setup). As far as I can see I haven't made any mistakes, and everything is working like normal. As I said, this isn't a tutorial. It's more like a lab experiment. I just wanted to report what I found. I have attached a text file which shows all the files (and line numbers) where I found and changed this code snippet. The line numbers indicate where something needs to be changed. I have also attached a zip file with pictures that shows the comparison between a ROM file with and without this change. Don't expect anything amazing. Like I said, the improvements are tiny.

Anyway. I hope this was somewhat interesting or useful.
Performance improvements are always important. I'll have a look. Thanks so much.
 

TheRetroBro

Active member
I have just finished a pretty big experiment. I went through all the Assembly code in NESmaker (yes every line) to look for instances of TXA STA temp or TYA STA temp or similar pieces of code. For a few years I have wondered how much NESmaker could be improved, both in terms of speed and memory usage if code snippets like TXA STA temp were replaced with STX temp, or similarly TYA STA ... STY. Well, now I have the answer. It's hard to give a 100% correct answer because there are several modules and different ways to set up games, but throughout the code there are 184 instances of this code snippet (that I could find). I re-wrote the code slightly to change every instance I could find to STX or STY. Unfortunately the result isn't mind blowing or anything, but since I love to optimize my code I think this is important. I noticed in some modules you can save 1% in bank $1F. The more important thing is the number of cycles saved. Again, it's nothing mind blowing, but it seems like you can save somewhere between 200 to 400 cycles by doing this. That means a tiny bit of improvement in speed.

PS! I have obviously not touched the instances for pushing to and pulling from the stack, or any other instance where TXA or TYA is appropriate.

To make a long story short, use this

Code:
STX temp

Instead of this

Code:
TXA
STA temp

whenever possible. Or the equivalent with TYA STA / STY. In the long run you will save some bytes and cycles, and as we know, every little bit helps when it comes to performance. This code snippet can be found in most default input scripts in NESmaker.

This isn't a tutorial or anything like that. I just wanted to share what I found. If you're not comfortable with assembly language and the NESmaker code base, you should be very careful about changing these files, and always remember to backup before doing something like this. I have done tests on games with every module (using a default NESmaker setup). As far as I can see I haven't made any mistakes, and everything is working like normal. As I said, this isn't a tutorial. It's more like a lab experiment. I just wanted to report what I found. I have attached a text file which shows all the files (and line numbers) where I found and changed this code snippet. The line numbers indicate where something needs to be changed. I have also attached a zip file with pictures that shows the comparison between a ROM file with and without this change. Don't expect anything amazing. Like I said, the improvements are tiny.

Anyway. I hope this was somewhat interesting or useful.
Now if we could find all the other ways to optimization the built in spaghetti code, games may not crash with more than 4 objects on acreen lol.

Great work bro!
 

mileco

Active member
Now if we could find all the other ways to optimization the built in spaghetti code, games may not crash with more than 4 objects on acreen lol.

Great work bro!
And it is crucial in arcade MOD, since just 4 objects on screen is most of the times not enough. If you have a couple of moving plats on your screen you can only have 2 more enemies, and it means an almost empty screen.
 

dale_coop

Moderator
Staff member
Replacing “TXA / STA” with “STX” has been discussed and used in many projects already. It's a known fact that you don’t save much, but it’s definitely cleaner.
The real work for optimizations would be to rewrite the object management system.
 

baardbi

Well-known member
Replacing “TXA / STA” with “STX” has been discussed and used in many projects already. It's a known fact that you don’t save much, but it’s definitely cleaner.
The real work for optimizations would be to rewrite the object management system.
Yes. You're right. This was basically an OCD kind of thing for me lol.
 

baardbi

Well-known member
question about this
\GameEngineData\Routines\BASE_4_5\Game\Subroutines\doUpdateActionTimer.asm Line 29


28(blank)
29 LDA tempD
30 TAY

I don't see TXA or TYA. What can I change LDA into?
You can change it to this:

;LDA tempD
TAY

The reason is that the last line before that is STA tempD. That means the value is already in A (accumulator), so we don't need to load it into A again.
 
I have just finished a pretty big experiment. I went through all the Assembly code in NESmaker (yes every line) to look for instances of TXA STA temp or TYA STA temp or similar pieces of code. For a few years I have wondered how much NESmaker could be improved, both in terms of speed and memory usage if code snippets like TXA STA temp were replaced with STX temp, or similarly TYA STA ... STY. Well, now I have the answer. It's hard to give a 100% correct answer because there are several modules and different ways to set up games, but throughout the code there are 184 instances of this code snippet (that I could find). I re-wrote the code slightly to change every instance I could find to STX or STY. Unfortunately the result isn't mind blowing or anything, but since I love to optimize my code I think this is important. I noticed in some modules you can save 1% in bank $1F. The more important thing is the number of cycles saved. Again, it's nothing mind blowing, but it seems like you can save somewhere between 200 to 400 cycles by doing this. That means a tiny bit of improvement in speed.

PS! I have obviously not touched the instances for pushing to and pulling from the stack, or any other instance where TXA or TYA is appropriate.

To make a long story short, use this

Code:
STX temp

Instead of this

Code:
TXA
STA temp

whenever possible. Or the equivalent with TYA STA / STY. In the long run you will save some bytes and cycles, and as we know, every little bit helps when it comes to performance. This code snippet can be found in most default input scripts in NESmaker.

This isn't a tutorial or anything like that. I just wanted to share what I found. If you're not comfortable with assembly language and the NESmaker code base, you should be very careful about changing these files, and always remember to backup before doing something like this. I have done tests on games with every module (using a default NESmaker setup). As far as I can see I haven't made any mistakes, and everything is working like normal. As I said, this isn't a tutorial. It's more like a lab experiment. I just wanted to report what I found. I have attached a text file which shows all the files (and line numbers) where I found and changed this code snippet. The line numbers indicate where something needs to be changed. I have also attached a zip file with pictures that shows the comparison between a ROM file with and without this change. Don't expect anything amazing. Like I said, the improvements are tiny.

Anyway. I hope this was somewhat interesting or useful.
Just saw this NICE I will get to toying with this list ASAP
 
Everything seems to be working, but \GameEngineData\Routines\BASE_4_5\Game\Subroutines\doGetPathInfo.asm Line 165 cause small glitch in path tiles. I put it back. Overall, it is not noticeable, but tiny improvement helps.
 
Top Bottom