1

EDIT: Perhaps what I am misunderstanding is that when it is said that the code we type gets turned into machine code of 0s and 1s. If these 0s and 1s are the abstracted representation of their electric states, then I actually don't have a question (it is just amazing that a compiler can take English and turn it into a form that can impact the processor). If the compiler turns the code a file that contains literal 0s and 1s, then I don't understand the transformation steps steps that strictly occur between this file of literal 0s and 1s and the execution of the program (I understand how programs already in RAM are executed).

I have been researching for an answer to this question all day. I have read a lot about the computer's hardware and understand concepts like the clock, its different states that it "steps" through to execute a program, and (at a basic level) the compiler. I also know that the bits of each part of an instruction code are wired in a certain way so that it does what it is supposed to and the compiler takes the code we write and converts it to binary. But my question is this: how does the processor "understand" this binary (or machine readable) code.

In the computer there are no 0s and 1s obviously, and 0s and 1s are an abstraction themselves reprenting the presence or absence of electricity. So how does a 0 go in one end of the computer and a bit being off come out the other? I understand how this may work with input from the keyboard because I understand how computer peripherals work and get stored to some degree (the wiring underneath the keyboard accomplishes this as an extension of the wiring of the hardware that accomplishes everything that the computer does), but the compiler and the other (seemingly missing) piece of the puzzle that moves the computer to action seem to be a black box right now. Thanks so much in advance, always appreciate any responses.

One last time just to be super clear: I understand the compiler takes our code and converts it to 0s and 1s. That's great, but how does this 0 get interpreted, by a component and/or a process, that causes this abstraction (like the #0) cause one of the computer's bits to be in a different position.

Thanks so much once again. I have searched for hours about this question on this forum and other places and I can't seem to find an answer that moves past the: "yeah the compiler converts to 0s and 1s and those do some stuff". The how is what I am after.

12
  • 1
    Computer's don't work on "the ones and zeros" any more than a person reading books works on the "a to z"'s. The bits are all organized into groups that have meaning, just like letters grouped into words. Commented Jan 25, 2021 at 23:20
  • Thank you for the response. I understand the computer does not operate on literal 0s and 1s. However, machine code is in the decimal system (i.e., when code is compiled, the resulting file is literal 0s and 1s). So how does the computer interact with and execute the machine code and turn these literal 0s into its functional form as either the presence or absence of electricity?
    – steez
    Commented Jan 25, 2021 at 23:46
  • @steez, in your last comment did you mean in the binary system?
    – Erik Eidt
    Commented Jan 25, 2021 at 23:55
  • Yes I did, apologies.
    – steez
    Commented Jan 25, 2021 at 23:56
  • I think you have the fundamental misunderstanding that there are literally 1's and 0's in the physical hardware, whether on disc or in the CPU. There are physical values that are used to represent 1's and 0's but are not actually or literally 1's and 0's. Thus, the interpretation of physical values as 1's or 0's is up to the kind of device (e.g. rotating rust, vs. dynamic ram vs. CPU registers).
    – Erik Eidt
    Commented Jan 25, 2021 at 23:57

3 Answers 3

3

CPUs have instruction sets, add two numbers, jump forward 3 instructions, if this is true run the next instruction, if not skip and instruction etc

https://en.wikipedia.org/wiki/Instruction_set_architecture

The 1s and 0s are stored in the memory of the computer and grouped together and read as instructions or data, ie run instruction number 3 or data ie take that bit of memory there and do some operations on it.

Essentially when you compile code you are turning it into combination of these very basic instructions which are stored on disc, loaded into memory and then run by the cpu.

Some of these instructions or areas of memory will map to pins on connectors to devices, such as a graphics card, or disc drive etc and their state, on or off will cause the device to do things.

14
  • Thanks for the response. What I am asking, more specifically, is that these "0s" and "1s" aren't really 0s and 1s, they are the presence or absence of electricity. So when the code is compiled into binary, the file is literal 0s and 1s. So how do these literal 0s and 1s then get transformed to the their matching electric states?
    – steez
    Commented Jan 25, 2021 at 23:38
  • 2
    @steez, The file is never literally 0's and 1's -- these values (1 & 0) are logical values, and they are encoded into some physical form on media (e.g. disc) where the media knows what physical values mean 1 vs. 0. Then they are transmitted to the processor or memory via interfaces that also have physical wires where 1's and 0's are differentiated by their voltage.
    – Erik Eidt
    Commented Jan 25, 2021 at 23:52
  • Ok this is the best answer so far, thank you. So when you type out each letter of a code like Assembly, each letter you type gets stored physically and then when there is an entire word that denotes some action, the individual characters are wired in a way that performs the desired operation?
    – steez
    Commented Jan 26, 2021 at 0:40
  • 2
    or rather, all files, all data on a digital computer is allways "1s and 0s" regardless of where the file is displayed as binary or not. some chip of memory or magnetic media has the on/off states which encode the data. The transform is what pixels you turn onnor off on the screen when combinations of the states are read
    – Ewan
    Commented Jan 26, 2021 at 7:57
  • @steez "the individual characters are wired in a way that performs the desired operation" - not quite; the CPU doesn't understand the words/concepts we use in our programming languages. The compiler translates what we say in code to simple instructions. E.g. I might say drawLine(pointA, pointB), meaning "draw line between two points", but the CPU doesn't understand that. It's more like "go to this memory location, change the number, move to the next, change the number, move to the next, change the number, ...". It "understands" a very limited set of things supported by its circuitry. Commented Jan 26, 2021 at 20:41
2

A compiler operates at a logical level, not concerning itself with physical storage, but rather with groupings of 1's and 0's — several different kinds of groupings.  The first grouping is text files aka source code, and the second grouping is machine code.

Text is stored on disc, for example, as a sequence of bytes, where a byte is a group 8-bits that is ordered from most significant to least significant (or vice versa — that's up to the media;  each bit of course is physically encoded in the media as physically measurable values differentiating 1's from 0's).  Each byte represents a number: the number represents an ascii or UTF-8 value.  This is a logical encoding or mapping of logical characters into logical bytes — a layered encoding well above the physical encoding of bits in media.

Machine code is also a logical encoding of command operations to numbers — also well above the physical encodings of bits in media.  It can also be stored on disc as a sequence of bytes.

When the compiler recognizes a program's syntax according to the grammar of the programming language, it builds an internal object diagram of that program.  Later it traverses that object diagram in order to generate commands in machine code to instruct the processor to follow the steps of the program.

Machine code is designed to be easily decoded (at least by comparison to other encodings) — usually there's a first field that is an opcode field that tells the processor something about how to interpret additional fields of the instruction.  The compiler knows these encodings and generates them for the processor such that it will perform the given program.

1
  • This was very helpful, thank you for your thoughtful responses.
    – steez
    Commented Jan 27, 2021 at 5:23
1

But my question is this: how does the processor "understand" this binary (or machine readable) code.

It doesn’t. It’s turns numbers into other numbers and puts them different places based on still other numbers.

What those numbers mean is someone else’s problem. That someone sometimes thinks of them differently based on where they are. But they’re still just numbers. The processor doesn’t need to know what they are. Just how to transform them and where to put them.

So how does a 0 go in one end of the computer and a bit being off come out the other?

From a microphone or analog joystick being sampled or a keystroke at the keyboard, zero finds its way in through many different ways.

If that joystick has a 100k potentiometer in it you might think 100,000 values can come out. But if I’m sticking the value in one byte then only 256 values can come out. Because a byte only has 8 bits. This is called analog to digital conversion.

Another way is someone just types in a zero at the keyboard because they want a zero. They can do it hex, octal, ascii, heck even binary if they want to be silly. They know where they want the zero and they know what it will mean. Doesn’t mean the processor does. All it knows is what it’s supposed to do with it.

What we programmers do is use the rules that the processor follows blindly to get some useful work done. That might be letting you see and edit what you’ve typed on the screen before you put it on paper. It might be hearing what that zero sounds like when a sound card plays it on the system speaker.

What gives that zero meaning in it’s different places is often something called an encoding. If we’re talking about ascii (or UTF-8) a zero often means this is the end of the string. (Pascal strings tell you how long they are in the first byte. For them zero means a zero length string).

But not everything is encoded text. Some stuff is just data and it doesn’t follow any set pattern to tell you what it means. It will only be meaningful to whatever created it.

It is impossible to simply look at a zero and know what it means. You need to know it’s context. The processor is no different.

The really magical thing the processor does is copy that zero around faithfully. It doesn’t let it turn into a 0.3 when no one is looking like analog stuff does. That’s the real reason we use 1 and 0. It’s on or off. Anything in between is noise to filter out.

But it takes humans to make that zero into something humans care about. The processor don’t care.

11
  • Thank you. I know that it doesn't literally understand binary code. What I am trying to get at is how code we write (in whatever human language) gets transformed into a form that makes something happen inside the CPU.
    – steez
    Commented Jan 26, 2021 at 0:04
  • @steez better now? Commented Jan 26, 2021 at 0:27
  • @steez, would it help to know that a computer does not literally translate from English? It translates from a specially designed language - a programming language, which is only superficially English-like - into CPU instructions. A stream of human-readable characters, typed or copied into the computer, is already encoded into binary data (often using an ASCII encoding), and the compiler simply converts (according to complex rules) that data into executable instructions (which are also a form of data).
    – Steve
    Commented Jan 26, 2021 at 11:41
  • The amazing part to me is how it recognizes the key words (or primitives) of the programming language. The human-characters that we see that execute an "add" function are already stored, but I am trying to get at how these are recognized based on our input. Is it just the keyboard input triggering certain switches being turned on and when you type "a" "d" and then "d" this triggers the execution of the code?
    – steez
    Commented Jan 26, 2021 at 23:29
  • 1
    Also, what you type in is important. Notepad will just make a file. That file might be ran later. A console will execute your line as soon as you hit enter. Either way can get to that add opcode. Commented Jan 27, 2021 at 1:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.