7 ms·
"Hello, world" in Assembly.
- losethos 16y agoI put an asm hello world in my video http://www.losethos.com http://www.losethos.com but somehow I forgot to preserve RSI with a push and pop. Unintentional mistake. The example in the /LT/Demo/Asm directory on the disk is correct. I got away with it in the demo, but you really need to preserve like 8 registers. RSI,RDI,RBP and I forget...R15,R14, R13. Something like that, I forget. It's written down in the demo. I try to do as little assembly as possible and have converted as much of the original asm to C+. You are free to trash RAX,RBX,RCX and RDX when interacting with C+. That's usually enough registers, so I don't remember the upper register requirements. LoseThos is 64-bit but I decided all code must go in the lowest 2 Gig addresses so CALL with rel32 can always be used. LoseThos runs everything including applications in ring 0 kernel mode. It's much faster doing a CALL than an INT. That's not important when the operartion being done is big, I suppose. Kernel code is not distinguished from any other code. The JIT compiler/assembler calls MALLOC and puts the code in that spot. OSMain goes in the lowest 1 Meg region, but that doesn't mean anything special.
- zppx 16y agoIf you test it under OS X you need to use the OpenBSD/NetBSD instructions using the FreeBSD code but you will use a 'macho' object file.
- alnayyir 16y agoI take it you mean Mach-O? Mach-O is mach microkernel object format. http://en.wikipedia.org/wiki/Mach-O http://en.wikipedia.org/wiki/Mach-O In general, BSD assembler tends to be pretty stack heavy for cultural/engineering reasons. I don't think it ultimately matters all that much if you're fiddling around on your personal machine.
- ant5 16y agoIn general, BSD assembler tends to be pretty stack heavy for cultural/engineering reasons. I don't think it ultimately matters all that much if you're fiddling around on your personal machine. I'm not sure I follow?
- zaa 16y agoYep, "nasm -f macho hello.s && ld -e _start -o hello hello.o" did the trick for me on Mac OS X 10.6
- bcantrill 16y agoWhy is this news -- or even interesting? Perhaps assembly feels exotic to a dilettante, but to anyone with a computer science degree this was simply a part (and an early and basic part) of their education...
- losethos 16y agoYou seem unusually bitter. Are you hiding something. To any programmer, asm is a curiosity. I'm not saying it's useful, but why the strong hate. You have an agenda, don't you. I feel like I'm fighting the forces of darkness. Evil people. You sound like an Indian.
- sciolizer 16y agoComputer science curriculums vary wildly. And just because you were taught assembly and how a cpu works, doesn't mean you learned how to do it on unix or even x86. (My college taught with the spim mips emulator. I also taught myself dos assembly when I was younger, but this is the first time I've seen unix assembly.)
- Locke1689 16y agoNah, what you're seeing here is relatively poor, old, IA32, AT&T-style (which means GCC targeted), Linux-targeted assembly code. This is not the article I would use to learn about Linux, operating systems, or assembly (although it's more accurate to say you learn "an ISA" instead of "assembly").
- recoil 16y agoThe article uses Nasm not GCC (or rather, not Gas - though Gas supports Intel syntax too now, afaik), and is definitely using Intel syntax. Note for instance the unintuitive order of source and destination in the MOV calls and the lack of delimiters on the operands, amongst other things. [edit: clarification]
- barrkel 16y ago
- younata 16y agoSweet. We're covering Hello World in my software dev. class on wednesday.
- chbarts 16y agoThis is somewhat old: The sysenter interface on x86 Linux is no longer experimental, and it's faster than the old int 0x80 interface as well. [1] However, going through either is a bad idea. Use glibc or similar instead: [1] http://lkml.org/lkml/2002/12/9/13 http://lkml.org/lkml/2002/12/9/13 1. Bad input values will at least potentially set errno, which allows more enlightening debugging sessions. 2. You don't have to reinvent printf and whatever other useful functions that are provided by libc and not the kernel. 3. You shouldn't be writing that much of your code in assembly anyway. Use C or something for most of the bulk and reserve assembly for the bottlenecks. Anyway, here's more details on how system calls on x86 Linux work now: http://articles.manugarg.com/systemcallinlinux2_6.html http://articles.manugarg.com/systemcallinlinux2_6.html
- d_r 16y agoFor those interested in how this translates to Win32, there's a site with a wealth of tutorials for Win32 assembly that I had used just about 10 years ago. I just Googled and it still exists. http://win32assembly.online.fr/tutorials.html http://win32assembly.online.fr/tutorials.html
- mahmud 16y agoYesterday I submitted "A Problem Course in Compilation: From Python to x86 Assembly". An 80 page book with worked examples. It got ZERO up votes. http://news.ycombinator.com/item?id=1662430 http://news.ycombinator.com/item?id=1662430 Yeah, I am jealous.
- alnayyir 16y agoNot anywhere in any non-trivial population of people will you ever see anything of serious depth and novelty (aka, not some kind of cargo cult "classic") get nearly as much recognition as LOLcat/Hello World type material. This is one of my favorite communities, but it's not exceptional in that regard. That said, I like your link.
- billswift 16y agoI think it may be when a site transforms from basically being a resource to being considered a community. I effectively just said goodbye to Less Wrong, which I have followed longer than HN.
- ced 16y agoHN works best for discovery of interesting fields/material. That's why short pieces like the OP get up-voted: if someone hasn't looked at assembly before, they might learn a thing or two. The problem with what you submitted is that there's no easy way to verify its quality, because 1. The author isn't well-known and 2. It's too long (and there's little incentive) for someone knowledgeable to go through it and post a thumbs up in the comments. If I seriously want to learn about compilation, I'll just Google site:news.ycombinator.com compiler books, and grab the top recommendations.
- boredguy8 16y agoIf you want traffic and downloads, work through the book a little at a time as blog posts, with "learn more" type links to your book. That will both satisfy "the masses" as others said, and solve the underlying problem you're trying to solve. Goodluck!
- deleted 16y ago[deleted]
- pencil 16y agoi would say it's a nightmare programming in assembly without going through your tutorial.!!!
- praptak 16y ago"System calls in Linux are done through int 0x80. (actually there's a kernel patch allowing system calls to be done via the syscall (sysenter) instruction on newer CPUs, but this thing is still experimental)." This is no longer the case. While int 0x80 still works, this is not how well behaved binaries should syscall on Linux. The proper way is to call a magic address on a magic page in own process space, which contains the proper code to do the system call. It will execute int 0x80 on CPUs that don't support sysenter/sysexit and sysenter on those that do. The page (vdso) is inserted in all processes' address space by the kernel. Although its address is probably fixed, the proper way to find it is to examine the ELF auxilliary vector, which in turn is passed by the kernel via the initial process stack.
- xtacy 16y agoIntel archs have support for call gates that allow jumps to privileged code (ring 0/1/2). It maps nicely to the notion of syscalls, but I wonder why it hasn't been used.
- praptak 16y agoNot sure about the times before the sysenter mechanism (maybe int 0x80 was more portable?), but now that sysenter is in place it is faster than call gates, because most of its behaviour is hardcoded in the CPU itself.
- Hoff 16y agoHere's the "Hello, World" written in VAX Macro32 assembler: http://labs.hoffmanlabs.com/node/1435 http://labs.hoffmanlabs.com/node/1435 And if you're so inclined, there are (free) OpenVMS licenses for hobbyists, US$30 CD media kits, and (free) VAX hardware emulators for most any OS platform.
- js2 16y agoAlso, http://www.int80h.org/smallest/ http://www.int80h.org/smallest/ The rest of int80h is also interesting, but is FreeBSD oriented. The site presents its own Hello World - http://www.int80h.org/bsdasm/#first-program http://www.int80h.org/bsdasm/#first-program
- js2 16y agoAnd for posterity, Mach-O http://www.osxbook.com/blog/2009/03/15/crafting-a-tiny-mach-o-executable/ http://www.osxbook.com/blog/2009/03/15/crafting-a-tiny-mach-...
- js2 16y agoAnd here's a Linux version of smallest - http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm... Edit: previously submitted - http://news.ycombinator.com/item?id=68056 http://news.ycombinator.com/item?id=68056
- jerhinesmith 16y agoCan someone give some insight on the last line? That's it. Simple. Now you can launch the hello program by entering ./hello. Look at the binary size -- surprised? Without being able to compile the executable myself, is the binary surprisingly large or small? A few months ago, someone posted this article: http://blog.ksplice.com/2010/03/libc-free-world/ http://blog.ksplice.com/2010/03/libc-free-world/ Which leads me to believe that the executable is surprisingly large given the content. Am I understanding that correctly?
- billforsternz 16y agoI am only guessing but I suspect the binary will be tiny. The earlier article you mentioned described how hello world is surprisingly large with libc overheads. It then sets out to eliminate those overheads and concentrates on the problems involved in doing that. But it doesn't mention how small the binary gets once those overheads are successfully eliminated, My expectation would be just a handful of bytes.
- Garra 16y agoIn my case the binary turned out to be much larger than I would have expected. I was expecting a couple hundred bytes tops, but 8k? osx-box:dev esa$ nasm -f macho hello.asm && ld -e _start -o hello hello.o osx-box:dev esa$ ls -lh hello* -rwxr-xr-x 1 esa staff 8.1K Sep 5 22:43 hello -rw-r--r-- 1 esa staff 721B Sep 5 22:39 hello.asm -rw-r--r-- 1 esa staff 389B Sep 5 22:43 hello.o