Skip to content

Instantly share code, notes, and snippets.

@nelsoncole
Last active December 24, 2017 18:12
Show Gist options
  • Save nelsoncole/6f20b0a9c5483cedd8f0d1c5878c57ed to your computer and use it in GitHub Desktop.
Save nelsoncole/6f20b0a9c5483cedd8f0d1c5878c57ed to your computer and use it in GitHub Desktop.
bootstrap test "pequenas alterações no código"
; Puxa Fred! Seu código é esquisito de mais.
; modificaficação no cs:ip para o endereço físico 0x7c00
; A pilha, também deve ser redefinida após a mudança de fluxo para 32-bit.
; cs:eip para mudança de fluxo ( 8:_protmode)
bits 16
org 0x7c00
jmp 0:_start
hello_world:
db `hello, world!\n\0`
align 4,db 0
_gdt_desc:
dw _gdt_end - _gdt - 1
_gdt_desc_addr:
dd 0
align 4,db 0
_idt_desc:
dw _idt_end - _idt - 1
_idt_desc_addr:
dd 0
align 4,db 0
_gdt:
dq 0 ; NULL selector.
dq 0x00_cf9a_000000_ffff ; code32,r/x,flat (sel=0x08)
dq 0x00_cf92_000000_ffff ; data32,r/w,flat (sel=0x10)
_gdt_end:
align 4,db 0
_idt:
dq 0
_idt_end:
align 2,db 0
_start:
cli
cld
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
mov esp,_stkend
; Guarda em EBX o endereço base "físico" deste segmento (será 0x7c00).
movzx ebx,ax
shl ebx,4
; Monta GDT_DESC
lea edx,[ebx+_gdt]
mov [_gdt_desc_addr],edx
; Monta a IDT_DESC
lea edx,[ebx+_idt]
mov [_idt_desc_addr],edx
call enable_a20
; Carrega
lidt [_idt_desc]
lgdt [_gdt_desc]
; precisamos mascarar interrupções e desabilitar NMIs se for preciso
cli
in al,0x70
or al,0x80
out 0x70,al
; Habilita o modo protegido.
mov eax,cr0
or eax,1
mov cr0,eax
; Salta para o código em 32 bits usando um trampolim.
push word 8 ; Ou seria CS? Está bem, aqui CS deve apontar para o vetor code selector no GDT (1*8bytes)
push word _protmode
jmp far [esp]
; Só o fast gate!
enable_a20:
in al,0x92
bt ax,1
jc .already_enabled
and al,0xfc ; Zerar o reset bit!
or al,2
out 0x92,al
.already_enabled:
ret
; Código de 32 bits.
bits 32
align 4
_protmode:
mov ax,0x10 ; DataSeg selector
mov ds,ax
mov es,ax
mov ss,ax
mov esp,_stkend ; A pilha deve ser redefinida após a mudança de fluxo.
; OBS: Todo ponteiro terá que ser carregado assim!
lea esp,[ebx+_stkend]
; Estamos no modo protegido! Sem TSS e sem interrupções ainda.
call clear_screen
lea esi,[ebx+hello_world]
mov edi,0xb8000 ; À partir do início da tela!
call show_string
.halt:
hlt
jmp .halt
clear_screen:
mov edi,0xb8000
mov ax,`\x07 `
mov ecx,2000
rep stosw
ret
; EDI = Posição na tela.
; ESI = String Ptr
show_string:
lodsb
test al,al
jz .end
mov ah,7
stosw
jmp show_string
.end:
ret
align 4
_stack:
times 32 dd `stk\0`
_stkend:
times 510 - ($ - $$) db 0
db 0x55,0xaa
@nelsoncole
Copy link
Author

"Alteração no cs:ip inicial para (0x07c0:0)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment