Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

By the way, I would love any feedback on this approach and where I should take this further.


I'm surprised no-one's mentioned it yet, but this approach should go well with Zed Shaw's Learn C the Hard Way:

  http://c.learncodethehardway.org/book/


The error message says: Undefined symbols for architecture x86_64: "_main" (etc.) If someone truly knows nothing about C, how does that person go from that error message to "In the case for C, the entry point is defined by the “main()” function." I'm not clear on where the beginner is going to go to "dig deeper and understand what it is that it’s trying to say."


As for going further staying with the bare-bones approach, I suppose you'd have to start looking at assembly output and how that fits with what the c-code does. I don't know anything about OSX x86_64 calling conventions etc -- but at least under Linux (and afaik windows) 64bits is a lot more friendly and fun than the mess that was 32bit (and 16bit) x86.

There are a couple of great (free) resources on 32bit x86 assembly I'm aware of:

http://www.drpaulcarter.com/pcasm/ http://www.plantation-productions.com/Webster/HighLevelAsm/i...

There's apparently some plans on upgrading HLA to x86_64 -- I don't know of any good tutorials or guides on working on 64bit assembly specifically I'm afraid.

Just adding "-S" and looking at the source can be helpful of course, although I much prefer nasm/intel syntax, for clang/gcc that should require:

   clang -S -mllvm --x86-asm-syntax=intel main.c
   gcc -S -masm=intel main.c
Note that gas syntax is the "default" in the gnu-world, so it might be easier to just go with that if you're just starting out.

It looks like clang might be generating less "noise" for tiny trivial programs, here's a side by-side-diff (in intel syntax) of int main{} vs int main { return 0;} (slightly reformatted):

    diff --side-by-side main.s main.no-ret 
      .file   "main.c"                          .file   "main.c"
      .text                                     .text
      .globl  main                              .globl  main
      .align  16, 0x90                          .align  16, 0x90
      .type   main,@function                    .type   main,@function
    main:                       # @main       main:          # @main
      .cfi_startproc                            .cfi_startproc
    # BB#0:                     # %entry      # BB#0:        # %entry
      mov     EAX, 0                             mov     EAX, 0
      mov     DWORD PTR [RSP - 4], 0         <
      ret                                        ret
    .Ltmp0:                                   .Ltmp0:
      .size   main, .Ltmp0-main                 .size   main, .Ltmp0-main
      .cfi_endproc                              .cfi_endproc

      .section ".note.GNU-stack","",@progbits   .section ".note.GNU-stack","",@progbits
It can be fun to do this with stuff like hello world (and contrast puts("Hello, world!"); with printf("Hello world!\n);).


Intel x86 assemblers family are so much nice that whatever any UNIX system has.


e12e, this is something I didn't think about, but fits perfectly into me theme of "zooming out". Thanks!




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

Search: