How Programming Works – Behind the Scenes
July 28, 2010 9 Comments
A computer program is a sequence of commands executed by the Central Processing Unit(CPU) one after another. Those commands are generally very simple (likesums, multiplications, reading data from the Random Access Memory(RAM)), but are combined to do more complicated tasks. Typically a program consists of thousands to millions of such simple commands.
Machine Code and Assembler
The most basic level of programming is to write machine code (single commands to the CPU) directly. This is very difficult as a modern CPU typically has hundreds of different commands, each of which does a different thing and has different interesting properties.
But the main problem with this approach is not even the difficulty, but the fact that every different CPU model has a different command set, different strengths and weaknesses, and that it takes a deep knowledge of how the different hardware components and the used Operating System(OS)work.
To the other side, machine code is the only thing a CPU understands. Thus it is necessary to transform every program in to machine code before supplying it. The question is only whether we want to do that by hand or use a program to do it.
Writing machine code means writing in binary directly, not text. This is of course very tedious partly due to the fact that keyboards are made to write text with, and mainly that humans are more used to think in text than in binary.
While this was popular once (as it was the only way to go) it is now almost forgotten.
Assembler is nothing but textified machine code. Instead of having to remember the number of each command, you use names for the different commands. Of course each CPU still has different commands, and you need to have all the same knowledge as in writing machine code directly. Assembler is still widely used to write high speed code, or for the kernel of an operating system (where the hardware is accessed directly), or for embedded systems. As assembler is plain text it cannot be executed by the CPU; it needs to be transformed in to machine code first. This is done using a program called “Assembler”as well. To give you an idea, assembler looks more or less like this:
pushl %ebp
movl %esp,%ebp
subl $20,%esp
pushl %ebx
movl $2,-4(%ebp)
movl-4(%ebp),%eax
leal1 (%eax),%edx
Generally assemblers offer a few other options as well to make programming a bit easier, but it is still very difficult.It is definitely a very bad idea to try (and fail) to learn programming using assembler.
Both of these approaches require a lot of skills and have the big disadvantage that it is necessary to virtually completely rewrite a program to port to another CPU architecture or even OS.
High-Level Languages
Then there are the high-level languages. There are literally hundreds if not thousands of them , and it is impossible to give a description of all of them. Most of them share a similar approach, though. They are special languages similar to English (or to other spoken languages, although English is clearly predominant) which can be transformed into machine code doing what you asked for in that special language. For example, in BASIC the command
PRINT “Hello”
Will print Hello on the screen. This is of course much easier to remember than a sequence consisting of things like th eassembler code shownin1.2.1.
There area few things to remark about programming languages ( and computers in general), though. A computer looks at every thing in the form of ones and zeroes, not in form of “moreorless”. Thus it will do exactly what you tell it, not what you meant. And it will not accept anything with mistakes in it.
Fo rexample, it will not execute
PRINT] “Hello”
Even if it is obvious that the ] was only a typo. Computers are not forgiving at all. If a command like PRINT] exists,it will execute that one even if it is obvious that it is not what you wanted.
But high-level programming languages are not only much easier to use than assembler or machine code; they have other advantages as well. As they do not rely on a particular CPU architecture, but use a more general approach and they are much more portable. That means that the same program can be used on different platforms with no or little changes.
This still leaves us with one problem. How is a program written in some programming language (called the“sourcecode”) translated into machine code for a certain platform? There are two main solutions, both with their own advantages and disadvantages.
Interpreters
An interpreter is a program which reads source code, and executes the commands in it while it is reading it. There is no real translation in traditional interpreters; basically the interpreter knows what to do when it finds a certain command. For example, when a BASIC interpreter finds a PRINT command it knows that it must print the text which follows it on the screen, and soon.
This approach has the following main advantages:
• A program can be run immediately after being written.
• Everybody with a suitable interpreter can run your program, independently from your platform. It is not necessary to ship different versions of your program for different platforms.
• Everybody can read, analyze and modify your source.
But it has several disadvantages as well,and that’swhy interpreted languages are not so widely used anymore today:
Compilers
The other approach are compilers. A compiler takes the source cod eand translates it in to machine code, generally generating some kind of “executablefile” ( the “.exe” files under windows, or some of the files with the“executable”flag under Unix). The program can be executed only after the compilation has finished. Of course, if the compilation fails because there are errors in the source of for some other reason, no executable is generated and it can thus not be executed either.
The main advantages of compiled languages are the following:
• Acompiled program is real machine code and runs directly on the CPU. It is thus generally very fast.
• It is not possible to read and analyze the source code if you only have the executable. Thus, people cannot steal your code so easily.
• A compiled program does not need any supporting programs like interpreters. It is a standalone program.
• Compiled languages are generally more powerful than interpreted ones.
• Many errors which might not be noticed in an interpreted program will be detected when you try to compile the source.
Compiled languages are used for most software applications, like office suites or HTML editors or anything else you can think of. They are the only way to go for high-speed applications like 3D games or photo retouching programs. The most popular compiled languages areC++, JAVA and C#.
The most popular compiled languages are C++, JAVA and C#???
Can you plz explain this line Turab Bhai??? Lagta hai shayad sote hoye ye lines likhe gaye hai
Ha Ha Ha Ha
Par kia kia ja sakta hai seniors aisa karte rehte hai
shayad adat hai
Dear Waqas;
You absolute have no idea about C and C++….
Java is widely used at enterprise level only…
Most of the desktop applications are NOT built in Java despite its cross-platform in-dependency.
The Linux and all its distributions have their kernel written in C…’
and for your information, C is the most efficient programming language when to comes to compilation and running…. and hence much preferred at System software programming level..
Get over with Java fever, for God Sake…
Ha Ha Ha Ha Ha
Turab Bhai shayad ap ne question nahi parha? Question compiled language ke hiwale se tha na ke performance !!!!!!
Kia Java compiled language hai????
Ha C widely deployed hai is me kia koi abham hai
Ha Ha Ha Ha
Kenals are mostly in C even IPhone uses the Objective-C and all the core library of Android is in C. And JNI is java is for mostly porting C and C++ code to Java
HA HA HA HA HA
Zayada gussa acha nahi hota
Ji.. Java compiled language hi hai….
Apni correction kr lain….
Ha compiled hi hai lekin apne article ke is portion ko parhte hoe compile ke definition kare zara. Java source file compiled ho ke machine code me nahi convert hoti balke then JVM interpret krta hai
http://www.med.harvard.edu/JPNM/Java/Java101/Interpreted.html
Read this and you can refer else something even. Context ko be dekh le
What you are referring to is the Java Byte Code which is meant for JVM,
which eventually COMPILES the byte code to native machine code…
And here is what your article says
I guess Java is still a Compiled Language…
Ali bhai ap thk keh rahe
but if by complilation you meant ” the translation of source code into object code by a compiler. ”
then yes java is compiled language.
and if by compilation you meant ” the translation of source code into machine language by a compiler”
than sorry ali bhai you are totally wrong because java is all about byte code which is interpreted by JVM and what java interpret and execute on machine is not java.
i hope i have cleared my point
yup.. you’ve cleared your point…. Here is the deal!
You’ll understand difference between a compiled language and others when you
have a look at html, javascript n php… and then compared them to JAVA, C or C#…
html, javascript n php are never compiled to any form whether bytecode or machine code..
the term compilation DOES NOT refer to machine code translation…
Get the Point Here!
Compilation is simply translation of source code to another form whether byte-code or
machine code . or any other impractical or crap code… it just has to be compiled….
therefore java is a compiled language.. html, css, ,javascript and php are NOT because
they are never ever compiled or changed to another form, they remain in the form
they were written in (the actual source code)….
I know you’ll understand,, the other bloke just won’t
Ali bhai ap thk keh rahe
.
.
but if by complilation you meant ” the translation of source code into object code by a compiler. ”
then yes java is compiled language.
and if by compilation you meant ” the translation of source code into machine language by a compiler”
than sorry ali bhai you are totally wrong because java is all about byte code which is interpreted by JVM and what java interpret and execute on machine is not java.
i hope i have cleared my point