Virtual Machine (VM'a) - A short Intro
before we get into VM's, Let's talk about CPUs
You know how your phone or computer sometimes gets bogged down when you have too many apps and browser tabs open? Or when you're streaming video and it starts buffering like crazy? This happens because of - central processing units aka CPUs.
These little microchip brains are the master multitaskers that make sure everything runs smoothly behind the scenes. They're constantly juggling multiple tasks, from loading web pages to running your games and apps. The faster the CPU, the more responsive your device feels.
CPUs have come a long way from the early days of computing. The very first CPUs could only handle one task at a time. Can you imagine having to close your messaging app just to scroll social media? Now CPUs have multiple cores to divide up the workload, along with other tricks to zip through tasks at lightning speeds.
This is the chip that executes program instructions and processes data. The CPU relies on another key component - computer memory - to store data, programs, and results on a temporary basis as it does its work.
It's easy to take CPUs for granted, but just imagine using your laggy old laptop again! CPUs may seem boring on the surface, but I promise they have some fascinating nerdy stories to tell!
[FUN FACT] : You also might be wondering why silicon for CPU and memory actually have to be separate, why can't they be in physical, same silicon? Turns out that the manufacturing process for DRAM is incompatible for the management process for logic. And so they can't physically put them on a same silicon, they have to actually have two separate silicones that have to go through separate manufacturing processes.
At their core, CPUs only understand one language - machine code. Machine code consists entirely of numbers that directly correspond to processor instructions. It's not at all human-readable! To make things easier for us, programming languages were created to abstract away the numeric machine code.
so, Machine code is this thing on the right. It's just numbers That's the only thing that CPUs understand. It's just numbers. And so the question then becomes well, how does it happen? Like this is crazy magic.
The thing in the left is what we call assembly, which is basically numbers converted to a friendly human-readable text. It kinda represents in between state. VM is translating the CODE you write into all these numbers that the CPU can go and execute.
Behind the scenes, virtual machines (VMs) create a simulated environment to run our code, separate from the actual hardware. The VM establishes a space in memory and a kind of virtual CPU with registers that act like quick-access local variables.
The reason for doing this is that real CPUs understand one low-level language - machine code. Different hardware has different machine code details. Rather than force developers to write timing and memory instructions in machine code for each device, VMs provide a consistent simulated CPU model across all platforms.
For Example : JavaScript started out slow in early browsers because it was interpreted at runtime, but modern JavaScript engines use just-in-time compilation and other tricks to make it much faster. V8 is the JavaScript engine inside Google Chrome which contains a virtual machine that simulates a whole computer environment to run JS code. This includes things like a memory heap, a virtual CPU, and registers to temporarily store values and references just like a physical CPU.
The key innovation is that instead of purely interpreting JS line-by-line, V8 compiles your JavaScript to optimized machine code right before execution. It does clever optimization and analysis to make your code run up to 10x faster. So when you load a web page, V8 translates the JS into efficient machine code that your device's real CPU can execute blazingly fast. It also manages memory allocation, garbage collection, and other tasks to create a complete virtual runtime optimized for JavaScript.
This all happens under the hood so web developers can just focus on writing JS without worrying about the nitty gritty machine details And so the VM has to create an illusion of all of these things for us.
And the way it all works is that you have a big array of memory, and you have a CPU. And CPU has what is known as registers, basically, those you can think of those as local variables.
Then there's the ALU, which stands for Arithmetic Logic Unit. This basically knows how to do math. Any kind of math you can think of. Multiplication, division, subtraction, whatever, math goes here, right?
CPUs rely heavily on their program counter register which simply contains the address of the next instruction to execute. On each cycle, the CPU fetches the instruction from memory pointed to by the program counter.
It does this by sending the target address over the address bus wires to the memory chips. Those chips then return the actual binary instruction code via the data bus to feed into the CPU. This retrieved instruction might do something simple like add two numbers or move a value
Based on the instruction type, the CPU will coordinate the data flow through various internal elements like the arithmetic logic unit to carry out the operation. It then advances the program counter to the next instruction address and repeats the whole process. So in summary, CPUs are really just giant state machines, The cycle of fetching and executing allows CPUs to quickly process billions of machine code instructions per second, enabling all the software magic we rely on.
So while the real CPU is still crunching ones and zeros, the virtual machine creates an intermediate layer handling things like memory management, optimization, and machine code generation. VMs provide portability and performance without developers having to code at the binary level.
Now when your application seems slow, you know there are two brains hard at work - the always-optimizing virtual machine and the blazingly fast central processor. Together they allow programmers to focus on productivity rather than micro-managing registers, caches, and assembly code instructions. Our modern, layered computing infrastructure enables innovation at new levels of abstraction.
with this you now know a bit about the fundamentals of CPUs, machine code, assembly language, and the role of virtual machines in modern computing.
if you have any doubts regarding this blog or just want to talk to me you can just send a dm x. Thanks for reading so far...
Subscribe to my newsletter
Read articles from Rohit Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rohit Singh
Rohit Singh
hey, I am a undergrad student studying the art of computer science.