Skip to main content

How do programs work on a computer? (Part 1/2)

Let's take a very simple program for understanding how this system works. The following example is a very basic C code that assigns 2 variables (x and y) and then adds them to generate a value 'z'.  Note: We do not output anything in this program void main() { int x = 3; int y = 4; int z = x + y; } But we have a pressing question. Computers inherently work on electronics and logic, so how does it understand and execute the program? The answer is not very simple and you have to bear with me through a tiny journey. Basic Logic Circuits Let's first begin with a very basic Circuit which implements AND logic and OR logic (skip if you are familiar)  As seen in the circuits above, for the left circuit, the LED lights up only when both switches are on. This is AND logic as "Switch 1 AND Switch 2 need to be on". In the circuit on the right, either of the switch will switch on the LED. Note that in the

How do programs work on a computer? (Part 1/2)

Let's take a very simple program for understanding how this system works. The following example is a very basic C code that assigns 2 variables (x and y) and then adds them to generate a value 'z'. 

Note: We do not output anything in this program

void main() {
    int x = 3;
    int y = 4;
    int z = x + y;
}
But we have a pressing question. Computers inherently work on electronics and logic, so how does it understand and execute the program? The answer is not very simple and you have to bear with me through a tiny journey.

Basic Logic Circuits

Let's first begin with a very basic Circuit which implements AND logic and OR logic (skip if you are familiar) 
As seen in the circuits above, for the left circuit, the LED lights up only when both switches are on. This is AND logic as "Switch 1 AND Switch 2 need to be on". In the circuit on the right, either of the switch will switch on the LED. Note that in the right circuit, the LED is on even when both switches are on. This is OR logic. "Switch 1 OR switch 2 need to be on". For now, ignore the higher brightness that occurs in the right circuit when both LEDs are on. We will discuss that later. Similar to this, but with a bit more complicated circuits, we can make a lot more such logic. You can have AND, OR, NOT, XOR (Exclusive OR), NAND (NOT AND), NOR (NOT OR), XNOR (Exclusive NOT OR) etc. More details

These gates work on binary logic. That is, whether a bulb is on or not and not what brightness it is at. This is done by setting a threshold minimum voltage for considering an 'on'. For example, we can define that for a 5V circuit, anything below 1.7V is off and anything above that is on. This also takes care of our problem that the bulb was brighter when both switches were on.

Binary and Binary Addition

Now that you understand basic logic gates, we see how we can add binary numbers. But let's first understand what binary numbers are. The world we live in, uses the decimal system. And usually, the world around us also works in the same way (1000m is 1km and 1000g is 1kg) where everything is a multiple of 10 (except for the United States, who for some reason, still live in the past with the imperial system). So, if you want to add 2 numbers, say 17 and 15. Here's how you do it:
7 + 5 (units digit sum) = 12. Because we have decimal, you read it as 10+2 (multiples of 10). The 10 gets carries to the next numbers and you have 1 + 1 (ten's place) + 1 (carry over) = 3 Final Answer is 32
    1
    15
  + 17
    --
    32

Now, in decimal system, your numbers range from 0 to 9. Every other number can be created by multiplying these numbers with 10 and adding them. Simple example would be 937 can be presented as 9*10*10 + 3*10 + 7 = 937
Similarly, binary system is for the base 2 (instead of 10). Hence, the only numbers you end up having are 0 and 1. Hence, you have a number like 1101. Yes, that looks exactly like what a hacker is doing in a movie, but let's not freak out. Instead, let's break it down as follows:
1*2*2*2 + 1*2*2 + 0*2 + 1 = 8 + 4 + 0 + 1 = 13
The numbers in bold and underline are from the number 1101 and the rest is just 2 multiplied 'n' times, where 'n' is how much on the left this number is. It's similar to "ten's" or "hundreds" or "thousand's" place. We choose this because, as seen in the diagram above, it's easy to just inform electronics of 'on' and 'off' rather than numbers from 0-9.

Now that you have some idea of how binary works, let's add 2 numbers in binary: 101 + 11
     11
     101
   + 011
    -----
    1000
The rightmost would be 1 + 1 = 10 (remember, in binary, you carry when you sum is 2. Hence 1 + 1 is not 2, but 10 and 1 will be carried). The next sum now becomes 0 + 1 + 1 (carry) = 10. The third one also goes the same way and the final answer is 1000
If you use the method above to convert it back to decimal like we did 1101 (binary) = 13 (decimal), you can see that we are adding 5 + 3 and the answer is 8 as expected.

Logic Circuits

This shows how you can design a 'half adder'. A half adder is just a circuit that adds 2 binary single digit numbers and gives you a result (sum and carry). If you do not understand the symbols in the gates shown, refer to this again. Similar to a half adder, you also have a 'full adder' as shown here. And these are just the beginnings. You can design subtraction, division, multiplication and a lot more, by just using these basic gates. And this is how a calculator works.
But we were learning about computers, right? Well, yes. In a very reductive sense, a computer is just a fancy calculator (don't hate me). But to make a calculator do what you do on a daily basis on a phone laptop, it needs to add a lot more functionalities built on top of these logic circuits. Let's go a step further. As you see above, 2 half-adders can make a full adder. And with the same logic, you can make adders that can add 2, 4, 8, 16, 32, 64 etc number of binary digits together.
Now, let's go back to the code at the beginning of this article. I want to do z = x + y, which is z = 3 + 4. Let's break this down. Forget about x, y and z for now. Instead let's just assume that all I want is 3 + 4 = 7.

Based on the above logic, you can design an adder that accept 3 (or 4) inputs and gives you a multi-digit output which can do the following:
  011  -> 3 in binary
+ 100  -> 4 in binary
  ---
  111  -> 7 in binary

Seems pretty simple if someone can somehow give it the input of 011 and 100. But for the sake of understanding in a flow, let's assume that this input is static. That is, there are literally 6 wires, of which some (representing 0) do not have any potential difference across them and others (representing 1) have some potential difference across them. As you remember, 0V is 0 (off) and >1.7V is 1 (on).
Now, your every-day computer (or even calculator) needs to be told what to do with these numbers. And that's where an 'op' or operation comes in. For now, assume that your over-simplified calculator just has add and multiply functions. You can have a simple switch in between which states, that when the switch is off, do 'add' and when it's on, do 'multiply'. You can have a separate circuit for add, which accepts 6 lines of input (in this case) and a different circuit for multiply. Now, the 6 lines of input will be 'forwarded' or connected to the choice you made, based on the switch position. And the output you get is either 3 + 4 = 7 or 3 * 4 = 12, depending on the switch position.
Simple? Now you can extend this across many other functions like division, subtraction, power of, and others. This is how a basic 'instruction' can work (the reality is a bit complicated as we will see)
Next, we look at how do we specify which numbers we want to perform operations on. For that, we need a circuit that can store the number we provide it with. We also need the ability to change that stored number. Let's look at a simple circuit that can do that. This is called an SR Latch
In the above diagram, S = Set and R = Reset. Check the output of Q. When you send '1' (on) to S, Q becomes '1' and remains '1' (can you figure out why?) and when you send a '1' on R(reset), it becomes '0' and remains so. So, now we can store a single bit and we can just make as many of these as we want to store out binary number. Note: Sending a '0' on both S and R is invalid and sending a '1' on both will mean that the state remains as it is, and random if this is the first contact
So, now, we can store the numbers 3 (011 in binary) and 4 (100) in binary using 6 such latches and then supply them to the adders we made before. This concludes the first part of the system, where we can somehow store a variable and then send it off to perform some operation and get some result back.
 
In the next post, we will learn about how we convert the human readable code given at the start of this post to something that can run on these electronic circuits.

Comments

Popular posts from this blog

Dynamically extending the Linux Kernel! An introduction to eBPF

A simple Google search of "eBPF" will tell you that it stands for "extended Berkeley Packet Filter". The words "packet filter" in the name makes me think this has something to do with Computer Networks. But, while it started as a packet filter system, it has extended much beyond that functionality since then. Facebook also had Whatsapp, Instagram, Oculus etc., under its belt, and hence the board decided to rename the company to Meta to symbolise this. In the same way, maybe eBPF should be renamed to represent what it is! So, what exactly is eBPF? To understand eBPF, let us first understand why it is required. A short video explaining eBPF can be found here . Pre-requisites In this journey, the first thing to learn is the difference between userspace and kernel space. This  sub-section of my previous post gives you an overview. As mentioned there, you don't want your user application to have access to any data of any other running application unless spe

The future of Web Acceleration and Security #2: DPDK

Most of you would have noticed by now that my content is heavily focused on Computer Networks and systems. And yes, I love these subjects (although my grades might say otherwise). I started this blog because the technology I deal with on a day to day basis usually does not have a tutorial targeted at people who do not already have multiple years of relevant industry experience under their belts. It is not trivial for most newbies (including myself) to understand concepts like SmartNICs , eBPF  and other stuff. And while writing this blog, I try to be as accurate as my knowledge allows me to be. Source and Attribution That being said, let us first understand the limitations because of which DPDK has become necessary. Pre-Requisites Before we begin, you should already know the basics of what a network packet is and how it's transmitted. If you are not aware of it, please visit the links below (both are necessary): Life of a packet Life of a packet - Deep Dive Now that you have unde