Download presentation
Presentation is loading. Please wait.
Published byClifford Patterson Modified over 9 years ago
1
4. Kernel and VGA ENGI 3655 Lab Sessions
2
Richard Khoury2 Textbook Readings None
3
Richard Khoury3 Kernel Last week, our stage-2 bootloader loaded a 32-bit kernel This week, we will create our first kernel! ◦ Displays a message ◦ Infinite loop
4
Richard Khoury4 Kernel org 0x100000 bits 32 jmp KernelCode %include "KernelDisplay.inc" msg db 0x0A, 0x0A, "ENGI3655 32-Bit Kernel", 0x0A, 0 KernelCode: mov ax, 0x10 mov ds, ax mov ss, ax mov es, ax mov esp, 90000h call ClearScreen mov ebx, msg call DisplayString jmp $ cli hlt
5
Richard Khoury5 Kernel We already have a simple display function in the bootloader ◦ Uses BIOS Interrupt 10h ◦ But that won’t work in Protected Mode We’ll have to use the VGA adaptor ◦ Functions “ClearScreen” and “DisplayString” (and more) are in the included file “KernelDisplay.inc”
6
Richard Khoury6 VGA Video Graphic Array (VGA) is the video standard since 1987 Consists of ◦ Graphics Controller: interface between video buffer and the rest of the system ◦ Video Buffer: a segment of memory ◦ Sequencer: converts memory data to colour indexes ◦ Video Digital-to-Analog Controller: converts memory data and sequencer indexes to analog signal ◦ CRTC: controls various hardware functions, such as screen resolution, converting DAC signal to video signal, and moving the cursor System Bus Graphics Controller Video Buffer Sequencer DAC CRTC
7
Richard Khoury7 Video Buffer Segment of memory Assigned by BIOS at start-up as 0xA0000 to 0xBFFFF ◦ 0xA0000 to 0xAFFFF: graphics mode ◦ 0xB0000 to 0xB7FFF: monochrome text mode ◦ 0xB8000 to 0xBFFFF: color text mode VGA system automatically displays the content of part of this buffer, based on current video mode
8
Richard Khoury8 Graphics Controller Supports different video modes For backward compatibility, it is in Mode 7 at start- up ◦ 16 colours ◦ ASCII text ◦ 80 columns by 25 lines ◦ 2 bytes per character First byte for character Second byte for attribute Bits 0-2: Foreground RGB Bit 3: Foreground intensity Bits 4-6: Background RGB Bit 7: Foreground blinking
9
Richard Khoury9 Displaying in Mode 7 Content of Video Buffer displayed on screen directly Therefore, we display simply by writing into the buffer at the right place What is the right place? ◦ Mode 7 is a colour text mode, so the right place starts at 0xB8000 %define VIDMEM 0xB8000 mov edi, VIDMEM ; pointer to video memory mov byte [edi], 'A' mov byte [edi+1], 0x94 0x94 = 1 001 0100 = blinking red on blue background
10
Richard Khoury10 Displaying in Mode 7 Each word in memory is a character position on screen ◦ 0xB8000 is top-left corner, coordinate (0,0) Displaying elsewhere on screen is simply writing elsewhere in memory Screen position is counted in characters from 0 ◦ 5th character of first line = 4 ◦ 5th character of second line = one line’s worth of characters + 4 ◦ More generally: (X,Y) position on screen = Y * screen width in characters + X
11
Richard Khoury11 Displaying in Mode 7 Memory position = 0xB8000 + screen position in memory But remember that each character is two bytes in memory ◦ So multiply by 2 (X,Y) position on screen = 0xB8000 + Y * 2 * screen width in characters + X * 2
12
Richard Khoury12 Displaying in Mode 7 Let’s add a blinking blue on red B at (4,5) ◦ Attributes: 1 100 0001 = 0xC1 %define COLS 80 xor ecx, ecx mov ecx, COLS*2; 2 * screen width xor eax, eax mov al, 5; Y mul ecx; Y * 2 * screen width push eax mov al, 4; X mov cl, 2 mul cl; X * 2 pop ecx add eax, ecx; Y * 2 * screen width + X * 2 add eax, VIDMEM; 0xB8000 + Y * 2 * sw + X * 2 mov edi, eax mov byte [edi], 'B' mov byte [edi+1], 0xC1
13
Richard Khoury13 Displaying in Mode 7 Now we can display a single character anywhere on screen What more do we need?
14
Richard Khoury14 What more do we need? We need to keep track of the current (X,Y) coordinates _CurX db 0 _CurY db 0 We need to watch for new lines ◦ Newline ASCII char cmp bl, 0x0A je.Row ◦ Reached column 80 inc byte [_CurX] cmp [_CurX], COLS je.Row We need to jump to the next line.Row: mov byte [_CurX], 0 inc byte [_CurY] We need to put all this in a character-display function
15
Richard Khoury15 What more do we need? String-handling ◦ Notice from our kernel mov ebx, msg call DisplayString ◦ And just last slide cmp bl, 0x0A Function DisplayString ◦ Split up a string, putting each character in bl, watching out for the 0 end char, and calling the character-display function for each one and the cursor-update function at the end ◦ Minor modification of the one we already have
16
Richard Khoury16 What more do we need? From the kernel, we also need call ClearScreen ◦ As you might guess, this function clears the screen and resets _CurX and _CurY to zero ◦ Clearing the screen is simply displaying 80 x 25 = 2000 consecutive spaces of the screen background colour
17
STOSW ◦ Copy the value of AX into memory at ES:DI in the specified order REP STOSW ◦ Repeat CX times CLD ◦ Specify order: copy left-to-right and increment ES:DI Clear Screen Richard Khoury17
18
Richard Khoury18 Cursor You might notice that the cursor doesn’t move as we add text The Cathode Ray Tube Controller (CRTC) handles the cursor The CRTC has several 8-bit ports, including ◦ Port 0x3D4: Index Register ◦ Port 0x3D5: Data Register The Index Register offset indicates what the data sent to the Data Register is ◦ 0xE: Cursor Location High ◦ 0xF: Cursor Location Low
19
Richard Khoury19 Cursor Moving the cursor to an address in EBX done in two steps, low byte then high byte For each ◦ Point to the right offset (0x0E or 0x0F) in the index register (0x03D4) ◦ Send the new value (BH or BL) to the data register (0x03D5) ◦ Recall: last week we saw a function to send a value from our Assembly program to a controller port OUT port#, value
20
Richard Khoury20 Cursor The new cursor position has to be in ebx The cursor position is a screen, not memory, position ◦ Use _CurX and _CurY without multiplying by 2 mov bh, byte [_CurY] mov bl, byte [_CurX] ◦ Compute ebx = bh * COLS + bl xor eax, eax mov ecx, COLS mov al, bh mul ecx add al, bl mov ebx, eax
21
Richard Khoury21 Lab Assignment New files online ◦ New kernel code: Kernel.asm & KernelDisplay.inc Write the functions in KernelDisplay.inc that handle the memory or controller ◦ ClearScreen ◦ DisplayChar ◦ MoveCursor ◦ DisplayString is already given Those are the first four OS functions we’ll make!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.