I've always wondered this, and still haven't found the answer. Whenever we use "cout" or "printf" how exactly is that printed on the screen?. How does the text come out as it does...(probably quite a vague question here, ill work with whatever you give me.). So basically how are those functions made?..is it assembly?, if so where does that begin?. This brings on more questions like how on earth have they made openGl/directx functions..
break it down people break it down.:)
Here's one scenario, with abbreviations:
printf
or cout
put characters into a buffer in the user program's address space.printf
asks for the buffer to be emptied early. Either way, the I/O library calls the operating system, which copies the contents of the buffer to its own space.It is amazing that the bear dances at all.
So basically how are those functions made?..is it assembly?, if so where does that begin?. This brings on more questions like how on earth have they made openGl/directx functions.
Those functions can be assembly or C, it doesn't change much (and, anyway, you can do in C virtually anything you can do in assembly.) The magic ultimately happens at the interface of software and hardware -- how you get there from printf
and cout <<
can be as trivial as a few pointer operations (see the 286 example below, or read about cprintf
further down), or as complex as going through multiple layers of diverse system calls, possibly even going over networks, before eventually hitting your display hardware.
Imagine the following scenarios:
I dig up my old 286 from under the dust and fire up MS-DOS; I compile and run the following program in real mode:
void main(void) { far long* pTextBuf = (far long*)0xb8000L; /* Poor man's gotoxy+cprintf imitation -- display "C:" (0x43,0x3a) in silver-on-black letters in the top-left corner of the screen */ *pTextBuf = 0x073a0743L; }
I am connecting with my laptop's Windows HyperTerminal to my serial port, which is hooked up with a cable to the back of a SUN box, through which I can access my SUN box's console. From that console I ssh into another box on the network, where I run my program which does printf
, piping its output through more
. The printf
information has traveled through a pipe through more
, then through an SSH pseudo-tty through the network to my SUN box, from there through the serial cable onto my laptop, through Windows' GDI text drawing functions before finally appearing on my screen.
Adding more detail to Norman's answer, hopefully more in the direction of your original question:
printf
and cout <<
usually perform writes to stdout
-- typically buffered writes, but that has not always been the case cprintf
, which wrote directly to video memory without making any system calls, memcpy
-style (see my 286 example above) -- more on that further downstdout
is a system call, be it write
under *nix, WriteFile
or WriteConsole
under Windows, INT 21, 9 under DOS, etc.stdout
abstraction is that it allows the operating system to do some internal plumbing and perform redirection (be it to a tty descriptor, to a pipe, to a file, to a serial port, to another machine via a socket etc.) stdout
s coexist on the same screen, e.g. in different windows -- something that would be much harder to do if each application tried to write directly to video memory on its own (like cprintf
did on DOS -- not what would be called today a true or usable multi-tasking operating system.)rxvt
console window application, PuTTY telnet/ssh client, Windows console, etc. will: stdout
: rxvt
or of the Windows consolecprintf
implementation or the OS) writes to the much smaller 80x25 or 80x50 etc. text buffer array, where (e.g. in the case of VGA) only two bytes are necessary to encode each character value such as A
or ▒
or ♣
(1 byte) as well as its color attributes (1 byte) -- that is, its foreground (4 bits, or 3 bits + brightness bit) and background colors (4 bits, or 3 bits + blink bit)Start at the History of Video Cards and GPU pages on Wikipedia for a more in-depth look at how we got where we are today.
Also look at How GPUs Work and How Graphic Cards Work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With