r/computerscience • u/TimeAct2360 • Oct 18 '24
how exactly does a CPU "run" code
1st year electronics eng. student here. i know almost nothing about CS but i find hardware and computer architecture to be a fascinating subject. my question is (regarding both the hardware and the more "abstract" logic parts) ¿how exactly does a CPU "run" code?
I know that inside the CPU there is an ALU (which performs logic and arithmetic), registers (which store temporary data while the ALU works) and a control unit which allows the user to control what the CPU does.
Now from what I know, the CPU is the "brain" of the computer, it is the one that "thinks" and "does things" while the rest of the hardware are just input/output devices.
my question (now more appropiately phrased) is: if the ALU does only arithmetic and Boolean algebra ¿how exactly is it capable of doing everything it does?
say , for example, that i want to delete a file, so i go to it, double click and delete. ¿how can the ALU give the order to delete that file if all it does is "math and logic"?
deleting a file is a very specific and relatively complex task, you have to search for the addres where the file and its info is located and empty it and show it in some way so the user knows it's deleted (that would be, send some output).
TL;DR: How can a device that only does, very roughly speaking, "math and logic" receive, decode and perform an instruction which is clearly more complicated than "math and logic"?
70
u/noerfnoen Oct 18 '24
https://nandgame.com/
15
u/MasterGeekMX Bachelors in CS Oct 18 '24
THe thing is that there is a huge way between CPU running code and being able to delete a file.
First of all, the CPU contains decoding logic that translates insctructions being fed up into enabling and disabling the different parts of the CPU. For example if you want to add 1 to a variable, you need to do one instruction to load the contents of some memory address into some register, then connect that register into one input of the ALU, and in the other input of the ALU put a one (usually done by turning on the carry in wire of the ALU adding part).
Depending on computer architecture, the CPU can access the rest of the system in some way. In older computers like the Commodore 64 other devices were simply available in the system bus connected to the input/output bus, meaning that reading from some range of memory could mean reading RAM or reading the contents of a cartridge, etc. In more modern computers the CPU has a companion chip (the chipset) that handles some of that connection, while the CPU does some of it by itself.
Well, an OS is the one responsible for making sense of all of that. The OS has code that talks to the rest of the system, and knows what signals to give to some device in order to perform some operation. As you say you have a bit of background in electronics, it is like giving signals to an I2C or SPI device to pull out data from a sensor or driving a small OLED display: you don't directly manipulate it, but instead use a series of signals that the other device responds to it in some way.
In order to delete a file, you need a way to talk to a storage media and be able to send and retrieve data to it. Then from there you make some filesystem so you can store data inside in some orderly manner instead of simply tossing data inside. Then you can make a function that presents that data in the form of files, and then you can make a command to "delete" some file, which means running instructions on the CPU that gives signals to the storage media to overwrite the correct memory cells where that "file" was stored.
If you want to understand more, here are some resources on youtube you can use:
Basically anything put up by the Core Dumped channel. He talks about how OSes work on the basic level with simple yet thorough examples. The channel is recent so he has few videos, meaning you can watch them all: https://www.youtube.com/@CoreDumpped/videos
Ben Eater has put up a series where he builds an 8-bit CPU in breadboards, explaning each part: https://www.youtube.com/playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU
In contrast Ben Eater also has a series where he takes the MOS 65c02 CPU, which is a simple 8-bit CPU, and makes a simple computer akin to the Apple I on breadboards: https://www.youtube.com/watch?v=LnzuMJLZRdU&list=PLowKtXNTBypFbtuVMUVXNR0z1mu7dp7eH&pp=iAQB
40
u/ninjadude93 Oct 18 '24
Everything gets compiled down to 1s and 0s by the time it hits a cpu. Lots of levels of abstraction on abstraction and standardization.
18
u/urva Oct 18 '24
Excellent question. And I think it’s a question that many people struggle to understand fully. They can even go their whole career in tech being an excellent engineer and not understanding it.
When ever you think something here is magic, just remember. THERE IS NO MAGIC. It’s just very small and there’s lots of stuff.
Imagine a ball, by itself it does nothing. But push it and it rolls. Now we’re on to something. Make a wooden box that fits one ball. Roll a ball towards it, it’ll go in the box. Roll another ball towards it, and it will not go in the box, but instead bounce out and roll away. The state of the system changes the behavior of the system.
Now imagine you had a gazillion of these boxes. They’re designed so that they can hold a ball, but if they’re full and another ball hits them fast enough, then both balls bounce out of the box. You could probably do some complex behavior.
Place the balls in really well thought out places, and roll 10 million balls in at very precise speeds, and you can probably draw stuff. Just imagine you zoom out and a full box is a black dot and an empty box is white dot. You can draw Shakespeare.
You might think this is crazy. Those boxes would have to be so perfectly well designed and placed and the balls would have to be rolled just right. And you’re right. It’s crazy. But it’s possible and it’s not magic.
Computers do the same thing. But with a gazillion 1s and 0s. Not magic. Just tons of them. And you’re seeing the super zoomed out view already.
I see you give the computer human qualities like “think” in your question. Remember, there’s no magic. Take any piece of the computer and it basically does one thing. “This part has state a. But if I send in electricity x then the state changes to y and the output is z”.
To answer your direct question I need to make it clear again. There is no magic. In this case ywhen you click to delete a file, you’re not actually deleting a file. There’s no such thing as “delete” or “file”. Your click sends electricity to a gazillion tiny parts of the computer that each change state and then give output xyz to the hard drive and the monitor. To you that seems like deleting a file. It’s convenient for humans to think like this. But for the computer, there’s no such a thing. It’s basically a really small machine.
4
u/fuzzynyanko Oct 18 '24 edited Oct 18 '24
This gets intricate and outside typical computer science. I think it's fine to talk about because there might be the curious. This is boiled down since it's been a while since microprocessors class, plus it goes into operating systems and so forth. Also, this is generalized and I'll probably mess up details
Deleting a file often actually does not delete it, especially with modern hard drives. It's treated more like an abandoned house. The house is still there, and you can loot it if you know how to.
The main CPU itself doesn't actually delete the file. It often asks the drive controller to do it. Many people working on lower levels usually only work on 1-3 layers. Many software developers only call to delete a file.
The CPU being the Central Processing Unit is an overall good name. It's the traffic cop that glues things together. Inside a PC, there's other processors and microcontrollers. Some said processors are also CPUs, but let's call them sub-CPUs. A graphics card is almost a computer on its own. The CPU coordinates all of the different pieces of hardware and tries to give the hardware instructions. Let's leave multi-core out of the discussion for now.
The Commodore 64 disk drive actually had a 6502 CPU in it!
For example, in many video games, what's happening? Again, this is generalized. The CPU sets up the game level. It forms it and then coordinates with the graphics chip to display it on a screen. It then (at least in the past) poll the input devices to see if you need to change anything in the environment. The CPU then moves different items on the screen around, maybe the world. The graphics chip, the game controller's circuit, maybe the keyboard, the disk drive, accessing RAM. That's all coordinated on the CPU
* Mostly the point is that there's often layers, which is very common in computing today. In older systems like DOS and the Commodore 64, you can actually skip several layers since the OS didn't shield the user from doing anything. Sometimes things in the past were fixed like memory addresses being allocated to certain hardware (ex: the Video Chip), so you could just access them sometimes with just a pointer (memory address)
4
u/Poddster Oct 18 '24 edited Oct 23 '26
Well, the good news here is that if you wait long enough in your course they should probably cover this. But it depends on the department I guess.
Anyway, the way I see it is that your fundamental problem here is one of mixing abstractions. You're mixing a very low level of abstraction (an ALU) with a very high one (use a GUI to delete a file).
To put this confusion in EE terms, imagine this question:
I think for a 1st year EE your instinct is that this is mixing the levels of abstraction, right? Well it's the same here.
Here a single wire can carry a single voltage for a single time period. But when grouped together and looked at over multiple time periods, we can stage to see a pattern, which we can interpret as a clocked binary pulse. And from there we can start to group those clocked binary digits into bytes, and from those bytes we can see a protocol happening. That protocol is how we send and receive USB commands. And then from there we can learn that there's one USB stack sending a command to another USB stack, and that on the computer end there is a driver sending commands down that USB stack, and on the HD end a disk controller that receives those commands. The disk controller deletes the hard drive blocks it's told to delete, and the hard drive driver is the thing that tells it which blocks to delete. It know what blocks to delete because it stores information about them in the file system meta data, and the file system meta data is presented to the user in a graphic user interface.
So we have wildly different levels of abstraction here. Whilst it's often very instructive to try and think about going from one level to another, it can often lead to confusion. A good example for an EE student would be learning about circuit theory, perhaps even the hydraulic equivalent, and then trying to think about it in terms of individual QE quarks and leptons. One of them (circuit theory) is a mass-phenomena that looks at millions of electrons at a time, the other looks at one.
At what point in that description of a USB hard drive command did it go from being "electrons" to being "files"? From "math and logic" to "user driven actions"? At what point in designing and building a bridge does it go from "math and logic" to "cars driving on it"? The mathematics that helped design that bridge are always there, but one day they're on paper and the next they're "in" the bridge somehow? :)
So it's not possible to answer your question directly, because it's mixing abstractions. The ALU is too low-level to even know what this is. Instead the programmer that programmed the software that is executing on the CPU "knows" about files and how they're stored, and so programs the software in such a way that:
So at what step did the ALU "do something"? The answer is in every step. In every single one of those steps the ALU did millions of things.
So the joining piece of information for you is if we can write software to control ALL of this stuff, and the CPU simply executes that software. You already know about the control logic, so I assume you know about the fetch-execute cycle. Each individual instruction is fetched from memory, interpreted by the control logic, and therefore executed. The control logic does this for one instruction after the other. And it's the programmer's job to put those instructions in an order that goes about deleting files and things.
If you want to know more about how humans build a digital, electronic computer, then I have a stock answer for that which roughly boils down to:
Petzold's book is the main draw. The other youtube videos are there for you to pass the time whist you wait for the book to arrive :) The Petzold book alone is worth its weight in gold for the general reader trying to understand computation. Most people can read that and will be completely satisfied when it comes to learning about computers. A second edition has recently been released after 20 years. The first edition is absolutely fine to read as well if you were to come across it. It's basically the same, but stops at 80% of the 2nd edition. Assuming you don't wish to buy it from those links above, it's easy to find via digital libraries on google :)
* Well, almost. As they're controlled by ICs you can change this on the fly as part of the protocol.
4
u/protienbudspromax Oct 18 '24
The CPU doesn't have any understanding that it is "deleting a file in your file system"
The ONLY thing the CPU understands are specific binary instructions which is called it's instruction set.
An instruction can generally have a operator code (what to do) along with some data, (what to do it with).
A CPU is not just an ALU, it has other features, functions. It is generally connected to some kind of bus/transport. It can do logical operations like OR, AND, NOR, NAND, XOR etc. It also has some storage in the form of registers.
The operator code i.e. the instruction that tells the cpu what operation to do, is generally part of microcode and it literally enables or disables parts of the cpu based on boolean logic/voltages.
It can interact with the RAM and read/write to it.
Everything else, is build on top of this as layers of abstraction.
From the perspective of the CPU it almost always does just one thing, regardless of what software is running on top,
fetch, decode and execute.
The cpu fetches the next instruction which is present at the address pointed to by its instruction pointer (a register)
It then decodes this (here is where code is broken down even further into microcode)
Finally it executes the needed instruction in the next couple cycles.
This is 99% of what a cpu does during anything except for when there is an interrupt.
But regardless of what you are running, an os, a simple single process, a game, whatever, this is ALL that a CPU does.
It doesn't have a notion of a "process" a "file system" an "os", everything is software.
And generally this is the level where someone studying electronics really care about, or atmost about architecture and computer org. And maybe somewhat goes into firmware dev.
But Everything that happens above it are generally in the domain of CS. People from CS generally takes this CPU and abstracts it out to do other things.
3
u/zshift Oct 18 '24
So files and other code are not represented just by what you see on a screen. Files all have IDs that are just a number, and the name or icon are words and pictures, both of which are interpretations of a series of numbers. When you say “delete file “foo.zip”, the computer also has the ID of that file behind the scenes. It takes that ID, then there’s a series of data points on your drive called a filesystem, the beginning of which (in most file systems), is a list of all the IDs, and where on the disk the data for that file is stored. So a number that references another number. Then (in an oversimplified explanation) the computer sets the ID and location to 0, indicating that there’s no longer a file there.
When your computer looks up the contents of that folder, any 0 entries represent space for another file.
This is a very simplified explanation, but that basically how everything in a computer works.
Words on a screen are represented by numbers, one set of numbers uniquely identities each letter. Then your computer looks up fonts, which are represented by bezier curves, which are more numbers. Then your computer asks the GPU to draw the letters using that font, and so it takes the number of each letter, gets back the corresponding numbers that represent the curves for that font, and the GPU draws those shapes defined by the curves. The drawing is done onto a bitmap (an array of 0s and 1s), which is sent to your monitor. The monitor takes those bits, and then interprets them as color. It does this by using the 1 values to turn on colors at each point on the screen via electronics, and a 0 means don’t turn that on.
TL;DR, it’s all numbers. Sometimes we do math on the numbers, other times we use 1/0 as on/off in electrical circuits. And the ALU is basically using a lot of on/off switches to do math, derived by Boolean algebra.
Most programmers will never work at such a low level, because we’ve defined abstractions over common actions. We have files of code written in (arguably) English, but need to be converted to commands written in numbers in order to run on your computer. We build more and more on top of that to make it easier to read and write complex code.
This is an excellent video from 2007 that explains this much better than I did, and honestly should be required viewing by all first-year students in comp sci. https://youtu.be/AfQxyVuLeCs?si=YBUAv2PSgkV2F45-
3
u/JmacTheGreat Oct 18 '24
I need to recommend to everyone in here the best video game for learning stuff like this:
Turing Complete
You make an entire computer, and run code on it. But you start off with building simple gates (like AND/OR gates), then that leads to like ALUs, then that leads to like memory, etc.
2
u/khedoros Oct 18 '24
Layers of abstraction. In CS, I had a series of courses that built up from boolean logic concepts, through logic gates, combinatorial and sequential logic, computer organization and architecture, OSes and their interface with software. Basically all the layers that go into answering that question.
Because you lost too many details with the "very roughly" part of the description. Different parts of the CPU store information, route data to and from different places depending on inputs, evaluate equalities and inequalities. The combination of those, plus arithmetic and logical operations, are enough to provide a CPU's capabilities.
2
u/tcpWalker Oct 18 '24
Nothing a classical computer does is more complicated than math and logic. They're all mappable to a simple machine called a turing machine, except that the tape they have to store data isn't quite infinite.
Basically transistors let you toggle the state of an output based on an input, you can connect those together in patterns to create logic gates and memory and math. You put a lot together so it can handle multiple inputs some in sequence some in parallel and create outputs. That's all a computer is doing.
It just happens to be doing that at enough scale that a server might have a million open file descriptors...
2
u/Noiprox Oct 18 '24 edited Oct 18 '24
For your specific example the missing link you're looking for is called the operating system. One part of an OS is the file system, which is a piece of code that turns a hard disk from a big block device (a thing that just copies fixed-size blocks of bytes to or from RAM at a specified address) into a hierarchical structure with files of varying sizes that have names and access control etc. The file system is the "math and logic" that implements that abstraction. Operating systems do a great many other vital things like direct the CPU to execute isolated processes, allocate and deallocate RAM, provide ways to communicate to devices via device drivers or over networks via the network stack, etc.
Deleting a file in particular usually involves reading bytes from a well-known address on the hard disk which contains a look-up table. The look up table tells you which addresses contain the bytes for all the various files. You use logic to find out whether the file in question is in the table and where it is if so. Then you zero out the part of the table which recorded where the file was on the disk drive, so that now the table does not contain that record anymore. These operations are all built out of simple logic and arithmetic, translated from C code into machine language that the ALU can process one opcode at a time.
2
u/voidvector Oct 18 '24 edited Oct 18 '24
The clock signal (oscillator,
RTC) allows the computer to change state independently as time moves forwards.This (along with CPU and memory) allows algorithms to be implemented that can be used to build more complex things.
2
u/sarc-tastic Oct 18 '24
Ben Eater!
2
u/clickrush Oct 18 '24
That's the issue!
Math and logic are fundamentally simple. You can describe their entire systems with just a few very simple primitives.
The complexity emerges from:
2
u/Hawk13424 Oct 18 '24
Going from deleting a file to ALU is millions of instructions. Just the idea of moving a mouse is hundreds of thousands of instructions.
Remember at the HW level, moving that mouse is optical pulses generated as you move it. And that file is magnetic fields recorded in circles on a metal spinning platter.
So the basic answer is millions of instructions built later upon layer to create more and more complex constructs.
4
u/evanlott Oct 18 '24 edited Oct 18 '24
Been almost 10 years since my architecture class, but IIRC:
Code is turned into assembly code and then into binary machine code and loaded into RAM as instructions. Program counter/pointer tracks the memory address of the next instruction to be executed, starting at the first one. CPU fetches, decodes, and executes an instruction. Instructions contain opcodes and parameters/operands (think of it as a function call). It also uses registers and L1 L2 L3 cache for faster performance than going to RAM. Advance the program counter and repeat until all instructions are executed
1
u/[deleted] Oct 18 '24
https://youtu.be/lNuPy-r1GuQ?si=_SqhM_a9YUSrlLc8
1
u/LifeHasLeft Oct 18 '24
Now I may misspeak a little bit because it’s been a while since I studied this particular topic, but I’ll do my best…
The electrical signals that are sent to the CPU by hardware devices, and the CPU is designed to interpret the signals as instructions based on the physical architecture etched into the silicon. That’s why you have different assembly languages for different CPU models.
The instructions it can handle are all relatively simple, as you noted. Arithmetic of course, but it will also store and retrieve segments of the binary signals in registers.
Some clever people used the handling of registers to perform more complex actions, like treating the registers as addresses to memory and pulling in cached data that corresponds to functions. Each individual action is super simple. Write a set of binary code to this memory address, move a set of bits along the memory to another address and do it again, etc.
Altogether the very simple actions are each happening lightning fast. Each CPU can have multiple processing cores which would perform these operations in parallel, and some machines have more than one CPU.
I recommend you take a course on assembly or similar topics if you are interested to learn more.
1
u/mihemihe Oct 18 '24
This is the best resource I always recommend to understand how computer works:
https://youtu.be/O5nskjZ_GoI
1
u/burncushlikewood Oct 18 '24
It's more of a hardware aspect of computing, a computer engineer would be more adept at answering this question than a computer scientist, my basic understanding of a CPU is a series of logic gates and theoretical computation that powers all of the features of the computer. It controls things like input and output, and allows compilation of code, the cpu also controls arithmetic, what allows your computer to run is it's ability to crunch numbers very fast
1
u/dontyougetsoupedyet Oct 18 '24
The CPU does more than math, it has a part called a control unit that controls the load, decode, and execute process. The CPU doesn't know about filesystems, those are sets of algorithms and data structures. The CPU just loads and changes and stores information and if that information happens to be things like trees then you can have features like deleting a file.
1
u/Paxtian Oct 18 '24
At the end of the day, everything is numbers. The hard disk includes a table that basically says, "The file named X is stored at memory address Y." So effectively to delete that file, you could take that entry for file X and set it to 0. Or have a "valid" bit in that entry that is set to 0 if it's to be deleted.
The exact implementation details matter less than the concept. There are numeric values that the CPU can set to do things like store/ delete files.
In networking, packets have layer upon layer of headers that are basically just numbers. Take a look at TLVs for various network protocols.
Computer graphics is all just numbers at the end of the day. Output values for R, G, and B for each pixel to the right address and that gets pushed to the monitor to set appropriate brightness values for each pixel.
Take a look at ASCII tables to see how numbers can be translated into text.
So yeah it's all just numbers. Certain numbers have special meanings.
1
u/amarao_san Oct 18 '24
It's foundational idea of the computer: you can describe any possible math operation as a number. As soon as you have them as numbers, you can work with them with math, so you work with math statements like with mathematical objects.
Now, computers. Which do the same, but, adding thing called 'side effects' and 'side causes', and that's all. A bit of Godel, a bit of Turing, plus some juicy side effects.
1
u/mikedensem Oct 18 '24
Your questions deal with many levels of abstraction – from bits to files on disks, so it can’t all be answered in one response. I think however, what you’re looking for is the link between purely Boolean logic and the seemingly magic array of stuff a computer can do?
Well that is simply the beauty of “abstraction” itself. Mr Boole was looking to reduce down all human language and actions into a simple set of Boolean operations – he came up with logic ‘gates’ as the key to be able to abstract away complex ideas into a cast array of simple “true and false” building blocks. He discovered that anything and everything in the world can be broken down into a constituent series of Boolean logic operations.
The computer at its heart (the CPU) is simply a massive scaling of Boolean logic gates that are cleverly designed to fit together in large carry-forward arrays building up the ability to do massive amounts of Boolean logic to produce these abstractions we value. Every CPU has a relatively small set of key commands to act on the logic gates (and as you pointed out with support from the registers/ram/etc) so that these commands in combination can be scaled up to create the magic. Software programs are complex language logic structures that are broken down (by a complier) to millions of CPU commands in such an efficient manner that when run at the huge clock speeds of modern CPU’s they can produce the illusions of complex actions and further abstractions.
The illusion here is that it seems more complicated than purely “math and logic”, but it is not. HDD makers use the same logic commands to track locations and lengths of files (contiguous bytes) on disk (a complex area in itself with indexes and tricky physics) but exposes only a small set of commands (through an API) for the CPU to ‘talk to’ (using an agreed protocol) so that both sides hide away their complexities behind abstractions. And those HDD’s (peripherals) are abstracted away again via a communications protocol to talk to the CPU over timed data busses (with buffers etc). Phew. At the end of the day this is all just a preposterous number of Boolean decisions being made through a very fast but well timed continuously repeating cycle of steps that never stops. Unless it Halts and caches fire!
1
u/CodeMUDkey Oct 18 '24
Well the answer to your last question is probably the simplest one. What you think isn’t a logic problem is in fact a logic problem. The storage/removal of data is a question of state. That state can physically be represented by a logic system.
In terms of how it physically does it this link does a nice job. This link is also really useful for the specific piece of transistor technology. Being able to manipulate the state of a transistor with voltage only is a real awesome thing.
1
u/Accomplished_Pay_385 Oct 18 '24
Binary bzzzt Logic Gates bzzzt
1
u/Menector Oct 18 '24
You tell the computer to delete a file. This (eventually) translates to the CPU running a CALL instruction to load instructions from the operating system's "delete file" code.
This code does very simple things in concept, although exact steps may vary depending on who programmed it. It may look up the file by comparing file names (treat letters as numbers and compare numbers). It may already have the physical address of the file as part of the CALL instruction. It proceeds to search through a common reference storage area checking for all references to the physical address and "deletes them" ( might be shifting data around, might just change a single bit used as a "is this still active" flag). The file is deleted.
As part of this, the data is still there. It's relatively expensive to "remove data" (replace with 00s), so usually the CPU just changes 1 bit for each address indicating it's no longer used and can be replaced at earliest convenience. But there's no more references to the data, and it will eventually be overwritten with new data.
The CPU is mostly doing comparison of basic numbers (addresses or file names) followed by simple addition (increase address pointing to items to check) and a series of MOV instructions to write at specific locations. Every single command, no matter how complex, can be broken down to a series of basic MOV, math, and comparison instructions effectively.
As others have said, we get there by abstraction, writing complex instructions (maybe in C) which get translated to assembly (human friendly machine code) then into specific instructions that the CPU recognizes. But in the end, everything from managing a video game to surfing Facebook comes down to the CPU executing (billions? trillions?) of very simple logic, basic math, and data transfer instructions.
1
u/greyfade Hundred-language polyglot Oct 18 '24
The ALU is not the only piece at work here. Yes, it only does arithmetic and logic, but it also communicates with the memory bus.
When an instruction is read off memory and decoded, it has within its encoding:
So, when the ALU gets a store instruction (which has two arguments, an input register and an output memory address), it puts the memory address it was given on the address bus, and then copies the contents of the register it was given to the data bus. Outside the ALU, then, the CPU's memory controller sends the data out onto the motherboard.
This is where it gets interesting. Memory isn't the only thing on the memory bus. Most modern computers use what's called MMIO - Memory-Mapped Input/Output. This basically means that at some memory addresses, instead of being connected to memory chips, the data bus instead is redirected to other parts of the system.
By "modern computers" here, I mean basically anything made after 1968.
The operating system defines the set of data structures that make up a filesystem and it understands how to communicate with the disk drive hardware controllers and the bus that connects it to the computer interconnect bus. Here's the general flow:
SYSCALL. This allows programs to command the operating system.SYSCALLfor theunlinksystem call. On x86_64 computers, that means the registerraxis loaded with the number that maps tounlink, andrsiis loaded with a pointer to a string containing the name of the file to delete.unlinksyscall.DISCARDcommand packet for the drive.) This data isSTOREd into memory.I've left out a lot of details and have probably also oversimplified to the point of being wrong, but you get the idea.
1
u/Logical_Hearing347 Oct 18 '24
It is a Turing machine. It can basically store numbers in memory, take it from memory to its registers and make some math with. You'll see how it works properly when you study Computer Organization.
1
u/joelangeway Oct 18 '24
One historically important Computer Science concept that connects abstract ideas like arithmetic and file systems to realizable physical machines is The Turing Machine. You have to play a sort of puzzle game to figure out how to get a Turing machine to do a thing, but you can hook up input output perception actuation whatever devices up to a Turing machine and if the thing you want it to do is actually computable, you can build a Turing Machine that can compute it.
It just might take longer than the length of time left in the universe’s very existence, so the hardware is in fact much much much more optimized than a Turing Machine, but there is no computer system that does a thing that a Turing Machine can’t do.
1
u/ChemistDependent1130 Oct 18 '24
Do nandgame
1
u/Fizzelen Oct 18 '24
First thing is to understand how a CPU works, the “best” way is to have a good look at how a CPU emulator works https://github.com/taniarascia/chip8#
Then boot loading, opening systems, hardware interfaces, file systems, human machine interfaces, OS console and command language, then GUI, then a file manager
1
u/FenderMoon Oct 18 '24 edited Oct 18 '24
This is a great question. Essentially, it's a LOT of instructions, and they do math and logic on data that's located within different memory addresses. Certain memory addresses (and certain instructions) correspond to manipulating data on data buses, sending data to the hard disk (controlled by the drivers) and other such things.
Even things like putting stuff on the display essentially involves manipulating data that's in memory. The hardware has certain predefined addresses for this stuff, and responds a certain way if the CPU interacts with it in a certain way. It's all broken down into simple instructions ultimately.
The operating system abstracts a lot of this away, so if that if you were writing an application that needed to save a file, you would just need to write instructions that call the operating system's API to do it. This would load certain code from the operating system to come in and handle it, which would manipulate the right memory addresses with the right data to interact with the hardware. As such, regular application developers don't really need to learn the super low-level details, that's for operating system developers to worry about.
1
u/wayofaway Oct 18 '24
Nand2tetris.org is pretty good for getting a handle on it.
1
u/One-Butterscotch4332 Oct 18 '24
Learn assembly, then learn how to convert assembly to machine code. Then it makes sense. Thank the lord for high level languages.
1
u/johndcochran Oct 18 '24
There's lots of good suggestions here, but I'd suggest you start with something more concrete and basic. And you could do far worse than to watch James Sharman's video series on building a 8 bit pipelined microprocessor using releatively simple jellybean TTL along with a several eeproms to store the microcode in.
URL is https://www.youtube.com/playlist?list=PLFhc0MFC8MiCDOh3cGFji3qQfXziB9yOw
1
u/macroxela Oct 18 '24
A lot of good answers here, just adding a good source to that. Core Dumped on YouTube has an entire series videos starting from how logic gates work, working his way up the layers of abstraction to how CPUs run code. He explains all of the steps in between in a digestable manner.
http://www.youtube.com/@CoreDumpped
1
u/Weekly_Victory1166 Oct 18 '24
Good question, as a software developer, I don't know. To understand at this level you might need to study electrical engineering. For me, a long time ago this book helped a bit:
https://www.worldradiohistory.com/BOOKSHELF-ARH/Technology/Radio-Shack/Radio-Shack-Understanding-Solid-State-Electronics-2nd-1972.pdf
1
u/UnkarsThug Oct 18 '24
I recommend the book Code: The Hidden Language of Computer Software and Hardware. Really good resource for understanding the extremely basic fundamentals of how the systems actually execute instructions.
In answer with the extreme basics, and bypassing optimizations that may be more recent, each basic section of machine code is put into the selection gate of the CPU in sequence, and that creates one output pin corresponding to the instruction used, which activates that circuit, with the additional details fed in. Instructions with the machine code usually include addresses for things at memory as well, so the system uses those.
1
u/flat5 Oct 18 '24
"From NAND to Tetris" on Coursera.
Truly outstanding course and you will understand very well at the end of it.
1
u/Tyler89558 Oct 18 '24
Someone did all the work for you, getting the computer to understand instruction from code (bunch of 1s and 0s) to do something (other 1s and 0s).
1
u/idylist_ Oct 18 '24
I would recommend looking into a MIPS processor and ISA if you want to internalize how CPUs work. Assuming you have an understanding of register transfer level operations (basically assembly).
The processor fetches an instruction from a queue, reads the appropriate registers, executes the instruction (does any required arithmetic with the ALU), accesses memory for stores or loads, then “writes back” the result of the instruction to a target register if required.
This covers memory access, arithmetic, conditionals, and jumps which are the building blocks of basically all other CPU functions.
As for how this is implemented, you can imagine a clock driven circuit that steps through the above stages in order. It is divided into a controller and a data path. The controller is the stateful circuit (finite state automata) and the data path is stateless. The data path has switches that decide whether to access a memory location, choose inputs or operation for the ALU, etc based on the current instruction. The controller will set these switches based on the current instruction. This is during the instruction decode phase typically. Then the controller directs the inputs to the data path and directs any outputs from the other end of it during execution.
1
u/TeeBitty Oct 18 '24
I highly recommend playing Turing Complete. It helped me tremendously when I took Computer Architecture and Assembly Lang and taught me a ton.
1
u/rwitz4 Oct 18 '24
You write code (probably in a run time), then the runtime executes a kernel which is in assembly which runs on the chip (the CPU), and the assembly moves the bytes (think binary around in registers. Look up difference between memory and register and a fun exercise 😝
1
u/Cat7o0 Oct 19 '24
so it really depends how deep you want to look at it.
simply you have gates like and, or, xor, not, and more. Using just those gates you can do everything a CPU can.
in a higher level it's instructions like the top comments says.
now on the lowest level where it's just the die I'm unsure how they actually form those gates. there is transistors but the exact formation of them that forms specific gates is not something I've looked at
1
u/TheForceWillFreeMe Oct 19 '24
This question is missing layers.
You think that your commands mean something. They dont.
Your commands first go from userspace to kernelspace and then from kernelspace to a seperate area for handling storage, then from that area to the Hard drive itself which then does its own computations to move the head and find what you are looking for.
You claim an ALU does this operation. It does not. The CPU has many different parts, like a FPU, ALU, and even integrated GPUs .
A good book to read more about this is OSTEP by Remzi and Andrea Arpaci Dessaeu (freeeeee)
1
u/Spiritual-Finding452 Oct 19 '24
Here's a simplified breakdown:
How ALU performs complex tasks
You're right; ALU only does arithmetic and Boolean algebra. However:
Deleting a file: a simplified example
Here's how the CPU might execute the "delete file" instruction:
DELETE FILE "example.txt"Decoding: Break down into micro-operations:
Microcode: Translate micro-operations into ALU-friendly instructions:
Key takeaways
Took me some time to write this. Hope this helps 👍
1
u/Red_I_Guess Oct 19 '24
It is converted into machine code which is basically power or no power and big circuits in the CPU use logic gates which is basically more circuits to different things depending on what inputs you get. E.g 0001 might be the route to store something and 00000001 could follow telling it to load in that slot and basically there's only one route it can take which ends up in it storing in the word specified
1
u/Enough_Cauliflower69 Oct 19 '24
I highly encourage you to go down the rabbit hole and investigate this bottom up. With bottom being logic gates and up being Assembly or the C language maybe. Highly rewarding imo. To answer your question: There is an Instructionset for example add, sub, shift, etc.. The individual instrctions are etched into silicon. Each command contains the code for the instrctuon to be performed and either the data directly or the address of the operands in memory. Then there are flags and other things it’s complicated, and awesome!
1
u/hibbelig Oct 19 '24
The CPU reads an instruction from memory, then does what the instruction says. Which instruction from memory? The Program Counter contains the address. Most instructions “say” a few things, among them bumping the Program Counter to the next address.
Of course, the instruction can say things like: add register R1 and R2 and put the result in register R3. That would exercise the ALU.
But it is also common to write things to memory. Some memory can be “real” RAM, but it can also be “fake” where there is actually another device listening on it.
Or the CPU has special I/O pins, and instruction that says to set the output pins to a specific pattern. There could be a device listening on that pin.
The device could be a hard disk, which then receives instructions such as reading certain blocks or writing certain blocks. Some blocks are actual file content, other blocks are the table of contents so to say (a directory listing). You delete a file by tweaking the table of contents.
1
u/c_glib Oct 19 '24
You should check out Little Man Computer. I have never seen anything that explains the basic functions of a CPU better than that little simulator.
https://peterhigginson.co.uk/lmc/
1
u/tyngst Oct 19 '24
It’s complicated, but in essence, the mouse outputs the coordinates, which are then mapped to the screen/canvas, which is maps the positions of the icons, which represent som file, which has a path, which is a string, which eventually maps to a a registry in memory, which holds the file.
It’s all actually shuffling of numbers (or bits), which, as you know, is done with cpu instructions. As many have already mentioned, going from a bit adder to a modern cpu (or the whole computer board) is enormous. To be able to understand the whole process in its entirety will probably take many, many years.
1
u/uberbewb Oct 20 '24
Check out branch education on youtube.
They provide great insight on how hardware works.
1
u/spgremlin Oct 20 '24
The CPU is NOT limited to “control unit” and “ALU”, it is an oversimplification.
The CPU also executes other important instructions including memory operations (read/write to RAM) and IO operations to command auxilliary units (ex: SSD storage controller) to store or retrieve data to permanent storage (typically it is done asynchronously and directly between storage and RAM, and then it reports that the operation is complete by an Interrupt). Same for networking controller, GPU, and other IO devices.
Also concurrency and multithreading control, context switching, locking mechanisms.
Overall, modern X86 CPUs may have thousands of internally supported instructions (OPs).
1
u/mcksis Oct 22 '24
Definitely take a computer architecture course or at least read up on the subject.
Semiconductors and other components are combined to form gates. Gates (and, or, nor, etc) get combined to form registers, counters, multivibrators, clocks, adders, etc. These then get combined into the ALUs and logical units in a CPU.
These technologies are studied in layers, with the disciplines of electronics, physics, Boolean algebra, synchronous and asynchronous circuits, register transfer languages, etc.
So there are many disciplines to understand it all; that’s part of what an electronics/computer science education will teach you (at least on the hardware side).
1
u/rebruisinginart Oct 22 '24
Read up on kernels and everything will start to fall into place.
1
u/Mental_Lawfulness_10 Nov 06 '24
https://youtu.be/Z5JC9Ve1sfI?si=M1dsgb9A7kfEcHuP
1
u/YesterdayRemarkable6 Mar 06 '25
deleting does not overwrite stored data. that could result in the possibility of an out of bounds memory write a.k.a. data clobbering a.k.a. very bad. It simply removes the memory page for that file, which serves as a table of contents for files.
1
u/Awkward_Specific_745 Oct 18 '24
I am just a beginner myself but i’ll try explaining from what I know. Not only do CPU’s do arithmetic and boolean algebra, but it can precisely move data from register to register. This is another important part as it directly allows the concept of loops through jumps and conditional jumps. Which in turn allows for variable storage and for subroutines. Which allows for if statements. So from what those give us that allows us ( and few other instructions) to make Assembly language as that is most of the traits of a high level language that you know of. And then after levels of abstraction you get to high level languages, which are Turing complete. I hope I explained it well!
1
u/murrayju Oct 18 '24
The simple answer is that deleting a file is very complicated, and takes many many CPU instructions to pull it off. Computers are all about breaking down tasks into very small steps, and then doing those steps very fast (billions per second! Let that sink in…).
A cpu knows how to do many simple operations, and those are composed together to do more complex things. There are numerous layers of abstraction that wrap up a set of complex things into a simple reusable package. Even the CPU instructions are an abstraction over the electrical properties of transistors and their organization into logic gates.
At a high enough level, you get to execute a simple
rmcommand, and that kicks off a mind-boggling chain of events in the blink of an eye.1
u/BitSorcerer Oct 18 '24
It’s magic, that’s how.
1
u/not_some_username Oct 18 '24
One instruction at a time (or x instructions with x = the number of thread )
0
u/Logical-Independent7 Oct 18 '24
I'm just a Junior CS major, and there are some excellent answers here so, instead I'd like to just say that IMO this was a great question and appropriately thought out.
0
u/wahnsinnwanscene Oct 18 '24
Don't forget memory regions are mapped for the cpu to address. Also there are abstractions so that the cpu can poke numbers into a memory region and tell another peripheral to send it to some place else. Other than that, the os organises the program start for the cpu to start sequentially running through the code.
0
u/LitheumClay Oct 19 '24
Go watch Ben Eater on YouTube
-2
u/Max_Oblivion23 Oct 18 '24
electricity can be on or off... right!? BOOL! Binary is born, 0 or 1. But what if you wanted to flip it around and say not0, not1? NAND! Digital logic system is born...
Now if you have 8 inputs that can be 0 or 1, you can add a bunch of logic gates that basically flip the whole things around like the is no tomorrow in all sort of madness inducing ways until the amount of possible outcomes becomes 64 per inputs... there you have a 64 bit architecture.
However, what if you patched in SOME parts of the DLS to execute in different ways... you have more possible outcomes... and you can have those outcomes go through each others logic through random access memory modules meant to process the outcomes. At that point outcomes are stored as integers < digits < arrays.
In our computers it's RAM, ROM, HDD, SDD a series or circuits that store different combinations of possible outcomes through exponentially more complex iterations.
When you delete a file the addresses simply returns to 00000000
0
u/[deleted] Oct 18 '24
[deleted] — view deleted comment
0
u/[deleted] Oct 18 '24 edited Oct 18 '24
[removed] — view removed comment