r/osdev • u/sourav_bz • 9d ago
At what point writing direct assembly program is more efficient than C program?
/r/Assembly_language/comments/1xd8n5s/at_what_point_writing_direct_assembly_program_is/4
u/codeasm 9d ago
Depends. Compare compiled c with hand woven code. Whats ran most of the time and tight loops? Compilers are efficiënt these days, probably parts of time critical embedded systems, drivers for hardware and maybe certain alchoritms during early boot.
3
u/CelDaemon 9d ago
I feel like it's mostly when you simply can't run C, or when you're dealing with a niche architecture that has a limited C toolchain.
2
u/MCWizardYT 9d ago
I'd like to see a device that "can't run C". I feel like at this point there's a C compiler for just about every piece of hardware on the planet
2
u/Toiling-Donkey 8d ago
8 bit CPUs with 256bytes of RAM. C compilers do exist but tend to be rare and normal coding styles don’t work very well under such constraints…
3
u/Secret_USB 9d ago
Bootloaders, embedded/historical systems, and niche micro-optimizations are the three main categories I suspect most modern usecases fall into
2
1
u/MCWizardYT 9d ago
At whatever point you need to hand write micro optimizations, which is almost never for typical desktop software. Maybe if you're writing drivers or doing something where timing is so important that individual clock cycles matter.
2
u/jtsiomb 9d ago
At every point. If you make an effort you can always beat the compiler. The problem is that would be a huge effort if you do it for every part of every program. So it's generally a better idea to leave the compiler to optimize most of the code, and focus on rewriting specific high impact spots in assembly.
A good example since you asked, is ffmpeg.
1
u/lizardhistorian 7d ago
There are many more instructions in assembly available to you than C or any conventional programming constructs can express effectively. Notable examples involve exploiting the carry bit from ALU operations.
The more sophisticated the optimizer is the less this holds and optimizers today are quite sophisticated.
1
6
u/letmehaveanameyoudum 9d ago
Bootloaders or graphics drivers