Inside every computer, there is a processor. It is a combination of complicated and powerful technology compressed in such a small space and can run arbitrary instructions. Therefore, today we’ll examine one of its most remarkable properties, which is its ability to execute code.
Turning the Program into Machine Code
You cannot run source code directly. Instead, you need to turn it into machine code before the processor can execute it. There are two types of programming languages based on how it’s converted into machine code. The first type is interpreted languages, whose interpreters change the source code into machine code at runtime. The second type is compiled languages, whose programs must be compiled before running. There is a more detailed explanation here so that you can check it out to gain some insight into the amazing process of code compilation.
The Fetch-Decode-Execute Cycle
To run an instruction, processors must fetch it first. A command mentioning the memory address of the line it needs to process is sent to RAM. The RAM then retrieves data from that specific memory address and then sends the line of code back to the CPU to further process it.
After that, the processor decodes the instruction, determining the opcode and the operand. The opcode is the basic function that it needs to run, and the operand is the data that is supplied to the function. Note that the opcode varies with instruction sets, so different compilers are needed for different CPU architectures.
The processor will execute the instruction after determining what it needs to do. Then, it will feed the data into logic gates, switching on certain switches and leaving some switches in an off state. Subsequently, the AND, OR, and NOT logic gates will bring the processor closer and closer to its final output until an array of bits are produced and fed back into RAM for storage and for the processor to further process it later.
Then, the CPU finishes its job with one line of code. What it does is restart the cycle to keep the program running. It fetches the next line, decodes the instruction, and executes it. This process continues from when the computer starts up to when the device shuts down because programs are always running in the background when the computer is turned on.
Conclusion
In this short article, we explained how processors execute code. Remember that modern processors can repeat this cycle at a whopping rate of billions of times per second, which enables you to use your computer smoothly. Remember that using your computer requires billions and billions of instructions to be executed, and the incredible machinery inside the processors enables this process.
References and Credits
- Jayric Maning. (2022, January 3). How Does a Computer Execute Code? Retrieved February 26, 2022, from https://www.makeuseof.com/how-does-a-computer-execute-code/
- (n.d.). Fetch-Decode-Execute Cycle. Retrieved February 26, 2022, from https://www.futurelearn.com/info/courses/how-computers-work/0/steps/49284
- (n.d.). Software: Running Programs. Retrieved February 26, 2022, from https://web.stanford.edu/class/cs101/software-1.html