How does a computer understand code?

It doesn’t. Well, not in the way you think.

When I say “code”, you probably think of things like “if this, do that” or “variable x equals 0”. But that’s not how it works.

Sure, you can create a fully-functioning computer program from that logic, but the computer never really sees that code. It’s turned into something else before that can happen.

Some Backstory

At its core, a computer is a processor. This processor is made up of a incomprehensibly large amount of on/off switches called transistors. These transistors are basically your average light switch, except they’re microscopic. The processor can read these transistor’s positions, 0 being off and 1 being on. This is machine language (a.k.a. binary code), and it’s the only thing a computer understands.

I’m sure you’re thinking, “But how do programmers code in English?” Don’t worry, we’ll get to that.

Programmers got fed up with binary pretty quick. So, they invented a bunch of shortcuts that used 3-letter commands to represent common binary instructions. This is assembly language. Assembly language is hard-coded (that is, directly written) into the processor, meaning Intel has a different assembly language than AMD (although they’re extremely similar).

Assembly was still pretty tough to understand, so programmers began making high-level programming languages to make things even simpler. They optimized these languages for certain purposes. For example, Fortran (developed 1957) was a language especially suited for scientific computing.

These languages still didn’t look like what most programmers use today, until C (developed 1972) was released. C (and its “sequel” C++) are considered to be the most influential languages in history, with most Operating Systems and a very large amount of software being written in C.

From High-Level to Low-Level

A compiler is a program which takes one computer language and turns it into another, saving the output to a file for later use. For example, clang is a compiler which compiles C code into binary (high-level language to machine language). A complied program is called a binary (or executable) for obvious reasons.

Some languages don’t take advantage of compilers, but instead use interpreters. An interpreter executes the provided code line-by-line in real time by turning it into machine language one line at a time. Some programmers may prefer interpreters because it means they needn’t create separate versions of their programs for different OSes; the creators of the interpreter handle that burden. The downside is that interpreters tend to be slower than a computer executing a binary.

Why does this matter?

Knowing that writting code in a high-level language is complete nonsense to the computer until it’s compiled is crucial to understanding how a computer works. Becoming a good programmer means you need to know what the code you write actually does.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s