So I was thinking about languages the other day, and it struck me that any program written in a compiled language that interacts with the Internet is then translated into assembly that has to interact with the Internet. I’ve just begun learning a bit of x86 assembly to help me understand C++ a bit better, and I’m baffled by how something so low-level could do something like access the Internet.
I’m sure the full answer to this question is much more than would fit in a SO answer, but could somebody give me maybe a basic summary?
User-space programs that “interact with the internet”, in all modern systems, do so by issuing system calls to the underlying operating system, which supplies the API for a TCP/IP stack.
The system calls in question (such as
socket,listen,accept, and so forth) are typically documented at a C level, but in each particular OS implementation they will translate to machine code, of course. But whether values go in particular registers, or locations in memory pointed to by particular registers, etc, is pretty minor and totally system-specific.If you’re wondering how the machine code (probably also compiled from C) in the kernel and device drivers “interacts with the internet” (in response to system calls), it does so both by building and maintaining in-memory data structures to track the state of various things, and by interacting with the underlying hardware (e.g. via interrupts, I/O ports, memory mapped device areas, or whatever that particular architecture uses) — just like it interacts with (say) a video display, or a disk device.