Printing To Screen: Difference between revisions
Jump to navigation
Jump to search
General rework.
[unchecked revision] | [unchecked revision] |
(→Printf: The explanations were not enough to base a stdarg.h implementation upon them, factually not quite correct, and as such probably more confusing than helpful.) |
(General rework.) |
||
Line 1:
==Basics==
This is quite easy.
Text mode memory takes two bytes for every "character" on
▲Text mode memory takes two bytes for every "character" on the screen. One is the ''ASCII code'' byte and the other the ''attribute'' byte. so <tt>HeLlo</tt> is stored as
<pre>
0x000b8000: 'H',
0x000b8002: 'e',
0x000b8004: 'L',
0x000b8006: 'l',
0x000b0008: 'o',
</pre>
The ''attribute'' byte carries the ''foreground colour'' in its lowest 4 bits and the ''background color'' in its highest 3 bits. The interpretation of bit #7
For instance, using <tt>0x00</tt> as attribute byte means black-on-black (you'll see nothing). <tt>0x07</tt> is lightgrey-on-black (
For colour video cards, you have 16kb of text video memory to use
When you print to any other page than 0, it will ''not'' appear on screen until that page is ''enabled'' or
==Printing Strings==
Line 27 ⟶ 28:
<pre>
/
void write_string( int colour, const char *string )
{
{
}
▲ *video=colour;
}
</pre>
Line 47 ⟶ 45:
==Printing Integers==
Just like in any environment
<pre>
Line 57 ⟶ 56:
</pre>
As this algorithm retrieves the digits in the "wrong" order (last-to-first),
Here is an example implementation of the itoa() function (which is not standard, but provided by many libraries):
Line 103 ⟶ 102:
(see more on [http://www.osdev.org/phpBB2/viewtopic.php?t=10319 the forum].)
== printf and variable argument lists ==
If you're working with C, you may want to print any number of arguments, like <tt>printf()</tt> does. For this, you need to handle variable argument lists. Looking at the <tt>stdarg.h</tt> file from other operating systems (e.g. Linux 0.1), you might be a bit confused by the macro definitions in that file, as they are basically black magic depending on the C calling conventions. As such, they are not exactly portable.
Line 125 ⟶ 124:
=== Nothing is Displayed ===
Keep in mind that this way of writing to video memory will _only_ work if the screen has been correctly set up for 80x25 video mode (which is mode 03). You can do this either by initializing every VGA register manually, or by calling the ''Set Video Mode'' service of the BIOS Int10h while you're still in real mode (in your bootsector, for instance). Most BIOS's
([GRUB] does this setup for you.)
Another common mistake is to, by following numerous tutorials spread across the net for example, link the .text section of your kernel/OS to the wrong memory address. If you don't have memory management in place yet, make sure you're using physical memory locations in the linker script.▼
▲Another common mistake
===Printing a Character===
While in Protected Mode, try a simple command like:
<pre>
// C
*((int*)0xb8000)=0x07690748;
// NASM
which should display 'Hi' in grey-on-black on top of your screen. If the previous step worked and not this one, check your paging / segmentation setup correctly maps your assumed video memory address onto 0xB8000 (or 0xB0000). NASM-only developers may use▼
mov [0xb8000], 0x07690748
// GAS
movl $0x07690748,0xb8000
</pre>
▲which should display 'Hi' in grey-on-black on top of your screen. If
=== Missing Strings ===
[[Category:Video]]
|