13 ms·
Web server for Linux written in amd64 assembly
- deleted 13y ago[deleted]
- Luyt 13y agoxor 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.
- sneak 13y ago> It takes a somewhat longer instruction code, and a few CPU cycles more, but it conveys meaning better. So do webservers written in Python. I don't think that was the point of this project.
- XorNot 13y agoCouldn't you just find-replace this in a makefile? Then again almost everything about assembly gets a bit "isn't that what we made other languages for?"
- xyzzy123 13y agoPersonally 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 13y agosame 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.
- lttlrck 13y agoA few more instruction cycles? Which architecture and which decade?
- ksk 13y agoThis decade :) For modern out-of-order CPUs - xor reg1,reg2 is problematic because the result depends on the previous contents of the register. Hence it cannot be executed out of order. However as a special case xor reg1,reg1 (along with sub reg1,reg1) will be detected by the cpu (intel anyway) as a 'zero idiom' instruction and because it has a smaller opcode than mov reg,0 its preferred. There are also other complex reasons - for detailed info see 3.5.1.8 in the Intel optimization manual.
- Dylan16807 13y ago>This decade :) The initial claim was that mov reg,0 was slower to execute.
- teraflop 13y agoSmaller opcodes will, all else being equal, take up fewer instruction decide cycles and less space in the CPU cache.
- ksk 13y agoA CPU cycle is Fetch,Decode,Execute,Writeback. The 'zero idiom' instructions do not consume any 'Execute' resources. Apart from this.. as the other poster said, smaller opcodes means more instructions can fit inside the caches.
- csmithuk 13y agoxoring 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.
- makmanalp 13y agoPretty 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!
- vidarh 13y agoMost decent assemblers supports macros, some of them fairly complicated. And there is Randall Hyde's High Level Assembly: http://www.plantation-productions.com/Webster/ http://www.plantation-productions.com/Webster/
- pjmlp 13y ago> It's awesome how assembly these days is reasonably high level: There were macro assemblers already available in the 80's!
- gillianseed 13y agoIndeed, I pretty much learned programming on the C64 using Turbo Assembler which had macro functionality.
- MrCool2000 13y agoAFAIK the Turbo Assembler was producing x86 bytecode, how would that work?
- NigelTufnel 13y agoI wonder how fast is this server. Is handwritten assembly faster than GCC/clang-written assembly?
- 0x0 13y agoThat depends on how skilled the developer is, and what cpu subtype they are targeting :)
- vidarh 13y agoMaybe, maybe not, but for a web server, if asm vs. C is making a noticeable impact on the overall performance, one of them is doing something very wrong - they should spend most of their time in sys-calls to shuffle data to/from the network, not executing web server userland code.
- userbinator 13y agoSpeed is probably not going to differ that much but size is one area where even a novice asm programmer can easily beat a compiler.
- nknighthb 13y agoNot 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.
- diestl 13y agoWhy would you do this, write it in C and let the compiler do the hard work.
- xedarius 13y agoI 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 13y ago> There's little justification for this other than 'I can'. And why isn't that enough?
- zeeed 13y agoSecurity through transparency and readability for a bigger audience.
- dllthomas 13y agoThat's a great reason not to use this. Doesn't seem a reason not to do it.
- BlackDeath3 13y agoThat'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!
- exDM69 13y ago
- Tepix 13y agoI'm surprised that asmhttpd doesn't use sendfile(2) anywhere. Isn't that the fastest way to send a file to a socket?
- nemasu 13y agoYeah, I'll be switching over to sendfile and getting rid of the large read/write buffers. So much cleaner and simpler ( and faster? ).
- colanderman 13y agoand 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.
- nemasu 13y agoDone ... so pretty now. :)
- edsiper2 13y agoasmttpd - 0.04 Using Document Root: htdocs/ An error has occured, exiting
- nemasu 13y agoIt's a crappy error message, but usually means the port is taken.
- callesgg 13y agoRuns with about 3% less CPU load than apache on my server. However apache also does ALLOT more stuff.
- gtirloni 13y agoNo libraries required? That is awesome!
- notastartup 13y agoso 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?