Hacker News new | past | comments | ask | show | jobs | submit login
Hello from a libc-free world (oracle.com)
117 points by ndesaulniers on April 7, 2013 | hide | past | favorite | 35 comments



A related and somewhat more entertaining article along the same lines: http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm...


When I read the article on Oracle's page, I was thinking of exactly this same article. Any change they are written by the same person?


I wrote an actual libc-free "Hello World" program for Linux: http://codepad.org/rUckf7jR

It should work as 32-bit or 64-bit and compile with clang or gcc. It compiles to about 11 assembly instructions.

To produce a fairly small file:

    $ clang -nostdlib -O3 -o helloworld -m32 helloworld.c
    $ strip helloworld
    $ strip -R .note.gnu.build-id ./helloworld
    $ wc -c helloworld
    428 helloworld


Why do people use the AT&T syntax for assembly language? It sucks and needs to die [1] [2] [3]. I would support an effort to remove it from the GNU toolchain.

[1] http://blog.reverberate.org/2009/07/giving-up-on-at-style-as...

[2] https://news.ycombinator.com/item?id=5498967

[3] http://forums.xkcd.com/viewtopic.php?f=40&t=19558


> Why do people use the AT&T syntax for assembly language? It sucks and needs to die [1] [2] [3].

It's just the syntax. Get over it. You get used to it in a few days, and using Intel syntax over AT&T doesn't make your programs any better.

> I would support an effort to remove it from the GNU toolchain.

That would break a lot of existing applications, removing it from the GNU toolchain would be the easy part.

You can still use .intel_syntax directive with the GNU toolchain or use another assembler like NASM or YASM. But in practice it's often easiest to just avoid hassling with assemblers and toolchains and use the AT&T syntax, despite the fact that you might not like it.


Bah. Coming from writing M68k assembly: Intel syntax needs to die. AT&T has some warts, but I still find it vastly more readable. x86 assembly is an evil enough mess without using a syntax seemingly designed to make it even more horrific.


Lots of people learn on AT&T syntax and don't know Intel syntax.


If you're going to be spending time writing/reading assembly there's a good chance you'll need to refer to the architecture manuals and, for Intel, those only use the Intel syntax. Matching the reference material I think should be reason enough to move away from AT&T syntax.


The entire ksplice blog is fantastic for interesting lower level insights and discussion.

Some personal favorites include:

https://blogs.oracle.com/ksplice/entry/strace_the_sysadmin_s...

https://blogs.oracle.com/ksplice/entry/attack_of_the_cosmic_...

https://blogs.oracle.com/ksplice/entry/much_ado_about_null_e...

EDIT: how do I format links nicely? markdown doesnt seem to work and help doesnt mention links


I remember reading the cosmic rays article but did not know it was also written by ksplice, neat! Also, I'm not sure how well HN supports markdown. I was able to use asterisks to get italics, but not square braces followed by parentheses for links. That was following github's cheat-sheet (go to github and press 'm' to see the markdown cheat-sheet).


It doesn't, only paragraphs and emphasis and code samples.

https://news.ycombinator.com/formatdoc


For a language with a C-like syntax that compiles into minimal x86 executables check out W [1]. It was designed for use in a small, single-executable self-hosting compiler targeting the HP 95LX family of early MS-DOS "calculators"/PDAs [2] and hence has no equivalent of libc; instead it expects you to code your own basic routines in assembly. I once wrote a bootable toy application in it that ran on regular x86 desktops (a command-line interpreter and calculator) and found the experience quite pleasant.

Here's a "Hello, World" in W:

    write := 0x8B55, 0x8BEC, 0x085E, 0x4E8B, 0x8B04, 0x0656, 0x00B8,
	     0xCD40, 0x7321, 0x3102, 0x8BC0, 0x5DE5, 0x90C3
    
    _() :=
    {
        write(1, "Hello, World!\r\n", 15)
    }
[1] http://www.vttoth.com/CMS/index.php/projects/49

[2] https://en.wikipedia.org/wiki/HP_200LX


You're not actually implementing your subroutines in assembly, but machine code.


Well, yes. I should have been more clear that it's a "bring your own assembler" kind of deal. You have put the machine code in hexadecimal in the final .W file before it's compiled. Back then I used a free early version of Hiew [1] to assemble the subroutine code for my project but if I did it now I'd either make some kind of a preprocessor to convert in-line assembly to machine code before the source is fed to W.COM (the compiler's executable) or modify the compiler itself to call an external assembler. You can do the latter since the compiler's source code [2] is licensed under the GNU GPL.

[1] http://www.hiew.ru/

[2] ftp://gateway.vttoth.com/pub/w.zip


Heh, kids these days. We needed to do that to even have a chance at competing in 4k intro competitions.


Ah the joys of getting Watcom C to work with PMODE [1] before PMODE/W made it easier.

[1] http://en.wikipedia.org/wiki/PMODE


Why would one use PMODE with Watcom when Watcom comes with DOS/4GW?


Size, ability to have a single executable, requirement to display their copyright, and probably a few technical ones I can't remember anymore; I vaguely recall stuff about DOS4GW being more strict with certain CONFIG.SYS and HIMEM settings, but it was almost 20 years ago. :)


Neat, do you still have any of your code from your 4k competition days lying around? I'd be curious to see what kinds of tricks you used.


Sadly, I don't have any of my own work anymore, although I'm sure I could write out a few procedural texture generators from top of my head.

Luckily, the demo scene has collectively decided to go back and open source much of it's best work recently: http://www.displayhack.org/2012/the-great-demoscene-sourceco...

The most recent 4ks are nothing short of stunning: http://www.youtube.com/watch?feature=player_embedded&v=0...


That is pretty amazing. I don't know the demo scene. Is it real-time or does the 4k do rendering that is viewed with a movie player?


Real time, it's a 4kb executable.

The demo scene has been around for decades


> Real time

Wow. That is super-impressive.


Is it still "Hello, world!" if that is never printed to the screen?


Good point! Just add some inline-assembly to have it call write:

  int main()
  {
      char *str = "hello world\n";
      int len = 12;
      __asm__ ( "int $0x80"
                :: "a" (4), "b" (1), "c" (str), "d" (len)
      );
      return 0;
  }


Not printing "Hello, World!" to the screen would be considered a LOGIC ERROR.



I once had an evening/weekend project to write C++ code on Windows without linking to the C and C++ runtime libraries. It was a lot of fun and it gave me some preparation for a next job that involved even more system-level stuff.



Interesting, I thought HN would only give you a point for re-posting something that had already been posted...


Notice that the older discussion had a nominally different URL.


posted Mar 16, 2010


Sorry, I guess it's not "news." But the guidelines don't mention that it has to be. And it's not really necrobumping since I'm not reopening an old thread, just posting an article I read that I found really interesting, regardless of its age.

From the guidelines:

"What to Submit

On-Topic: Anything that good hackers would find interesting. That includes more than hacking and startups. If you had to reduce it to a sentence, the answer might be: anything that gratifies one's intellectual curiosity."


Your comment adds nothing to this discussion, you didn't even link to the previous comments as the other poster did. What are you trying to achieve?


Could someone please explain to my how it is acceptable that oracle neends to plant cookies for me tor read this?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: