Printing To Screen: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (Why doesn't rollback affect only the last edit? :-/)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(25 intermediate revisions by 14 users not shown)
Line 12:
0x000b8004: 'L', colour_for_L
0x000b8006: 'l', colour_for_l
0x000b00080x000b8008: 'o', colour_for_o
</pre>
 
Line 19:
For instance, using <tt>0x00</tt> as attribute byte means black-on-black (you'll see nothing). <tt>0x07</tt> is lightgrey-on-black (DOS default), <tt>0x1F</tt> is white-on-blue (Win9x's blue-screen-of-death), <tt>0x2a</tt> is for green-monochrome nostalgics.
 
For colour video cards, you have 16kb32 KB of text video memory to use. Since 80x25 mode does not use all 16kb32 KB (80 x 25 x 2, 4000= 4,000 bytes per screen), you have 8 display pages to use.
 
When you print to any other page than 0, it will ''not'' appear on screen until that page is ''enabled'' or ''copied'' into the page 0 memory space.
 
====Color Table====
 
{| {{wikitable}}
|-
! Color number
! Color name
! RGB value
! Hex value
|-
| 0
| Black
| 0 0 0
| 00 00 00
|-
| 1
| Blue
| 0 0 170
| 00 00 AA
|-
| 2
| Green
| 0 170 0
| 00 AA 00
|-
| 3
| Cyan
| 0 170 170
| 00 AA AA
|-
| 4
| Red
| 170 0 0
| AA 00 00
|-
| 5
| Purple
| 170 0 170
| AA 00 AA
|-
| 6
| Brown
| 170 85 0
| AA 55 00
|-
| 7
| Gray
| 170 170 170
| AA AA AA
|-
| 8
| Dark Gray
| 85 85 85
| 55 55 55
|-
| 9
| Light Blue
| 85 85 255
| 55 55 FF
|-
| 10
| Light Green
| 85 255 85
| 55 FF 55
|-
| 11
| Light Cyan
| 85 255 255
| 55 FF FF
|-
| 12
| Light Red
| 255 85 85
| FF 55 55
|-
| 13
| Light Purple
| 255 85 255
| FF 55 FF
|-
| 14
| Yellow
| 255 255 85
| FF FF 55
|-
| 15
| White
| 255 255 255
| FF FF FF
|-
|}
 
==Printing Strings==
Line 27 ⟶ 118:
If you have a pointer to video memory and want to write a string, here is how you might do it;
 
<sourcesyntaxhighlight lang="c">
// note this example will always write to the top
// line of the screen
Line 39 ⟶ 130:
}
}
</syntaxhighlight>
</source>
 
This simply cycles through each character in the string, and copies it to the appropriate place in video memory.
 
For a more advanced print function, you need to store variables for x and y, as the display controller will not print a newline. This involves a switch statement or similar construct.
You also have to test for x>80 or y>25 and in the case of x>80 setting x to 0 and incrementing y, or in the case of y>25 scrolling.
 
==Printing Integers==
Line 47 ⟶ 141:
Just like in any environment, you repeatedly divide the value by the base, the remainder of the division giving you the least significant digit of the value.
 
For example, since 1234 = 4 + 3* 10 + 2 * 100 + 1* 1000, if you repeatedly divide "1234" by ten and use the resultremainder of the division, you get the digits:
 
<pre>
Line 60 ⟶ 154:
Here is an example implementation of the itoa() function (which is not standard, but provided by many libraries):
 
<sourcesyntaxhighlight lang="c">
char * itoa( int value, char * str, int base )
{
Line 98 ⟶ 192:
return rc;
}
</syntaxhighlight>
</source>
[http://www.strudel.org.uk/itoa/ And here is a shorter one]
 
(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.
 
The good news are that <tt>stdarg.h</tt> is part of a freestanding implementation, so you can #include it even from your kernel source files. Under [[GCC]], the following simple implementation uses the gcc's built-in functionality to do all the work for you:
 
<source lang="c">
#define va_start(v,l) __builtin_va_start(v,l)
#define va_arg(v,l) __builtin_va_arg(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_copy(d,s) __builtin_va_copy(d,s)
typedef __builtin_va_list va_list;
</source>
 
An implementation of <tt>stdarg.h</tt> and <tt>printf()</tt> is available from geezer/osd ([http://my.execpc.com/~geezer/osd/code/inc/stdarg.h stdarg.h], [http://my.execpc.com/~geezer/osd/code/inc/_printf.h _printf.h], [http://my.execpc.com/~geezer/osd/code/tinylib/stdio/doprintf.c doprintf.c]).
 
Solar's [http://pdclib.rootdirectory.de Public Domain C Library] has those implementations as well.
 
== Troubleshooting ==
Line 124 ⟶ 199:
=== Nothing is Displayed ===
 
Keep in mind that this way of writing to video memory will _only_''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 do that initialization for you, but some other (mainly on laptops) do not. Check out [[Ralf Brown's Interrupt List]] for details. Note also that some modes that are reported as "both text & graphic" by mode lists are actually graphic modes with BIOS functions that plot fonts when you call char/message output through Int10h (which means you'll end up with plain graphic mode once in [[Protected Mode]]).
 
([[GRUB]] does this setup for you.)
Line 149 ⟶ 224:
=== Missing Strings ===
 
Sometimes printing individual characters works, but printing strings fails. This is usually due to the <tt>.rodata</tt> section missing in the linker script. The GCC option <tt>-fwritable-strings</tt> is a substitute workaround, but the real solution is to add <tt>.rodata</tt> to the script.
 
Previously, GCC had an option <tt>-fwritable-strings</tt> which could be used as a workaround for this, but it was deprecated in version 3.0 and removed in 4.0 and later, which was released in 2005. Even when the option was available, it was a kludge; the real solution was, and still is, to add <tt>.rodata</tt> to the script.
 
==See Also==
*[[Printing to the screen without a db]]
 
[[Category:Tutorials]]
[[Category:Video]]
[[Category:Text UI]]