Processes are entirely operating system abstractions, not something CPUs natively understand or keep track of. It turns out CPUs have a super basic worldview they only see the current instruction pointer and a bit of internal state. (This was one of those psyching-myself-out moments for me - seriously, this is how the program you are using to read this article is running! Your CPU is fetching your browser’s instructions from RAM in sequence and directly executing them, and they’re rendering this article.) The CPU continues running its fetch-execute cycle as usual, so the program begins executing! The operating system loads this into RAM and instructs the CPU to jump the instruction pointer to that position in RAM. Let’s go back to the original question: what happens when you run an executable program on your computer? First, a bunch of magic happens to get ready to run it - we’ll work through all of this later - but at the end of the process there’s machine code in a file somewhere. One example is the instruction pointer, which can’t be read directly but can be updated with, for example, a jump instruction. ![]() Other registers are only used internally by the CPU, but can often be updated or read using specialized instructions. Some registers are directly accessible from machine code, like ebx in the earlier diagram. Each CPU architecture has a fixed set of registers, used for everything from storing temporary values during computations to configuring the processor. Registers are small storage buckets that are extremely fast for the CPU to read and write to. This instruction pointer is stored in a register. Some instructions can tell the instruction pointer to jump somewhere else instead, or jump different places depending on a certain condition this makes reusable code and conditional logic possible. That’s why code runs! The instruction pointer just keeps chugging forward, executing machine code in the order in which it has been stored in memory. This is the fetch-execute cycle.Īfter executing an instruction, the pointer moves forward to immediately after the instruction in RAM so that it now points to the next instruction. After executing each instruction, the CPU moves the pointer and repeats. The CPU stores an instruction pointer which points to the location in RAM where it’s going to fetch the next instruction. The CPU always reads machine code directly from RAM, and code can’t be run if it isn’t loaded into RAM. That includes the program code itself as well as the code at the core of the operating system. RAM is your computer’s main memory bank, a large multi-purpose space which stores all the data used by programs running on your computer. The remaining bytes are 512 ( 0x200) in little-endian byte order.ĭefuse Security created a helpful tool for playing around with the translation between assembly and machine code. The first byte ( 05) is an opcode specifically representing adding the EAX register to a 32-bit number. For example, add eax, 512 translates to 05 00 02 00 00. Assembly is a helpful syntax for reading and writing machine code that’s easier for humans to read and write than raw bits it is always compiled to the binary that your CPU knows how to read.Īn aside: instructions aren’t always represented 1:1 in machine code as in the above example. ![]() What we call machine code is nothing but a series of these binary instructions in a row. The “instructions” that CPUs execute are just binary data: a byte or two to represent what instruction is being run (the opcode), followed by whatever data is needed to run the instruction. It was a 4-bit architecture instead of the 64-bit systems we use today, and it was far less complex than modern processors, but a lot of its simplicity does still remain. The first mass-produced CPU was the Intel 4004, designed in the late 60s by an Italian physicist and engineer named Federico Faggin. It starts chugging as soon as you start your computer, executing instruction after instruction after instruction. ![]() The central processing unit (CPU) of a computer is in charge of all computation. Let’s start with the basics of how your computer works at its very core. This simplicity is very beautiful and sometimes very, very cursed. It’s still hard for me not to psych myself out, expecting more complexity or abstraction than actually exists! If there’s one thing you should burn into your brain before continuing, it’s that everything that seems simple actually is that simple. The one thing that surprised me over and over again while writing this article was how simple computers are.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |