Operating System-Operating System-Screen Printing in the Kernel (Part 2)

Operating System-Operating System-Screen Printing in the Kernel (Part 2)

Follow the previous implementation sequence to implement PrintString

int PrintString(const char* s)//需f要对参数进行判断,如果参数为空,返回-1
{
    int ret = 0;

    if( s != NULL )
    {
        while( *s )
        {
            ret += PrintChar(*s++);//不为空时进行循环,直到遇见结束符结束
        }
    }
    else
    {
        ret = -1;
    }

    return ret;
}

//在这里需要单独对NULL进行定义   kernel.h

#ifndef KERNEL_H
#define KERNEL_H

#define NULL ((void*)0)

#endif

The operation of clearing the screen is as follows

void ClearScreen()//二重循环对屏幕每一个位置打印空字符
{
    int h = 0;
    int w = 0;

    SetPrintPos(0, 0);

    for(h=0; h<SCREEN_HEIGHT; h++)
    {
        for(w=0; w<SCREEN_WIDTH; w++)
        {
            PrintChar(' ');
        }
    }

    SetPrintPos(0, 0);
}

Implementation of PrintIntHex()

int PrintIntHex(unsigned int n)
{
    char hex[11] = {'0', 'x', 0};//将整数进行转换
    int i = 0;

    for(i=9; i>=2; i--)
    {
        int p = n & 0xF;//位与判断

        if( p < 10 )
        {
            hex[i] = ('0' + p);//将0-9数字转换为字符
        }
        else
        {
            hex[i] = ('A' + p - 10);//当低四位大于10,A B C
        }

        n = n >> 4;//将N右移四位
    }

    return PrintString(hex);//16进制的字符串打印
}

I implemented the functional interface before, and then implement the remaining functional interface
int PrintIntDec(int c)-recursive deduction
code implementation-decimal

int PrintIntDec(int n)
{
    int ret = 0;

    if( n < 0 )
    {
        ret += PrintChar('-');//多打印一个字符

        n = -n;

        ret += PrintIntDec(n);
    }
    else
    {
        if( n < 10 )
        {
            ret += PrintChar('0' + n);
        }
        else
        {
            ret += PrintIntDec(n/10);
            ret += PrintIntDec(n%10);
        }
    } 

    return ret;
}

Operating System-Operating System-Screen Printing in the Kernel (Part 2)
Cursor tracking in protected mode-operation of two different designated ports The
Operating System-Operating System-Screen Printing in the Kernel (Part 2)
above figure shows the cursor setting of the upper eight bits and the lower eight bits. The code implementation has been implemented in the int SetPrintPos(short w, short h) function. The assembly code of setting the cursor to the upper 8 bits and lower 8 bits is embedded in the C language code

Experiments and results-in line with expected results

Operating System-Operating System-Screen Printing in the Kernel (Part 2)Operating System-Operating System-Screen Printing in the Kernel (Part 2)
The code related to the experiment is saved at this address and can be downloaded if needed https://down.51cto.com/13465106/up

summary

1. The GCC compiler only supports inline assembly in AT&T format
2. The PrintCha() function can be realized through assembly
3. PrintCha() is the basis of other screen printing functions
4. The cursor position is set by operating the 0x03D4 and 0x03D5 ports

Guess you like

Origin blog.51cto.com/13475106/2551651