(Lower level of C++) When using "cout" on a piece of data, were does it go to before being displayed on screen?

236 views Asked by At

Specifically talking about the C++ part of the code here: [LINK]

(intel x86, .cpp & .asm hybrid program.)

From dealing with chars/strings' pointers in .asm I know it uses dl/dx registers for their storage-before-display (in case of 2h and 9h functions).

How is it in the case when the data (specifically, a floating-point value) gets sent to C++ portion of the hybrid, and then is treated with cout?

Where is that value stored before the cout converts it into a string to be displayed? (Is it a register, or a stack, or something else?)

1

There are 1 answers

0
Thomas Matthews On BEST ANSWER

The lower level stuff of C++ is platform dependent. For example, reading a character from the keyboard. Some platforms don't have keyboards. Some platforms send messages when a character arrives, others wait (poll the input port).

Let's talk one level down from the high level language.

For cin, the underlying level reads characters from the input buffer. If the buffer is empty, the underlying layer reads characters from the standard input and stores them into a buffer until an end-of-line character is detected.

Note: there are methods to bypass this layer, still using C++.

In many OS based platforms, the C++ libraries eventually call an OS function to fetch a single character. In Linux, the OS delegates this request to a driver. The driver has the responsibility of reading the character from the hardware and returning it. The driver is the piece of code that gets the character from the keyboard.

There are exceptions to this path, for example piping. With piping, the OS redirects the requests from standard input to a file or device, depending on command line.

Where is that value stored before the cout converts it into a string to be displayed? (Is it a register, or a stack, or something else?)

The compiler calls a function that converts the internal representation of a floating point variable into a textual representation. This textual representation is sent to the underlying cout function, character by character; or as a pointer to a string. The textual representation can reside almost anywhere: stack, heap, cache, etc. It really doesn't make a difference. Most processor registers are too small to contain all the characters in a textual representation of a floating point number.

The floating point value may be stored in a register, on the stack, or other places before passed to the conversion function. Depends on the optimization level of the compiler and the API for the conversion function. The compiler will try to use the most efficient storage types.