6 ms·
Hello from a libc-free world
- spicyj 15y agoBut now the program doesn't even print "hello, world"...
- gaius 15y agoAnd it's from March 2010 but there's no Part 2.
- genieyclo 15y agoThe link for Part 2 seems to have been removed from the site, someone linked Part 2 on G+[0] on Lamernews, where I originally found this part 1 as well. [0] https://plus.google.com/113789777314020158193/posts/8SekPWzxkjS https://plus.google.com/113789777314020158193/posts/8SekPWzx...
- ash 15y agoIt's a shame Oracle moved Ksplice blog after its purchase. This move appeared to be slow and not complete. Part 2 was originally here: http://blog.ksplice.com/2010/04/libc-free-world-2/ http://blog.ksplice.com/2010/04/libc-free-world-2/
- wolf550e 15y agohttp://timelessname.com/elfbin/ http://timelessname.com/elfbin/
- rachelbythebay 15y agoI thought of this one instead: http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm...
- unoti 15y agoWhy do we use xorl to store 0, but mov for 1? mov $1, %eax xorl %ebx, %ebx # Why not mov for this one? mov $0, %ebx # Wouldn't this work as well? I'm guessing mov would work in either case, but there's some reason people always do the xorl instead?
- ori_b 15y agoPerformance. For this example it doesn't matter, but if you're trying to optimize something in assembly it tends to be a good habit.
- grimlck 15y agoThe xorl instruction uses fewer bytes than the mov. (The movl instruction encoding has the bytes needed for a 32 bit constant, while the xorl instruction encoding would not)
- bdonlan 15y agoThe xorl is one byte smaller, and can be faster. On modern CPUs it's more about the compactness of code than speed, but on some embedded CPUs you can save a cycle or two this way.
- alexscheelmeyer 15y agoBoth will work. The xor version is an old trick that many years ago was faster to execute. I do not think that is still the case, but there is still the difference that the XOR opcode is only one byte and the mov version is multiple, so it saves a few bytes in the executable.
- jevinskie 15y agoMore importantly, these days, is that it saves a few bytes in the caches.
- deleted 15y ago[deleted]
- maximilianburke 15y ago
- tptacek 15y agoIt's not "hello world" if it doesn't print "hello world"; the runtime doesn't even need to know about "hello world" in this "version". It's not even that much harder to actually print something without libc; just include your own stub for write(2).
- dbbo 15y agoI think the `string' quoting format in the ld output is breaking their "syntax" highlighting.
- 0x12 15y agoNearly 100 upvotes for a page that fails its stated goal and which spends 100's of lines to do something you could do with one system call (sys_write) and a string constant? http://asm.sourceforge.net/intro/hello.html http://asm.sourceforge.net/intro/hello.html
- scott_s 15y agoThe stated goal is not the real point. The real point is to learn about how programs are executed on real systems. Unfortunately, the second half which would (hopefully) have completed the example does not exist. What you linked to is great, but it uses a different approach to explain the subject. That approach presents the correct thing to do, and explains some of it. The benefit of this approach is that it's short and sweet, but the downside is it's easy to miss subtle points. For example, what you linked to never explains why you have to call sys_exit. Someone reading this would know that's the right thing to do, but they may never really understand why that's so. An alternative approach - which the posted article does - is to try the obvious but wrong thing, then explain why it fails. Fix that problem, and try the next obvious but wrong thing and keep iterating. The problem with this approach it can be a long process, particularly if you just want to know how to do something and are less interested in the why. But the benefit is that by seeing how things fail, and then seeing the fix for them, the reader can gain a deeper understanding of what's going on. In general terms, one is more of a reference, and the other is more of a lesson. I like the try, fail, fix iteration for explaining things. I've used it many times when teaching. I find that it matches well with what we would actually do on our own, and for that reason, tends to stick with people better than simply saying "this is how to do it."
- 0x12 15y agoI'd be more inclined to sympathize if it had actually worked. As it stands it is a perfect example of how little knowledge of what goes on under the hood is present with the current generation of programmers. That's sad, because even if you never use that knowledge in your 'day job' I think that such knowledge does make you a better programmer. If you write a blog post about something like this at least finish the damn thing rather than to leave a bunch of obviously wrong snippets laying around to confuse whoever lands on that page. There are plenty of good pages on introductory assembler on the web, this isn't one of those and I'm really surprised to see it this high on the homepage. Maybe it shows how much the HN crowd would like to get a little bit of insight of what actually powers their computers, the error is mine in assuming that such knowledge would be commonplace here.
- ChuckMcM 15y agoIts hard to express how sad reading that made me. The second thought was, what the heck are people being taught in school these days? Can you get a CS degree these days without writing a compiler? Seriously? As a tutorial on a blog about Arduino's or some other processor family, great stuff. As a blog entry from a company like Oracle, not so much. And as others pointed out, had the person approached it from first principles they would have arrived at the equivalent of 'puts()' a lot sooner.
- toksaitov 15y agogcc -m32 -static -nostdlib -x c - <<< 'int main(void) { int s = 12; char *m = "hello, world"; int n = 1, d = s - 8; asm("int $0x80"); d = (n = 0) + 1; asm("int $0x80"); }' && ./a.out # Hello from a libc-free world on Darwin 11.2.0, GCC 4.2.1, x86
- runn1ng 15y agoThe article is for march 2010. I don't think there will be part 2.
- jgenx1 15y agoarchive.org has a copy of Part 2: http://web.archive.org/web/20101023104654/http://blog.ksplice.com/2010/04/libc-free-world-2/ http://web.archive.org/web/20101023104654/http://blog.ksplic...
- achille 15y agoPart 2: https://plus.google.com/113789777314020158193/posts/8SekPWzxkjS https://plus.google.com/113789777314020158193/posts/8SekPWzx...
- worldimperator 15y agoThere are also smaller implementations of libc, eg. http://www.fefe.de/dietlibc/ http://www.fefe.de/dietlibc/