Hacker News new | ask | show | jobs
by Wo0kToR 5494 days ago
These Win32 assembly programs are quite cool. I used to dabble with writing small things in it. But I realised a while ago that, as Luyt has already touched on, 95% - 99.x% of the CPU time is spent in the Win32 library code, which is written in C, and which you have to call if you want to do anything significant, eg. put up a GUI. That defeats the performance benefit of writing such programs in asm: like Luyt said the slow down from writing the same thing in efficient C or C++ code would be minute. You still get the benefit of tiny executables, though.

One might consider the fact that nowadays it's only worth writing "busy code" in assembly, and most or all of a GUI app isn't busy code, but that would be being sensible. A web server might have a few pieces of busy code in it IF it's designed to connect to hundreds or thousands of users at once. It would be good to write those in assembly. :)

1 comments

Also, generally speaking you'd have to be a pretty fine assembler programmer to out-optimise a modern compiler in a meaningful way; either that, or have some knowledge that the compiler doesn't (e.g. data parallelism).

Of course, there's always the fun factor :-)

In terms of out-optimizing a modern compiler, it really depends on what you're optimizing for. For performance, a modern compiler is going to destroy you 99% of the time -- I've been writing x86 assembly for many years and in the vast majority of cases, I can't write faster code than MSVC. However, if code size is what's important, it's easy to beat any compiler. You can often get your code down an order of magnitudes, sometimes even two. Sizecoding has become my passion in recent years, and it's amazing how much you can do that the compiler can't if you really care; for instance, this is a bootsector I wrote that reads a kernel from NTFS: http://demoseen.com/ColorblindBoot.s.txt As far as I know, this is the only NTFS reader that actually fits into the boot sector (510 bytes, of which it uses every one), which is kinda neat.
Why did you use port I/O instead of int 13h?
Using int 13h is fine for floppies, but when you're using it with hard drives it gets messy -- you have to use the extended versions which are buggy as hell. Much easier to just do it directly, and it doesn't take up much more space (if at all).
For a large amount of code, like a whole program, maybe. But I think you can still get big gains in small loops or algorithms written and optimized in assembly. Even more so if you optimize for a specific processor and cache. Write the rest of the program in a high level language.
And run a profiler, before you optimize.