1 ; Everything between real mode and the C kernel
7 mov es, bx ; set extra segment to starting address of video RAM
8 mov byte [es:0], 'L' ; print an L to screen, to let us know that we actually got here
10 cli ; disable interrupts
11 xor ax, ax ; clear accumulator
12 mov ds, ax ; clear data segment, this makes sure the next instruction reads from the right place
13 lgdt [gdt_desc] ; load the Global Descriptor Table
15 mov eax, cr0 ; move Control Register 0 to accumulator
16 or eax, 1 ; flip the bit which controls memory protection
17 mov cr0, eax ; move the accumulator back to CR0
18 jmp CODE_SEG:protected ; not quite there yet, need to do a far jump to clean the pipeline from any 16-bit instructions
19 ; note that the jump does not have to be physically far away, it just needs to use a segmented address
21 bits 32 ; we are finally in 32-bit mode
23 mov ax, DATA_SEG ; put the selector for the data segment in the accumulator
24 ; in real mode, segmentation works by taking one of these segment registers, shifting it right by 4 bits or 1 hex digit
25 ; and then adding a 16-bit offset to form a 20-bit address
26 ; example: 1234h:5678h
27 ; 1234h is shifted to 12340h, 12340h + 5678h is 179B8h
28 ; in 32-bit protected mode, these segment registers do not hold the segment address itself, but a selector in the GDT
29 ; so we have to update the segment registers accordingly
36 ; reinitialize the stack at a safe location
40 ; transfer control to the kernel
43 ; above call should not return in normal circumstances, but if it does hang forever
47 pusha ; save registers to stack
48 mov edx, video_memory ; initialize dx with location of VRAM
51 mov al, [ebx] ; read next char and put it in al
52 mov ah, 00000111b ; puts the VGA text mode color white on black into ah
54 cmp al, 0 ; if the next character is null, we reached end of string
55 je .done ; so return the instruction
57 mov [edx], al ; otherwise put the next character in the video memory
58 mov [edx+1], ah ; do the same for its color
60 inc ebx ; point to next character in string
61 add edx, 2 ; point to next character in VRAM
63 jmp .loop ; go back to the loop
66 popa ; restore registers from stack
69 video_memory equ 0B8000h
71 ; the actual Global Descriptor Table
72 ; refer to Volume 3, Chapter 2, 2.1.1 of Intel's 64 and IA-32 Software Developer's Manual for more info
92 dw gdt_end - gdt_start - 1
94 CODE_SEG equ code_seg - gdt_start
95 DATA_SEG equ data_seg - gdt_start