looking under the hood of your programs, literally!

by

it’s common knowledge that high programming languages are different from one another. some are object-oriented while others require VMs. one way these languages differ is how “low” a particular language is to assembly. for those of you who don’t know what assembly is, i’ll put it this way: if you want fast software, you won’t find anything better then assembly. the general rule is that the closer a language is to assembly, the faster it’ll run.

Now, after this nice intro, i’ll get down to business. take a look at the overly simple-code bellow:

int main(){
        int i = 0;
        i+=1;
        return i;
}

you won’t find anything special other than the fact that the a preschooler could have written it. the real fun starts next: assuming you have gcc and gdb, go ahead and compile the source. then start the gdb:

        gcc code.c -o code.bin
        gdb code.bin

for the other 90% (windows users) : as far as i know, gcc is included with bloodshed dev-C++, the question is whether gdb is included or not. if anyone knows anything, leave a comment.

once running gdb, simply type disas main, and the compiled code will be shown in assembly:

(gdb) disas main
Dump of assembler code for function main:
0x0000000000400448 :	push   %rbp
0x0000000000400449 :	mov    %rsp,%rbp
0x000000000040044c :	movl   $0x0,-0x4(%rbp)
0x0000000000400453 :	addl   $0x1,-0x4(%rbp)
0x0000000000400457 :	mov    -0x4(%rbp),%eax
0x000000000040045a :	leaveq 
0x000000000040045b :	retq   
End of assembler dump.

If anything, this proves that c is fast. but more importantly, it shows how the program is compiled, and therefore, can be compared with other compilable languages to reliably determine execution speed instead of relying on unreliable benchmarking tools.

by the way, i stumbled on this little gem by accident, and I’m glad that i did. now i have proof that C rules🙂

SIGTERMer

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: