views:

580

answers:

13

I need to write an extremely lightweight program (trying to get below 8Kb) that performs some simple math. The language also needs to be platform independent. Which language do you think would work the best? (Oh, and no frameworks allowed.)

+1  A: 

c or assembly (though you'll need to compile for each platform)

TCL is a scripting language that's cross-platform (not sure how lightweight it is)

I'd suggest Java but the VM probably thrashes your size requirements

Glen
assembly is very platform-dependent
Zydeco
sure, but 8k is a pretty low limit, so I'm giving him options
Glen
I agree that if all that's needed is simple math (meaning essentially no libraries, I/O, etc), C is the way to go.
Zarkonnen
+3  A: 

Would browser based JavaScript be an option? Lightweight and runs on almost all platforms via browser.

Conrad
Good call - I missed that one and it was really obvious!
David Robbins
Err, didn't he write that no frameworks allowed? If a web browser isn't a framework, I don't know what is.
Joonas Pulakka
Nope, can't go there. I'm actually a web designer myself so its the first thing I thought of.
Jack Mills
if webbrowser is framework for javascript, then CPU is framework for machine code ;-)
lubos hasko
@mad-j -- how is the browser a framework? At the end of the day code needs to run sowewhere!
Conrad
+1  A: 

Are you thinking of something like pico?

soulmerge
+3  A: 

Lua is cross platform and extremely light weight.

David Robbins
light, but not light enough. It's a 50k binary - though it could be hacked lower by removing what you don't need.
SpliFF
A: 

Not sure what you mean by framework??

python

why?

  1. It comes installed on my linux distributions, OSX and can be packaged as a native executable on windows. In other words the user of your app won't have to install anything
  2. It's really easy to learn.
Ed Sykes
windows doesn't come with python, so even if the py script is < 8kb, the entire thing will be a few MB's.
lyrae
ahh that's true, didn't think of that.
Ed Sykes
+6  A: 

C. Of course you need to compile separately for each platform, but other than that, it's quite light, and platform-independent (or multiplatform, whichever expression you prefer).

Joonas Pulakka
Be careful about the compiler though. Some (most?) will create massive executables (comparatively).
Brendan Long
He's talking about simple math and targeting Linux/Windows. I don't know what happens on Windows, but on Linux I can't imagine a non-broken compiler that you can't squeeze an 8KB binary out of for anything less than 150-200 lines of unremarkable C. Just be sure to strip the binary.
Nicholas Knight
There are different compilers out there, for sure, but I think most of them are quite adequate in this respect: C is essentially not much more than human-readable assembly language. In practice the executable size is largely determined by statically linked libraries; if you're not using such, what else could the compiler then put there to make the executables huge? C++ is a completely different animal, of course.
Joonas Pulakka
A: 

What about adobe AIR. You can develop using javascript anf make it a desktop application.

Shoban
+4  A: 

Which platforms are the program targeting?

So far, we have the following requirements:

  • Program must be under 8 kilobytes.
  • Must be platform-independent.
  • No frameworks allowed.

Here are some questions:

  • Can the 8 KB program be a script for a scripting language?
  • How big can the runtime environment for the program be?
  • Should the program be native code on its own?
  • Which target platforms should the code run on?
  • Is cross-compiling the code for each platform an option?

The questions that I've presented are going to affect the desirable options. If the program must be 8 kilobytes with the runtime environment, then there really isn't much of a choice other than compiling against the target environment.

If the target is an embedded device, or non-x86, then its likely that the choices will be further restricted. Small embeddable langugages such as Lua can still be used (written in C), but that would require a fairly large "runtime" for the script to execute.

If cross-compiling to the targets are an option, then writing a program in C and compiling to each target platform will probably yield fairly small native programs.

With the current requirements, there are a few questions that need to be addressed in providing a good comprehensive answer. Other than that, the best that can be done is a brainstorming of options that may or may not lead to a desirable solution.

coobird
You missed one of the requirements: 'no frameworks allowed', so I guess a scripting language doesn't count...
fretje
@fretje: I'm not quite sure a scripting runtime can be considered a "framework", but that's an additional requirement that was mentioned, so I'll add it to the answer. Thank you for pointing that out! :)
coobird
The program is targeting Linux and windows, I doubt I'd need to target anything else. No scripting languages allowed, really the program needs to be a standalone executable. I'll find out about the other questions
Jack Mills
+1  A: 

dc

It's basically an overblown calculator but it's 25K and zips down to 12K. It also does some string operations, macros and runs external processes. You won't find much smaller without writing it yourself. Source and win32 binaries available.

SpliFF
A: 

Bespin from Mozzila is an online environment. So it is, by definition, platform independent. Try at https://bespin.mozilla.com/

Varun Mahajan
+3  A: 

What about Forth?

Maxim Kim
If you can't get a good Forth environment in under 8K, you're not trying. :D
JUST MY correct OPINION
A: 

Even C requires a runtime. Only realistic answer to this question is dc, or even a custom calculator that executes scripts, implemented in C. (Such a calculator could be smaller than dc, if it doesn't depend on arbituary precision).

Arafangion
A: 

You'd be surprised how much you can get done in less than 4k of Java:

http://www.java4k.com/

Of course that doesn't include the JVM :-)

mikera