Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the CPU understand programming languages?

Programming books sometimes point out that the CPU doesn’t understand the C++ language (or any high level programming language). All the C++ statements must be translated into machine code before they can be executed. But who does the translation?

Oh, that’s no mystery, they say; the translation is done by the compiler—which itself is a computer program. But in that case, the computer is doing the translation.

This seems to me an insolvable paradox. The CPU (the “brain” at the heart of the computer) doesn’t understand a word of C++, yet it performs the translation between C++ and its own internal language. Isn’t that a contradiction?

like image 329
Mohamed Ahmed Nabil Avatar asked Dec 08 '22 22:12

Mohamed Ahmed Nabil


1 Answers

A large part of the answer is this: C++ source code (or any high level programming language) is stored in a text file, just as you might store an essay or a memo. But text characters are stored in numeric form. When the compiler works on this data, therefore, it’s doing another form of number crunching, evaluating data and making decisions according to precise rules.

In case that doesn’t clear things up, imagine this: You have the task of reading letters from a person who knows Japanese but no English. You, meanwhile, know English but not one word of Japanese.

But suppose you have an instruction book that tells you how to translate Japanese characters into their English-language equivalent.The instruction book itself is written in English, so you have no problem using it.

So, even though you don’t understand Japanese, you’re able to translate all the Japanese you want, by carefully following instructions.

That’s what a computer program is, really: an instruction book read by the CPU. A computer program is an inert thing—a sequence of instructions and data—yet the “knowledge” inside computer arises from its programs. Programs enable a computer to do all kinds of clever things—including translating a text file containing C++.

A compiler, of course, is a very special program, but what it does is not at all strange or impossible. As a computer program, it’s an “instruction book,” as described. What it tells how to do is to read a text file containing C++ source code and output another instruction book: This output is your C++ program in executable form.

The very first compilers had to be written in machine code. Later, old compilers could be used to write new compilers...so, through a bootstrap process, even skilled programmers could rely on writing machine code less and less.

like image 81
Mohamed Ahmed Nabil Avatar answered May 20 '23 11:05

Mohamed Ahmed Nabil