Live data from Hacker News

Web server for Linux written in amd64 assembly

github.com

11–20 of 59 posts

Re: Web server for Linux written in amd64 assembly

#11
post #3

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.

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.

Re: Web server for Linux written in amd64 assembly

#12

Pretty neat! It's awesome how assembly these days is reasonably high level: 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…

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

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

Re: Web server for Linux written in amd64 assembly

#14
post #12

Pretty neat! It's awesome how assembly these days is reasonably high level: 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…

> It's awesome how assembly these days is reasonably high level: There were macro assemblers already available in the 80's!

This. Microsoft MASM is the canonical example of one.

Born in 1981, still being updated today:

http://en.wikipedia.org/wiki/Microsoft_Macro_Assembler

Re: Web server for Linux written in amd64 assembly

#15
post #3

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.

A few more instruction cycles? Which architecture and which decade?

Re: Web server for Linux written in amd64 assembly

#16
post #3

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.

xoring something with itself is a common idiom in ASM. Been doing it since the Z80 (XOR A). Not sure that meaning is enhanced at all.

Re: Web server for Linux written in amd64 assembly

#18

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

Not in the general case. It's been a long time since x86 assembly developers could commonly beat a decent optimizing compiler.

The thing about compilers is that they're leveraging, even if imperfectly, the collective wisdom of their authors and of the companies who actually built the chips and have offered insight, advice, and sometimes even code. It's very probable they know more performance tricks than you do.

One problem is landmines in the ISA, such as instructions that look like they exist to be used, but are really traps implemented in suboptimal microcode for the unwary programmer who didn't look closely at their performance characteristics. Or certain sequences of instructions that might combine to do something ridiculously slow[1].

These landmines vary by microarchitecture. An instruction that's incredibly slow on one line of x86 chips might be a wonder-drug on another. This both increases the probability that your code will hit a landmine on at least some CPUs, and gives you a possible "in": Compilers aren't going to optimize perfectly for every microarchitecture. If you know exactly what you're doing (or spend a hell of a lot of time on trial and error), you might be able to come up with optimal codepaths for specific chips that the compiler didn't.

By and large it's not worth it, though. Hand-tuned assembly still ends up in places, but increasingly rarely, and it's confined to small hot-spots. A particular algorithm or part of an algorithm gets re-implemented in assembly because the compiler just can't get it right.

[1] I could have sworn there was a story about this just recently, but I can't seem to find it. Something like a piece of code running way slower than anyone thought it should, until an AMD engineer piped up and said "Oh yeah, don't do that, it causes a pipeline flush." for reasons that were utterly non-obvious to anyone who didn't know the internals of the chip.

Re: Web server for Linux written in amd64 assembly

#19

Pretty neat! It's awesome how assembly these days is reasonably high level: 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…

If you or anyone is looking for something which takes care of a lot of the entry and exits to functions I would suggest they look x264's x86inc.asm [1]. It was designed for using SIMD in DSP functions not for writing whole programs but I don't think it would get in your way of doing that.

Plus it is BSD licensed for those that hate the GPL.

[1] http://git.videolan.org/?p=x264.git;a=blob;f=common/x86/x86i...

Re: Web server for Linux written in amd64 assembly

#20
post #12

Pretty neat! It's awesome how assembly these days is reasonably high level: 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…

> It's awesome how assembly these days is reasonably high level: There were macro assemblers already available in the 80's!

In my OS/architecture class we used a textbook whose author had piled on so much macro assembling on top of SPARC asm (macros all in m4, naturally) that he in effect was writing the book using a personal high-level language constructed out of gobs of m4. Like the bizarro-world version of personalized language construction in Lisp-land...

Was this book (the first review complains about the same thing): http://www.amazon.com/gp/product/0130255963/ref=as_li_ss_tl?...

Post reply on HN