r/AskComputerScience • u/[deleted] • Aug 02 '19
How does a computer receive and then process data from an input, and output data?
Say if I have a keyboard connected via USB to my PC, a key press will send the binary version of the ASCII character to the computer, an application and the operating system will incorporate it into a program - but how does it get sent to main memory, as instructions can only be executed from there - if there was no program, would it simply be sent to main memory and then be executed?
And secondly, how would a CPU output something i.e. a monitor? Are there specific instructions in it's instruction set to output to a specific connection?
2
u/Lofter1 Aug 02 '19
The CPU doesn't care for the output or output-method. It just sends and receives data from/to a controller, for example, an USB controller. This controller then converts the data so that it can send it, and the received data so that the CPU understands it. And with graphics output, the CPU usually doesn't do much, this is done by the GPU on your graphics-card. Sometimes a CPU has a GPU build into it (ie Intel Integrated Graphics). It sends instruction to the GPU to process something, the GPU sends it, just like the CPU, to a controller for a specific connection to convert it into the right signal (HDMI, DisplayPorrt, VGA,...).
And no, if there is no program for the CPU to execute, it wouldn't even receive anything, as you never told it to receive data. And the program doesn't get the keystrokes, either, this is all done by your OS which will then send the key to the program that awaits input right now. The keystroke will not magically just go into memory, and it will not execute a program through it, as it executes binary CPU instructions, not characters or strings. For example, the Assembler Instruction ADD could be compiled into 0bx01000010. The CPU doesn't understand "ADD", 0bx01000010 the CPU understands and knows "I need to perform an addition now".
1
u/smm_h Aug 02 '19
how would a CPU output something i.e. a monitor?
This is done by specific library calls such as OpenGL by the OS, passed onto the appropriate accelerator e.g. GPU and written unto a graphics buffer. Displaying that buffer is also done by the OS and your device drivers.
I understand your general confusion. Most gigantic tasks in computer science, such as the entire infrastructure of the internet, how operating systems work, how a gaming console works, how a 3D computer aided design application works, etc., they all follow the layered structure.
Layers are pieces of software that are built on top of each other and cooperate with each other. Each layer offers services to its upper layers and uses services provided by layers below it.
This may seem unclear so I will use an analogy. You're using the layered structure everyday without knowing it. For example, when you order a pizza, you use a service provided by the pizza place. They on the other hand, use services provided by a butcher (meat) and a farmer (flour, tomatoes).
Now you, don't ever have to directly deal with the butcher and farmer. Not that you can't, you just don't have to. They themselves use services provided by others and you yourself provide some services to others. Maybe your friend asked you to order the pizza, that means they're using a service you're providing.
Same goes with computers. Apps use services of the OS, OS uses various low level libraries of itself or third parties, and they all use the different pieces of hardware you have.
1
u/claytonkb Aug 02 '19
Say if I have a keyboard connected via USB to my PC, a key press will send the binary version of the ASCII character to the computer, an application and the operating system will incorporate it into a program - but how does it get sent to main memory, as instructions can only be executed from there - if there was no program, would it simply be sent to main memory and then be executed?
You will benefit from reviewing computer architecture (this is the term you're looking for). The CPU, memory and I/O (the three main components of a computer system) are connected together by a common communication channel referred to as the system bus. These days, there are a lot more details, but that's the general overview. When the CPU wants to talk to an I/O device, it sends a message over the system bus to the device. There are special helper devices (called controller hubs) that help the CPU talk to I/O. The reason we have controller hubs is that the CPU core operates thousands or millions of times faster than I/O devices in the system, so the communication between them has to be buffered (a buffer is just a small scratchpad memory for storing a tiny amount of data until it is needed, usually in a communication channel).
And secondly, how would a CPU output something i.e. a monitor? Are there specific instructions in it's instruction set to output to a specific connection?
Both monitors and keyboards (and mice and USB and so on and so forth) are I/O devices and are treated more or less the same. The monitor is usually connected to a graphics device which sits on the system bus so the CPU (or system controller) usually doesn't directly speak to the display, it speaks to the graphics device which, in turn, speaks to the display.
1
u/khedoros Aug 03 '19
Say if I have a keyboard connected via USB to my PC, a key press will send the binary version of the ASCII character to the computer
The keyboard produces a scancode, along with some button state info, packages it in a USB data packet, and transmits it as a serial stream of bits over the cable. It enters the USB controller, which raises an interrupt* for the CPU to handle the data. It might be that the data is transferred by "Direct Memory Access" directly into some standardized/specified area of memory, or stored in some data buffer in the USB controller; honestly I don't remember the specifics there. The interrupt is handled by the OS kernel's drivers, which interpret the packet and forward it along in a way that may vary a bit between OSes.
Are there specific instructions in it's instruction set to output to a specific connection?
Depends on the device and the CPU. X86 definitely has port-based I/O, with instructions called "IN" and "OUT". I know that they were used in older machines to communicate with a lot of hardware, things like the keyboard controller and portions of the various sound adapters (like Adlib synthesizers were configured for port 0x338, and were completely controlled through that).
Two other methods are memory-mapped I/O, where specific memory locations are routed over the system bus to memory or registers in external hardware, and DMA (which I already mentioned), where a chunk of memory might be pushed directly into a known location in memory, or pulled from a known location in memory, to a device.
A chunk of audio might be transferred to your sound card via DMA. The state of your graphics hardware might be some mix of memory-mapping, port-based I/O, and DMA. That's some driver-level kind of stuff, that I don't have much exposure to, outside of the context of DOS (because we had to configure some of that stuff by hand, back in the day). So I'm not 100% on how a modern machine would do it, but it would be a combination of the methods I've described.
* Interrupts are typically meant to signal something like "a transfer has completed, and data is available to be processed". In a CPU, there's a register that tracks the address of the next instruction to fetch. An interrupt basically says "No matter what you're doing, push the address of the instruction after the currently executing one to the stack, switch into an interrupt-handling mode (maybe, depending on CPU type), and run the code that's registered as the interrupt handler".
1
u/uber1337h4xx0r Aug 03 '19
Just a heads up: I'm pretty sure keyboards don't actually receive the ASCII code from the keyboard. They most likely get a code between like 1 and 200 and then the software decides what that code means.
1
u/terriblestraitjacket Aug 03 '19
For security reasons, you actually don't want keyboards to send pure ASCII, a raw number, or the like.
Generally you want to send encrypted packets that can be verified as coming from the keyboard. This prevents an eavesdropper from firstly, knowing what you typed and secondly, sending whatever commands they wanted on the same channel.
The one we use is https://www.microsoft.com/accessories/en-au/aes-encryption
1
u/uber1337h4xx0r Aug 03 '19
Nice. I think I used one of these years ago, but it caused too many issues with either my games or entering passwords into web sign ins (I can't remember which. It was like 10 years ago).
But I do recall it messed up keyloggers somehow.
16
u/kedde1x Aug 02 '19
It happens via hardware interrupts. In short, when you press a key, an electronic alert signal is sent to the processor, called an interrupt, which then handles the keystroke.