Live data from Hacker News

Main is usually a function. So then when is it not? (2015)

jroweboy.github.io

1–10 of 59 posts

Re: Main is usually a function. So then when is it not? (2015)

#4
post #3

Back when compiler warnings possibly cost extra processing time to generate, it was possible to make gcc compile the craziest things. After much experimentation, it turned out that the smallest program that would compile and run was only 5 bytes long: main;

> make gcc compile the craziest things

In languages like Haskell you can just make 2+2=5: https://codegolf.stackexchange.com/a/28794

Re: Main is usually a function. So then when is it not? (2015)

#5
post #3

Back when compiler warnings possibly cost extra processing time to generate, it was possible to make gcc compile the craziest things. After much experimentation, it turned out that the smallest program that would compile and run was only 5 bytes long: main;

> make gcc compile the craziest things In languages like Haskell you can just make 2+2=5: https://codegolf.stackexchange.com/a/28794

That’s not that crazy. It just shadows the global + function. And thanks to lexical scope it has a rather limited impact.

Re: Main is usually a function. So then when is it not? (2015)

#7
Complete final program for the laziest of us (after incorporating @10000truths's advice):

  const int main[] __attribute__ ((section(".text"))) = {
      -443987883, 440, 113408, -1922629632,
      4149, 899584, 84869120, 15544,
      266023168, 1818576901, 1461743468, 1684828783,
      -1017312735
  };
Compilation (gcc 13):

  $ gcc -Wall main.c -o main
  main.c:1:11: warning: ‘main’ is usually a function [-Wmain]
      1 | const int main[] __attribute__ ((section(".text"))) = {
        |           ^~~~
  /tmp/ccsWmdiD.s: Assembler messages:
  /tmp/ccsWmdiD.s:4: Warning: ignoring changed section attributes for .text
Execution:

  $ ./main 
  Hello World!

Re: Main is usually a function. So then when is it not? (2015)

#8
post #3

Back when compiler warnings possibly cost extra processing time to generate, it was possible to make gcc compile the craziest things. After much experimentation, it turned out that the smallest program that would compile and run was only 5 bytes long: main;

I still get a segmentation fault out of that when I run it. I think there's flags you can use to get the linker to not complain about missing main if you give gcc an empty file.

The craziest thing I got gcc (the AVR version, specifically) to compile for real purposes was a preprocessor macro that spit out tens of thousands of asm blocks with memory barriers and nop (do nothing) instructions with "PORTB = 1;" in the middle and "PORTB = 0" at the end. I needed it to bitbang out a clock signal to read an RFID tag on poorly documented hardware. (fun fact: the clock the cpu uses on an arduino uno is considerably worse and less accurate than the one that's on the board for the USB chip.)

Re: Main is usually a function. So then when is it not? (2015)

#10
post #9

The article says `lea` helps calculate the array relative address on AMD64. Why does the article say the problem would be tricky on 32-bit? `lea` is an old instruction. Thanks.

x86-32 doesn't have PC-relative addressing (`%eip`).

Btw, his code is wrong, it assumes pointers fit into 32 bits.

Post reply on HN