Optimization of assembly code of cc65 possible?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Optimization of assembly code of cc65 possible?

Post by DRW »

When I write inline assembly code in C with cc65 (__asm__("LDA %v", variable)), then the code will get optimized just like the rest of the C code and I can see the optimization in the generated .s file.

Is there a way to run the same optimizer over a hand-written .s file and to generate a second .s file out of it?

I know, I could simply turn the whole file into a C file with inline assembly. But is there also a simpler way? A way that allows me two write my assemy code in an actual *.s file, but where I can generate a copy of it that I then put into the compile process, so that the ROM always automatically has the optimized code.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
calima
Posts: 1745
Joined: Tue Oct 06, 2015 10:16 am

Re: Optimization of assembly code of cc65 possible?

Post by calima »

No, that's not possible. The optimizer is part of the C compiler, the assembler has nothing of the sort.
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Re: Optimization of assembly code of cc65 possible?

Post by DRW »

Maybe this would be a good feature in the future. It shouldn't require that much programming logic: The assembler simply needs to convert the assembly file into a C file with inline assembly. And then, it lets the C compiler run over it to produce another assembly file.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
calima
Posts: 1745
Joined: Tue Oct 06, 2015 10:16 am

Re: Optimization of assembly code of cc65 possible?

Post by calima »

Is it useful though? Even a newbie to asm can easily write better code than what cc65 does.
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: Optimization of assembly code of cc65 possible?

Post by Oziphantom »

If you want an optimising assembler, tass64 has a build in optimiser, but it only provides you with hints on what can be optimised. As sometimes doing an optimisation will undo the ability to optimise. It won't rearrange your code, but it will find shortcuts and dead opcodes like doing a clc after a bcs for example.
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Re: Optimization of assembly code of cc65 possible?

Post by DRW »

calima wrote:Is it useful though? Even a newbie to asm can easily write better code than what cc65 does.
You can surely write better Assembly code than what the cc65 compiler turns into Assembly out of C.

But my idea is about writing hand-written Assembly code and getting that one optimized by the compiler.
I assume that hand-written assembly code is better than the same hand-written assembly code + optimizations on it, don't you?
Oziphantom wrote:If you want an optimising assembler, tass64 has a build in optimiser
Can it work with the ca65 syntax? My idea was to check my existing code without much hazzle and with the same or better optimizing quality that cc65 uses for inline assembly blocks.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: Optimization of assembly code of cc65 possible?

Post by FrankenGraphics »

That [tass64-style tips] would be an interesting feature for ca65. Maybe it could output all optimisation tips in a separate list; rather than cluttering the console (the cc65 suite already has all kinds of notes, warnings and error notices).

With 1)a command line option to activate optimisation tips, and 2) a directive to ignore generating tips for certain blocks, it'd be effective.
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: Optimization of assembly code of cc65 possible?

Post by Oziphantom »

DRW wrote:
Oziphantom wrote:If you want an optimising assembler, tass64 has a build in optimiser
Can it work with the ca65 syntax? My idea was to check my existing code without much hazzle and with the same or better optimizing quality that cc65 uses for inline assembly blocks.
It depends on how deeply you use CA65s features, both use the dot directive format, so .byte .word .segment etc. Tass64 has a build in linker rather than an external one, so they conceptually do the same things, just with different levels of abstraction. The macro format is not compatible. A better question might be, What does CA65 do that you need, and can Tass64 do it as well? Then if it can/or you like the idea of the optimizer more, do a small test with the optimizer to see if it will does enough to switch.
Post Reply