
Web server for Linux written in amd64 assembly - nkurz
https://github.com/nemasu/asmttpd
======
makmanalp
Pretty neat! It's awesome how assembly these days is reasonably high level:

[https://github.com/nemasu/asmttpd/blob/master/http.asm](https://github.com/nemasu/asmttpd/blob/master/http.asm)

It dawns on me why we couldn't have shortcuts for several patterns that show
up everywhere:

\- mov, mov, mov then call/syscall could just be written as call(arg, arg,
arg) since it's not that difficult to figure out which argument needs to go to
which register if there was a defined order of arguments.

\- push push push push <function body> pop pop pop pop <ret> could just be a
define really. I realize that there are cases where you wouldn't necessarily
do that, but that seems to be the minority. The compiler could just figure out
what registers you use in the routine and push / pop those. If you want to
keep a register, there could be added syntax for that.

It seems in both of these cases the language optimizes for simplicity and
flexibility, ignoring the common case. Neither of these strike me as
situations where introducing the abstraction would require a lot of compiler
"magic" to guess and optimize. It's almost just string replacement.

Then again, you could just write all this in C and let the compiler figure it
out. Interesting stuff!

~~~
pjmlp
> It's awesome how assembly these days is reasonably high level:

There were macro assemblers already available in the 80's!

~~~
gillianseed
Indeed, I pretty much learned programming on the C64 using Turbo Assembler
which had macro functionality.

~~~
MrCool2000
AFAIK the Turbo Assembler was producing x86 bytecode, how would that work?

~~~
pjmlp
> x86 bytecode

What?!

~~~
mehmetemre
I guess he's talking about Borland's Turbo Assembler that was coming with
Turbo Pascal:
[http://en.wikipedia.org/wiki/Turbo_Assembler](http://en.wikipedia.org/wiki/Turbo_Assembler)

~~~
pjmlp
My remark was because the OP used bytecode to describe assembly opcodes.

~~~
nkurz
The usage is nonstandard, but it provides an interesting perspective. What if
he was planning to run the X86 code on a virtual CPU, is his usage then
correct? Does the nature of compiled code change if it is executing on a
"real" machine instead of a virtual one?

~~~
pjmlp
I would say yes, given that some bytecode instruction sets make a CISC
processor look like a RISC one.

------
Luyt

        xor rax,rax
    

I used this construct often to zero a register, in the time that memory and
CPU cycles were scarce. But nowadays, my time is a more valuable resource, and
I tend to write:

    
    
        mov rax, 0
    

It takes a somewhat longer instruction code, and a few CPU cycles more, but it
conveys meaning better.

~~~
xyzzy123
Personally I find the xor form easier to follow, it's SUCH a long-standing
convention that if I saw a mov rax, 0 I would wonder what was going on.

~~~
rjzzleep
same here. _but_ if you weren't proficient in writing and reading asm the
statement would no longer be true.

now, you could argue if that were that the case, wth is that person doing
there anyway.

also if you compile something like `return 0;` it used to be compiled down to
`xor eax,eax; ret;`, but meh barely anyone i know coding these days even knew
this to begin with.

------
Tepix
I'm surprised that asmhttpd doesn't use sendfile(2) anywhere. Isn't that the
fastest way to send a file to a socket?

~~~
nemasu
Yeah, I'll be switching over to sendfile and getting rid of the large
read/write buffers. So much cleaner and simpler ( and faster? ).

~~~
colanderman
_and faster?_

You'll get way more bang for your buck by minimizing userspace→kernel round
trips and memory copies than by hand-optimizing assembler code. sendfile is
one great way to do this.

You'll get even more bang for your buck by _eliminating_ the kernel from the
packet processing path by using netmap, PF_RING/DNA, or DPDK, and a user-space
TCP/IP stack.

Assembly only really helps alleviate GCC's moronic decisions resulting in
excessive stack spills and alignment-unaware loads & stores.

Something about carts and horses.

------
diestl
Why would you do this, write it in C and let the compiler do the hard work.

~~~
xedarius
I agree. There's little justification for this other than 'I can'.

If you need to hit the metal you can inline assembly in both c and c++.

~~~
mhurron
> There's little justification for this other than 'I can'.

And why isn't that enough?

~~~
zeeed
Security through transparency and readability for a bigger audience.

~~~
dllthomas
That's a great reason not to _use_ this. Doesn't seem a reason not to _do_ it.

~~~
BlackDeath3
That's exactly right.

I wrote a web server in C. I'm not really a web guy, it was a lot of new stuff
to me, I'm a pretty amateurish programmer, and it probably doesn't even
deserve to be called a "web server". But it was fun, and really cool to know
that something that I wrote (with the help/jump-off point of a tutorial or
two) can be used to serve web pages to a client. I can run the program, pop
open Firefox, and use the browser to click through a set of test pages as
though it was being served by a real web server. That's fucking cool, and that
was all the reason that I needed to do it.

Don't Reinvent The Wheel - unless you want to!

------
NigelTufnel
I wonder how fast is this server. Is handwritten assembly faster than
GCC/clang-written assembly?

~~~
drv
I don't want to be too harsh - this is a fun idea, and I'm prone to silly
fantasies about rewriting slow code in assembly myself - but this particular
assembly doesn't take advantage of many of the "dirty tricks" that are
available in low-level code.

As one example, check out the content-type detection, which is essentially a
long chain of repeated strlen + strcmp; assembly language doesn't magically
make bad algorithms fast.

~~~
yohanatan
Not to mention that long chain is ugly to read. I would rather see a macro
defined and called multiple times than to see the same block of code
copy/pasted over and over.

------
callesgg
Runs with about 3% less CPU load than apache on my server. However apache also
does ALLOT more stuff.

~~~
driverdan
Under heavy load?

~~~
callesgg
Yeah downloading a large file. Apache was about 14% CPU Load asmttpd about 11%
CPU Load

------
gtirloni
No libraries required? That is awesome!

------
edsiper2
asmttpd - 0.04

Using Document Root: htdocs/ An error has occured, exiting

~~~
nemasu
It's a crappy error message, but usually means the port is taken.

------
notastartup
so what is the implication of this? Does this mean that this webserver will be
much faster and using less resource because it's written for linux directly?

