I want to compile a C program so simple I can explain all of the assembly
blog.ksplice.com
blog.ksplice.com
All these wonderful things are being built (Clojure, JRuby) on top of JVM that are of no use outside of web/EE because default JVM is so heavy.
Yes, a VM does a lot more than just bootstrapping your stdlib, like in case of libc, yet I keep thinking there must be plenty of unnecessary fat to strip off. Just look at Microsoft CLR: same feature set, yet none of that sluggish starting, RAM-wasting JVM nonsense.
We joke that doing anything in C# will result in an XML parser being included somewhere. This is not that far from the truth; e.g. calling float.ToString() will pull in whole internationalization system, which probably somewhere needs to read some global XML configuration file to figure out whether daylight savings time is active when Eastern European Brazilian Chinese calendar is used.
http://aras-p.info/blog/2009/11/14/improving-cmono-for-games...
The sad thing is that he's not kidding - if you profile a typical application under Mono or .NET, it loads an XML parser almost immediately.
http://blog.headius.com/2009/03/bitescript-001-ruby-dsl-for-...
http://blog.headius.com/2009/05/bitescript-002-scripting-exa...
http://blogs.sun.com/alanb/entry/is_the_jdk_losing_its
Somewhere in there is a comment that says the JVM loads around 300 classes to run an empty main class.
I'm curious to see what the merged HotSpot+JRockit JVM will look like in a few years.
If anything, CLR is more bloated than the JVM, not less. The libraries it uses just are used at startup, so when you start an app they are already in memory.
Note that I don't think that the bloatedness of JVM or CLR is a bad thing -- as long as you properly dynamically link, the cost of having them is quite minimal on modern hardware.
This tutorial walks you through making a kernel image that GRUB can load, and shows how to poke bytes into video memory to print characters to the screen:
While a little masochistic I find I still call upon what I learned back then while writing in C and to a lesser extent higher level languages.
Steve Wozniak, Founders at Work
Amazing full inspiring interview (I have read every interview in JL's book and it is by far my favorite)
(Come to think of it, I vaguely remember having in mind some of Isaac Asmiov's science essays when I wrote that -- one of the type in which he would trace the discovery of some principle through several refinements over the centuries. Those essays gave me an appreciation for the value of studying science in its historical context. But that's a much more general influence than what I'm referring to.)
I've used that page as a reference several times over the years.
As long as those abstractions don't leak, it isn't a big deal, but when they do you had better know what's going on down below.
The executable was 736 bytes: http://rythie.com/labs/cpuid.php
As a preview, to provide the correct C ABI to main() you need to zero the BSS (this is how your static variables get initialized), maybe copy the initalized data, call the constructors and destructors for C++ / C (gcc extension), provide memcpy() and others stdlib functions (which gcc will use even if you don't), etc. You can do all of this without assembly in C, but it does take some effort.
by Jessica McKellar
It's disturbing how many Pascal-isms still pervade my thinking, even 20 years since I last (willingly) touched the language.
I always imagined something 'called us', then just returning from that would be enough to get everything shutdown.
Certainly when I wrote 6502 code, or 68000 code, I would just return.
On the iPhone, you get advised you are shutting down, do your clean-up, then someone shuts down the message loop and you are gone.
We need to write our program like this:
$echo 0000000: 55 48 89 e5 b8 ff aa 00 00 c9 c3 |xxd -r > sum2.bin
Here we have the SAME little program in C: $ cat sum.c int sum(void){ return 0x00ff + 0xaa00; }
We can getLook at the results: $ gcc -c sum.c -o sum.o (get the raw opcodes in osx, intel arch )
$ otool sum.o -td|sed -n '3,$p'| awk '{ print $0}'|xxd -r > sum.bin
Now you can look at asm level your code: $ ndisasm -b 32 sum.bin 00000000 55 push ebp 00000001 48 dec eax 00000002 89E5 mov ebp,esp 00000004 B8FFAA0000 mov eax,0xaaff <-- gcc put our 'sum' final product here at compilation time 00000009 C9 leave 0000000A C3 ret
So your program is now reduced to this code: $ hexdump sum.bin 0000000 55 48 89 e5 b8 ff aa 00 00 c9 c3 000000b
Test your 2 files md5 sum.bin sum2.bin MD5 (sum.bin) = a0ccc94bcdc860a81ff28252f56c2257 MD5 (sum2.bin) = a0ccc94bcdc860a81ff28252f56c2257
We could probe our code with a selfmade userland loader: $./uloader sum2.bin Display Opcodes to exec: 55 48 89 e5 b8 ff aa 00 00 c9 c3 End opcodes code to exec address: exec_code =0x100100080 new crafted Proc : address = 0x100100080 returned value ==>aaff
----BEGIN Code--- #include <stdio.h> #include <stdlib.h>> #include <unistd.h> #include <fcntl.h>
int main( int argc, char argv[] ){ unsigned int (proc)(); unsigned int fdprog=0; unsigned int exec_code=NULL; unsigned char ptr=NULL; unsigned int returned_value=0x0; exec_code=(int ) malloc( 100 ); ptr=( char )exec_code; fdprog=open(argv[1], S_IRUSR ); printf("Display Opcodes to exec:\n"); while( read(fdprog, ptr, sizeof(unsigned char)) ){ printf(" %02x ", ptr ); ptr++; }
printf("\nEnd opcodes\n");
printf("code to exec address: exec_code =%p \n",exec_code);
proc=(unsigned int (*)() ) exec_code;
printf("new crafted Proc : address = %p \n",proc);
returned_value= (*proc)(); //here is the magic bro! :)
printf("returned value ==>%lx \n",returned_value); return 0; }
----END Code ---Saludos! Jorge A. Garcia.