Skirox wrote:When I'm saying "optimizing", I mean creating fewer variables and writing code that uses constant values (using values directly)...
...which is called "magic numbers" and generally frowned upon, because maintainability of the source goes to hell.
That's what constants are for.
...which will take less memory.
...again, how do you figure?
I took the liberty of optimizing your code further:
Code: Select all
// High byte is the text attribute (see TEXT_ATTRIBUTE below),
// low byte is the character ASCII code.
static unsigned short * const VIDEO_MEM = (unsigned short *) 0xb8000;
// FIXME: Extend this to a complete list of color codes.
static unsigned short const COLOR_white = 15;
static unsigned short const COLOR_black = 0;
// Use this macro with the COLOR_* constants above to get the
// desired text attribute high byte (high nibble is background, low
// nibble is foreground color). To get the value to write to the
// VIDEO_MEM array, binary-OR this macro with the character
// ASCII code.
#define TEXT_ATTRIBUTE( foreground, background ) ( ( ( ( background ) << 4 ) | ( ( foreground ) & 0x0f ) ) << 8 )
// FIXME: Bad design, as this puts the burden of keeping track
// of "current" cursor position on the caller.
void putch( char const c, unsigned char const pos_x, unsigned char const pos_y, unsigned char const foreground, unsigned char const background )
{
VIDEO_MEM[ pos_x * 80 + pos_y ] = c | TEXT_ATTRIBUTE( foreground, background );
}
void clear_screen()
{
for ( int i = 0; i < 80 * 25; ++i )
{
VIDEO_MEM[i] = ' ' | TEXT_ATTRIBUTE( COLOR_white, COLOR_black );
}
}
It yields
exactly the same object code as your clrscr() source does (tested with gcc -std=c99 -O3 | strip)...
Edit: Beaten twice, but I gotz codez.