How does Software/Code actually communicate with Hardware?

My question is:

When I press the "Shut down" button in Windows/Linux,the computer shuts down. How did the command "Shut down" actually make the computer Physically shutdown?

To make my point clear:

When we kick a ball,there is physical contact between the ball and our leg,for the ball to move.So how is the physical connection achieved between software and hardware? How does plain text of codes make the computer do what it does?


Solution 1:

While the answer is fairly complex, and requires understanding the basics of electrical systems & circuits (KVL, KCL), binary numbers, and boolean logic, we can describe the process at a high level: code (represented by plain text words) is compiled, assembled and ultimately translated into a combination of Zeroes and Ones, which represent low and high voltages respectively.

When voltages are applied to materials & circuits, those voltages can change their physical properties, such as closing a circuit which supplies current to a light bulb (causing it to illuminate) or supplying current to a dc-motor which might open up a CD/DVD/Bluray/(?) Drive.

Now imagine a hypothetical 1-bit CPU, which has the ability to close a circuit and deliver current to a buzzer, making a sound. This 1-bit CPU has a single Input, which can have two values: 0 and 1.

This simple CPU has a very simple assembly language: ON and OFF, and we have a fancy programming language that provides some nicer abstractions: cpu.turnOn,cpu.turnOff.

I write my program cpu.turnOn; compile it, assemble it, and it's ready to be run. When I run the program on my CPU, the buzzer turns on.

In the real world, computers are made up of more complex systems. Instead of a single 1-bit CPU, we have 64-bit CPU, with complex instruction sets, and a myriad of devices.

To make things for a complex system to interact, these systems are made up of layers of abstractions.

The lowest layer is voltages, circuits and silicon, or what you might think of as the actual 'physical hardware.' Along side the hardware will typically sit a 'microcontroller' or a specialized processing unit that is designed to interact with the specifics of a hardware. Imagine an optical disk drive, it's microcontroller has the ability to eject the drive bay, start up the motor, align the laser, and stream data off disc.

The software that the microcontroller runs, is referred to as firmware. It's a specialized operating system that controls hardware functions and may also include an API. In the imaginary 1-bit CPU example, the program would be firmware, and the cpu.turnOn, cpu.turnOff would be the API.

Given a computer is made up of lots of hardware components (graphics, storage, communications, i/o), computers are made up of lots of specialized firmwares. For anything useful to get done with that hardware, another layer of abstraction is needed, for example to deal with Keyboards in a universal manner, or to allow mice, touchpads, and trackballs all to behave the same despite different interactions. This is where the Operating System comes in. The OS provides an API to manage groups of related devices, and provides hooks for hardware vendors to provide the translation between the OS commands, and the commands the microcontroller understand. In Windows land, this is a driver.

The next layer of abstraction above Operating Systems and Drivers, are applications, what users use to do real work (or play Fortnite). These programs are written in a myriad of languages, SDK, and toolkits, and is the reason why StackOverflow exists. Those languages compile down into executable code which the OS loads and manages, and executed by the computer.

Putting it all together with the shutdown command: the command interpreter uses an OS level API that manages system power. That API sends a notification to the rest of the OS to handle things like gracefully flushing memory buffers, saving application state, terminating communication channels, and powering off a variety of hardware systems (or more likely going into low-power consumption mode). It also uses a power driver (ACPI?) to interface with the computer's power management subsystem. This subsystem is instructed to shutdown, which in turn sends a signal to the computer's power supply to break the circuit and no longer supply power to the majority of components.

Solution 2:

If you think software is a different creature in comparison with hardware, no explanation would satisfy you. Think software like a sequence of electric charges. All the code that you write would get stored as sequence of electrical charges either on RAM or disk. So you are NOT writing text but the sequence of electric charges. Your video card is drawing the stuff on the monitor in English to help you to understand what you are typing. In one way, perhaps truly, whatever you do on the computer is physical.

Solution 3:

Software is stored in the hardware as magnetic domains on the hard drive or floppy disc, or as low and high voltages in computer chips. When you type on a keyboard, each character is converted into an electrical series of 0's and 1's which are then stored as low and high voltages in the computer chips called RAM. The low and high voltages in RAM are then converted into the magnetic domains on the hard drive or floppy disc for later reading back by the disc heads into voltages, or are stored as low and high voltages in non-volatile computer chips for later reading back. The low and high voltages represent the electrical 0's and 1's which were generated by the keyboard characters.

Solution 4:

Well, the shutdown isn't actually physical, all circuits in the computer aren't totally off until you physically unplug the power.

The software uses the APM (Advanced Power Management) interface in BIOS to control the power circuits in the computer.

When the computer is off, it can still be turned on without physically pulling a switch, for example by a Wake on LAN signal from a network card in the computer.

Solution 5:

The leg kicking a ball is a good example. It's quite similar in a machine. The CPU is connected to all the other parts of the system, but unlike the nervous system which is a physical wiring, with all nerves being connected at once, the CPU's does not maintain a permanent connection to the rest of the system. It connects to the desired part on demand - similar to making a telephone call - all telephones have connections, but only a few are connected at any one time.

The cpu does work by running instructions - the software program. There are instruction codes that instruct the cpu to dial some part of the system. Each part has a number, and the cpu has an instruction to dial a number. Once the cpu dials that number, it sends a message to that part - the message is simply data - from one bit up to any arbitrary size block. The hardware at that location then acts on the message encoded.

In doing it like this, the cpu can control any piece of hardware using the same mechanism. The only thing that changes for each device is the number the cpu has to dial and the data the cpu sends to the device - details that are put into the software the cpu is running.

So, to turn the machine off, the cpu dials the number for the power management device, and sends it instructions to go into an appropriate power state. The hardware responds, and the PSU stops sending primary power to the motherboard.

When you write softare, you don't have to know all these details yourself. They are usually pre-packaged as ready to use code, so your software just has to say "shutdown" and the ready-made codes for this (usually in the BIOS) are executed to perform the shutdown, as outlined above.