Jiahao Cai
Jiahao Cai

Reputation: 1250

What is the function of 0x0700 in cga_putc?

I am studying for Operating System Engineering recently, and I found a "magic number" that I can't figure out in the kern/console.c when I tried to read the source code.

Basically, static void cga_putc(int c) in kern/console.c is a function for output the character to the console, it is used by cprintf in this kernel.

static void cga_putc(int c)
{
    // if no attribute given, then use black on white
    if (!(c & ~0xFF))
        c |= 0x0700;

    switch (c & 0xff) {
    case '\b':
        ...
    case '\n':
        ...
    default:
        crt_buf[crt_pos++] = c;     /* write the character */
        break;
    }
    ...
}

However, I don't understand the function of if (!(c & ~0xFF)) c |= 0x0700;, can anybody help me with this? I don't know which material I should look at.

Upvotes: 2

Views: 740

Answers (1)

Clarus
Clarus

Reputation: 2338

Read it through.

~0xFF: invert 0xFF, i.e 0xFFFFFF00 if using a 32 bit number

c & 0xffffff00: look at the high order bits

if (!c): if no high order bits, set the high order bits to 0x0700;

The purpose of this is pretty much explained in the preceding comment:

// if no attribute given, then use black on white

Highly likely that the high order bits are controlling the FG and BG color of the text.

Upvotes: 4

Related Questions