Skip to content

Instantly share code, notes, and snippets.

@NightBrownie
Created March 5, 2013 07:49
Show Gist options
  • Save NightBrownie/5088663 to your computer and use it in GitHub Desktop.
Save NightBrownie/5088663 to your computer and use it in GitHub Desktop.
[BITS 16]
[ORG 0x7c00]
;setting to protected mode
;---------------------------------------------------------------
;save the registers data
pushf
pusha
push ds
push es
;set ds and es to zero
xor ax,ax
mov ds,ax
mov es,ax
;loading of GDT
lgdt [GDTP]
cli
;setting to protected mode
mov eax,CR0
or eax,01h
mov CR0,eax
;jumps to protected mode code segment
jmp 08h:protected
[BITS 32]
protected:
;setting the data segments for a protected mode
mov ax,010h
mov ds,ax
mov es,ax
;---------------------------------------------------------------
;output string in protected mode
mov bx,0B8000h
mov es,bx
mov di, protected_string
mov cx, [protected_str_len] ;loop counter
xor bx, bx
mov bx, 3040
out_protected_mod_loop:
mov ah, byte [di]
mov byte [es:bx], ah
mov byte [es:bx+1], 4fh
add bx, 2
inc di
loop out_protected_mod_loop
mov ax,010h
mov ds,ax
mov es,ax
;setting to real mode
;---------------------------------------------------------------
;jumps to real mode code segment
jmp 018h:real_1
[BITS 16]
real_1:
;setting data segments for a real mode
mov ax,020h
mov ds,ax
mov es,ax
;setting to real mode
mov eax,CR0
xor eax,01h
mov CR0,eax
;setting real mode code segment
jmp 0000h:real_2
real_2:
sti
;load registers from the stack
pop es
pop ds
popa
popf
;---------------------------------------------------------------
;output string in real mode
mov bx,0B800h
mov es,bx
mov di, real_string
mov cx, [real_str_len] ;loop counter
xor bx, bx
mov bx, 3200
out_real_mod_loop:
mov ah, byte [di]
mov byte [es:bx], ah
mov byte [es:bx+1], 4fh
add bx, 2
inc di
loop out_real_mod_loop
;Global descriptors table
;---------------------------------------------------------------
GDT:
;empty descriptor
dq 0
;GDT (protected mode code segment)
dw 0FFFFh
dw 00000h
db 00h
db 010011010b
db 011001111b
db 00h
;LDT (protected mode data segment)
dw 0FFFFh
dw 00000h
db 00h
db 010010010b
db 011001111b
db 00h
;GDT (real mode code segment)
dw 0FFFFh
dw 00000h
db 00h
db 010011010b
db 010001111b
db 00h
;LDT (real mode data segment)
dw 0FFFFh
dw 00000h
db 00h
db 010010010b
db 010001111b
db 00h
;GDT pointer
GDTP:
dw GDTP-GDT-1
dd GDT
;-------------------------------------------------------------
real_string db 'Hi from real mode!'
real_str_len dw $-real_string
protected_string db 'Hi from protected mode!'
protected_str_len dw $-protected_string
;filling free space to 510 bytes
times 510-($-$$) db 0 ;2 bytes less now
db 0x55
db 0xAA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment