For programs to run on a computer, does the code have to be converted into machine code so the CPU can run it? How does this happen?
-
That's how it mostly works, but that is not always the case. Sometimes code is not run directly on the hardware but is instead interpreted by another program - the interpreter - which runs the actual machine code. – 500 - Internal Server Error Oct 18 '16 at 23:31
1 Answers
Wow! That needs a lot to explain. :D First, machines like humans have their own languages so we can simply say that if you want a computer to work as you say, you have to say it in its language :)
But you probably heard about compiling and interpreting: Compile: convert (a program) into a machine-code or lower-level form in which the program can be executed.
So basically it means that the code code will be converted to something else like an executable file, when the programmer(s) decides that they are done programming. So if you look at an .exe file with notepad, you cannot simply understand any thing. and the code that has been compiled for windows, cannot be executed on Mac.
interpreting : the code will be converted by another program in the runtime. So the code is human readable until the last seconds. Like if you right click on this page and select "view page source", you can see the HTML code that has been generated for this page. This means that the code flexible and can work on different machines like as you see, you can see the same page on your Mac, windows or with different browsers like chrome, firefox or IE but then it will be a lot slower than compiling.
What we do in practice?
We compile our code to a an intermediate language that is understandable by a virtual machine that is specific for each machine.
Let me explain it with an example. Lets say someone wants to give a speech in UN lets say in Chinese.
If he translate all of his speech to different languages and give it to people, it is compiling.
If he speaks and some people translate his words online to French, English, etc. then it is interpreting. But it sucks and you probably won't find anyone to do it for many languages
If he give a translated version (like English version) of it to translators, before the speech and they can read it and say in to different languages when the speaker speaks, then it is what we do now :D
You can read more in here : Runtime vs Compile time
-
-
@DeathSwagga don't know why people gave you negative points. Probably because you've asked a very broad question but I don't know for sure :) – Ashkan S Oct 19 '16 at 07:54
-
-
@DeathSwagga good, your question got 2 positive points meaning that you've got 10 points :) it was probably the tags then. – Ashkan S Oct 19 '16 at 12:10
-
@DeathSwagga if you think that you've got your answer from my post, please mark it as answer and vote for it so the others can use it in the future – Ashkan S Oct 19 '16 at 12:11
-