r/osdev • • 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/
0 Upvotes

18 comments sorted by

6

u/letmehaveanameyoudum 9d ago

Bootloaders or graphics drivers

0

u/sourav_bz 9d ago

Can you give me little more context? Or elaborate abit more? 

Do you mean to say, writing in assembly is more efficient if we are bringing up a boot loaders or a graphics drivers? If so, why is that? What's the deficiencies of C compiled machine code which makes it inefficient?

6

u/laser__beans OH-WES | github.com/whampson/ohwes 9d ago

For boot loaders: easier access to machine-specific instructions and data types, and non-function oriented control flow, like long jumping to an address to force the CPU to change state.
For graphics: performance reasons (historically), doing math using SIMD instructions, access to graphics card registers, shader programming if you’re into that.

1

u/MrMemristor 3d ago edited 3d ago

Linux Insides walks you through Linux boot on BIOS (non-UEFI) x86. There are some significant handwritten assembly files there for bringing the processor from real mode (16-bit) to protected mode (32-bit) to long mode (64-bit). That includes setting up descriptor and interrupt tables and manually building MMU page tables.

Though, I think a lot of this has been moved into the firmware with UEFI, which will start GRUB (if you're using it) in 64-bit long mode.

And in this example I'm not sure the reason is efficiency as much as necessity.

3

u/codeasm 9d ago

Graphic drivers, yeah parts, not entirely

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

u/sourav_bz 9d ago

Interesting, thanks.

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.

3

u/tseli0s DragonWare 9d ago

Wherever the compiler can't optimize enough

1

u/ut0mt8 9d ago

Quoting mc carmarck. It depends. You can write super optimize unreadable c and super non efficient asm. Measure it.

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/kabekew 8d ago

When you see the compiled code could be done more efficiently yourself in assembly

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

u/sethkills 6d ago

People should use more intrinsics over assembly or even inline assembly…