Sometimes it is fun just to write something for the sake of writing it rather than to have a useful end result. I was out for dinner with a group I used to work with and one of them is currently writing a game for the Sega Genesis. You would think that programmers would see how illogical writing a game for a “dead platform” is. Obviously the end result isn’t the point, it is the journey. Non-programmers might not get that sometimes the point of coding is having fun.
What coding do I consider fun? For me I have always had a fascination with minimalism. Small executable and efficient code have always been fun for me. I even lost marks on one of my exams in University because I made a mistake on my third iteration of optimizing the code to answer the question. The solutions weren’t even rated on their performance or size! Did I learn a lesson from that? Other than being correct is more important than speed, I continue to enjoy writing a small amount of code that can do more.
So…how do you make a small executable? I remember reading about a contest to write the smallest application that could download code off the Internet and run it. The author of the program was able to do this in something crazy like 138 bytes or something like that. I’m not interested in going THAT small, but I am interesting in looking at what you can do with a modern compiler like CL.
How big is the following code when compiled with Visual Studio 8’s (Visual C++ Express 2005) compiler?
int main(int argc, char* argv)
Step 1: Simple compile with CL.exe – 45,056 bytes
Seems like a lot of space for not doing very much. What should we trim next?
Step 2: Turning it to a windows program – 2,048 bytes
Modified the code a little:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int nShowCmd)
MessageBox(NULL, “Small Test”, “Small EXE“, MB_OK);
cl main.cpp /link /SUBSYSTEM:WINDOWS /ENTRY:WinMain /DEFAULTLIB:user32.lib
2kb is about as small as I would be looking for. The trick moving forward is avoiding anything that uses the standard library and static constructors. I still need to dig into more of what overriding the standard entry point does before I start writing stuff that is “useful” with this framework. Do I recommend anyone actually does any of the above? No, it is just for fun to see what you can do with small exes. Might add some rendering stuff next.