Tuesday 25 June 2019


Basic View of a Computer
Electricity to Executable-Logic


  • Matter is composed of atoms (we can go smaller than this but this should be enough for this question)
  • 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 (5Volts) 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:
88 F3 A4 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
Voila, our first coding language :)

  • 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.

Let's walk the other way, from software to electrons now

  • When you type Google <http://www.google.com/> in the browser and hit Enter key, an http request is sent from your browser (the client) to Google <http://google.com/> (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.

Overly simplified it ?? . But this is what it is in essence. There are tons of other things happening but mostly, it's different software programs interacting with each other (remember the movie matrix?).

Computers are man-made miracles of the highest order. No single person could have thought of all this. It has taken more than 50 years and millions of smart people to get to this point. Most computer programmers and professionals I've talked to, have an incomplete picture of what a computer actually is and (as you put it) how does electricity get converted to software.

Friday 18 March 2016

Referencing and dereferencing CRACKED

A little code that clarifies the whole * and & in C that confuses a few !


Snip-A::

#include <stdio.h>

main()
{
   int i = 30;
   *(char *)&i = 234;
   printf("%d",i);
}


In the above code i is defined as an integer variable telling the compiler to set aside 2 bytes of memory and place 30 in their, lets not getting the big-endian or little-endian way of bit-patterns in here, I'll simply assume simple binary representations in-memory

it would look something like this in the memory:
| 00000000 00011110 |  ----> 0x1004   // those are 2 bytes in memory at address 1004 to represent i holds 30

Lets evaluate the more interesting statement which comes next
&i // this is simple get the address of i which is 1004
while the compiler is looking to reference this address as what ? it gets that this is a character reference


For simplification, I'll use only code snips further to explain what happens in the computer memory (RAM) when we play around with *'s and &'s (a.k.a referencing and dereferencing)