Step Two: Add a screen output function to the kernel

Source: Internet
Author: User
Tags pack readable

This step, mainly a transition, prepares for future work.

The main work here is to tidy up the code with an output function that adds a string to the int type value.

In yc09, the compiled binaries, functions appear to be arranged in the order of the code, and then all variables are placed at the end.

Throughout the kernel, the kernel.c file is the core. Add any modules later, including the header file at the beginning of the kernel.c file, and include the code file at the end of the kernel.c file. This ensures that the kernel's entry function (the main function) is always at the front.

All header files contain macros, data structure definitions, global variables, and declarations of functions.

and the corresponding code file, is the specific function body.

Here is the newly added or modified code, files that have not been changed will no longer be posted.

Code:kernel.h (New)

Files: kernel.h//Features: Kernel header files, placing the required macros, data structure definitions, global variables, and declaration of functions//Author: Miao//Time: 2010-5-14/* Macro definition Area */#define PROTECADDR 0X7F00 Program base//GDT after entering protected mode Select Sub #define SELECTORCODE32 8*1//point to code snippet at 32 bit segment, executable readable #define SELECTORVIDEO 8*2//point to Memory head address #define Selecto RData32 8*3//points to the 32-bit segment so that variables in the program can read/write to/* data structure and global variable definition area *///GDT bounds, note that this is different from the GDT in Boot.c, the new GDT will be loaded immediately after jumping from boot.c. Descriptor label_gdt[] = {//segment base segment bounds attribute descriptor (0, 0, 0), descriptor (PROTECADDR, 0XFFFFF, DA_CR | DA_32),//32-bit code snippet, executable readable descriptor (0xb8000, 0xFFFF, DA_DRW),//memory address segment, readable writable descriptor (protecaddr, 0XFFFFF, DA_DRW | DA_32),//Make 32-bit code segment variable can read and write}; #pragma pack (1) struct GDT_PTR {t_16 size; void *addr;} Gdtptr = {sizeof (LABEL_GDT), (char*) &label_gdt + protecaddr}; Segment bounds, base address #pragma pack () int kernel_main ();

CODE:KERNEL.C (change)

File: kernel.c//function: Kernel program, currently function for testing several output functions in PRINT.C//run: Run.exe automatically compiles boot.c and generates an IMG and calls Bochs to run the program. Author: Miao//Time: 2010-5-14 #define YCBIT 32//Tell the compiler to compile the program in 32-bit format #define ycorg 0x0//This value will generate address base offset for variable functions such as at compile time, simple, set to 0 # Include "Global.h" #include "kernel.h" #include "print.h"//Kernel entry point ASM void Main () {LGDT cs:gdtptr//Load new GDTR mov eax, Sel Ectorvideo mov gs, AX//Video Segment Selection sub (purpose) mov eax, SelectorData32//Make 32-bit code segment variable (printplace) can read and write mov ds, ax jmp kernel_main} int k Ernel_main () {int i;//test string output function Disp_str ("This is the default color string./n"); for (i=0;i<=0x7f;i++) {disp_color_str ("C ", i);} DISP_STR ("/n"); for (i=0x80;i<=0xff;i++) {disp_color_str ("C", I);} DISP_STR ("/n"); for (i=0;i<0x2f;i++) disp_int (i); DISP_STR ("/n"); for (i=0xffff;i>0xffe0;i--) disp_int (i); while (1); return 0; } #include "print.c"

Code:print.h (New)

Files: Print.h//Features: Print header files, placing required macros, data structure definitions, global variables, and declaration of functions//Author: Miao//Time: 2010-5-14/* Data structure and global variable definition area */int disp_pos;// Record the coordinates that have been output to the screen (the "cursor")/* declaration area of the function */void Disp_str (char * info), or use the blue-bottom red character to output a string asm void Disp_color_str (char * info, int colo R);//Use the specified color to output a string void disp_int (int input);//output a number in hexadecimal format

CODE:PRINT.C (New)

File: print.c//function: Output characters to the screen related functions//run: Run.exe automatically compiles boot.c and generates an IMG and calls Bochs to run the program. Author: Miao//Time: 2010-5-14/* screen color and character display (two byte size) high byte low byte 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 blinking R G B highlight R g B ASCII background color character color character */void Disp_str (char * info) {disp_color_str (info,0x14);//default Blue bottom red Word} asm void Disp_color_str (char * info, int color) {PU SH EBP mov ebp, esp mov esi, [EBP + 8]//info mov edi, disp_pos//Take the last input after the "cursor" position mov ah, [ebp + 0xc]//color DPC_1:LODSB Read one character from the source address that ESI points to in Al Test Al, Al//al is empty JZ dpc_2//Is empty (no string to output), End Function cmp al, 0Ah//is the carriage return? JNZ dpc_3//Not line break, skip to dpc_3 direct output character push EAX//Save character color information in AH mov eax, EDI//Find out the next line starting position coordinates mov bl, wrap div bl and EAX, 0FFh Inc E AX mov bl, mul BL mov edi, eax pop eax jmp dpc_1 Dpc_3:mov Gs:[edi], ax//Put AX information into the video memory display out add EDI, 2 jmp dpc_1 dpc_2: mov disp_pos, EDI//save "cursor" position pop EBP ret} char *itoa (char *str, int num)//number in front of 0 is not displayed, for example 0000b800 is displayed as 0xb800 {Char *p = str; Char ch; int i; T_bool flag = false;//flag whether the 0 output, from the high, if the current bit is not 0, marked true, followed by 0, will output *p++ = '0 '; *p++ = ' x '; if (num = = 0)//is zero, output *p++ = ' 0 ' in 0x0 form; else {for (i=28;i>=0;i-=4)//convert from hexadecimal high to character form {ch = (num >> i) & 0xF; if (flag | | (Ch > 0)) {flag = TRUE; ch + = ' 0 '; if (Ch > ' 9 ')//for a,b,c,d,e,f ch + = 7; *p++ = ch;} }} *p = 0; return str; }//Converts a number to a hexadecimal string to display void disp_int (int num) {char output[16]; itoa (output, num); disp_str (output);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.