Friday 3 July 2015

How Electricity turns into Binary Signals!


Matter is composed of atoms. Atoms have electrons and flow of these electrons is defined as electricity.
Now, to make use of these electrons, we create transistors which can store/free electricity as needed. They are stored in units of 1 (5 Volts) and 0 (0 Volts). An 8-bit number is then represented with 8 transistors. So 8-bit representation of the number 3 will be : 0000 0011. How is that achieved in hardware? Keep 8 transistors side-by-side (called registers and memory units). Make the first 6 transistors hold 0V and the next 2 transistors hold 5V. Now, an organization of such registers and memory makes a cpu+ram. To make it easy to compute using the CPU, we developed machine code. This language is what essentially runs on the CPU. What do I mean by "run"? It means, keep flipping bits. If I want to perform 2+3, in machine, I would store 2 in one register (register explained above) and 3 in another register. Then I would take these values to an Adder unit which would do a mathematical add (not the same as voltage addition) and give me the reply in another register. This is what a sample machine code would look like:
80 02 F3         
80 03 F4         
88 F3 F4 F5

Obviously, no one understood anything with this. So we came up with an ingenuous system to make it human readable. This is called assembly language. The following piece of code represents the above mentioned numbers:
MOVI 2, REG A 
MOVI 3, REG B
ADD REG A, REG B, REG C (add A and B and store in C)

where MOVI = 80
          REG A = F3
          REG B = F4
          REG C = F5
          ADD = 88

Now, assembly is too hard for humans to remember and code properly in. So they developed compilers that would convert a high level language like C to assembly language (remember, this assembly language does the actual flipping of bits)
So, a C representation of the above mentioned assembly would be:
{
    int a = 2; b = 3; 
    c = a+b;
}

Just like people could write poems with English and not with hand signs, we realized that with an expressive language, people could write some better programs. Then compile it to assembly. Then that would flip bits in registers. Which in turn would affect transistors, which affect flow of electrons. With the above found expressiveness, we wrote operating systems to maximize hardware usage, since it was seen that the CPU remained idle while we fetched data from disk. Everything from your keyboard input to mouse to desktop to windows to sound is a program written in such expressive languages, running on top of the OS. On the OS, we developed a network stack called TCP/IP. This stack provided a standardized methodology for computers to communicate with each other. Once that was working and we managed to hook computers to each other using cables, we went on to create WWW and http. This allowed people from different networks to communicate with each other. Note that http is a protocol. Servers and Clients are programs that follow (at least) http in addition to internal protocols.
When you type Google in the browser and hit Enter key, an http request is sent from your browser (the client) to Google (server). In your own computer, the browser is a program written in C/C++. This gets compiled to assembly (actually browser is already compiled, you're just giving input numbers to the compiled browser). The operating system (windows/linux etc) and device drivers are all already compiled to assembly and are running on your machine. When the browser assembly gets it's turn to run on the CPU, it runs the assembly. This assembly code does flipping of bits in registers and memory. The registers and memory are composed of transistors. Transistors control the flow of electrons and hence electricity.

No comments:

Post a Comment