top | item 2615330

A web server in x86 assembly language

75 points| hide1713 | 14 years ago |intel-assembler.it

19 comments

order

Luyt|14 years ago

Hmmm, 90% seems to be glueing Win32 APIs together for a GUI. That's not very exciting in assembly; neither is it significantly more efficient than doing it from C. It's also very unreadable, and I fear for maintainability.

And does this webserver serve many simultaneous connections? I doubt it. You'd learn more about proper webserver programming techniques by studying the source code to nginx [1] or mongrel2 [2].

As an example, it's more useful in showing how to build GUIs in assembly, and that's not very productive. You do get small executables, tho.

[1] http://nginx.org/en/

[2] http://mongrel2.org/home

mhd|14 years ago

For a short while it was quite fashionable to have really small Windows programs around, before broadband was common enough, as a reaction to bloated programs, Java runtimes, multi-megabyte VB executables etc. Saw quite a number of assembly-based editors back then. But to be honest, most of those were a small wrapper around the Windows edit controls...

Wo0kToR|14 years 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. :)

danparsonson|14 years ago

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 :-)

kragen|14 years ago

It's mostly calls to some kind of assembler macro built into MASM, which means it's not really x86 assembly language:

            INVOKE SetFilePointer,hFile,FOffset,0,FILE_BEGIN
            INVOKE ReadFile,hFile,pMem,FSize,offset NotUsed,0
            INVOKE send,wParam,pMem,FSize,0
            INVOKE GlobalUnlock,hMem
            INVOKE GlobalFree,hMem
            INVOKE closesocket,wParam
            INVOKE CloseHandle,hFile    ;Done with file and socket too.
The guy wrote it in 2001, when he was in high school, and he's done a lot more cool and interesting stuff since then, and it looks like he's graduated to Linux: http://lingcog.iit.edu/~scubed/projects.xml

Too bad he stopped adding new projects to the page in 2007. His "portfolio" page has a few more things since then.

Wo0kToR|14 years ago

Screw MASM. TASM Ideal Mode all the way!

msie|14 years ago

It would have been really neat if the page was served by the very same assembly code.

limmeau|14 years ago

It's also an example of a Win32 program in assembler, together with a WndProc for a window. Does Win32 require a window if you want to write a web server?

DrJokepu|14 years ago

Obviously not, a Win32 process is not required to have any windows at all. Actually, web servers are typically ran as services and services are not allowed to interact with the user interface at all in theory (not just with windows, but with things such as printers either).

narag|14 years ago

No. Without seeing the code, my guess is it's used either as a control console or for IPC.

leon_|14 years ago

I'd say code readability is at the level of a Node.js application.

smokeyj|14 years ago

That's a bit unfair towards node. With proper foresight clean code can be written in almost any language.

Argorak|14 years ago

Hm, no rack adapter yet?

duck