Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [org 0x8000]
- [bits 16]
- jmp short start
- gdt_start:
- dq 0x0000000000000000
- dq 0x00CF9A000000FFFF
- dq 0x00CF92000000FFFF
- dq 0x00CF92000000FFFF
- gdt_end:
- gdt_descriptor:
- dw gdt_end - gdt_start - 1
- dd gdt_start
- print:
- push si
- push ax
- push bx
- .loop:
- lodsb
- or al, al
- jz .done
- mov ah, 0x0E
- mov bh, 0
- int 0x10
- jmp .loop
- .done:
- pop bx
- pop ax
- pop si
- ret
- start:
- cli
- xor ax, ax ; Set ES=DS=0
- mov es, ax
- mov ds, ax
- mov ss, ax ; Set SS:SP (stackpointer) to 0x0000:0x7c00
- ; Will grow down beneath bootloader
- mov sp, 0x7c00
- mov si, start_sixteen
- call print
- in al, 0x64
- .wait_ibe:
- test al, 0x02
- jnz .wait_ibe
- mov al, 0xD1
- out 0x64, al
- .wait_ibe2:
- in al, 0x64
- test al, 0x02
- jnz .wait_ibe2
- mov al, 0xDF
- out 0x60, al
- first_sixteen:
- cli
- lgdt [gdt_descriptor]
- mov eax, cr0
- or eax, 1
- mov cr0, eax
- jmp dword 0x08:.afterstart
- [bits 32]
- .afterstart:
- mov ax, 0x10
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- mov ss, ax
- mov esp, 0x7c00 ; Set protected mode stack to 0x7c00
- ; Initialize the entire BSS area to zero
- mov ecx, _bss_end-_bss_start ; Length of region in ECX
- xor eax, eax ; AL=0 (also used for rep stos)
- mov edi, _bss_start ; Offset of BSS region
- rep stosb ; Set ECX bytes of data at ES:[EDI] to AL(0)
- ; Remember parameters are passed right to left. Push in opposite order
- ; Setup exception 0 (div by zero)
- push 0x8E ; Attribute for 32-bit interrupt gate (enabled)
- push 0x08 ; Kernel code selector (from GDT)
- push isr_divby0 ; Handler for exception
- push 0x00 ; div by 0 (arithmetic error) is exception 0x00
- call isg
- add esp, 4*4 ; Remove the 4 parameters
- ; Setup exception 6 (invalid opcode)
- push 0x8E ; Attribute for 32-bit interrupt gate (enabled)
- push 0x08 ; Kernel code selector (from GDT)
- push isr_invopcode ; Handler for invalid opcode
- push 0x06 ; Invalid code is exception 0x06
- call isg
- add esp, 4*4 ; Remove the 4 parameters
- call imake ; Set IDTR (via lidt)
- ; Cause invalid opcode exception
- ; ud2
- ; Cause division by 0 exception
- xor ax, ax
- div al
- cli
- .hltloop:
- hlt
- jmp .hltloop ; Infinite loop
- isr_divby0:
- ; Replace with a proper interrupt/exception handler
- hlt ; Inifinite loop
- iret
- isr_invopcode:
- ; Replace with a proper interrupt/exception handler
- hlt ; Inifinite loop
- iret
- i686IDTLoad:
- mov eax, [esp + 4]
- lidt [eax]
- ret
- ; void isg (uint32_t entrynum, void *handler, uint16_t selector, uint8_t attribs)
- ; Setup interrupt gate entrynum in the IDT with given handler function, selector, and attributes
- ;
- ; Parameters pushed on stack
- ;
- isg:
- push ebp
- mov ebp, esp
- sub esp, 8
- mov edx, dword [ebp+10H]
- mov eax, dword [ebp+14H]
- mov word [ebp-4H], dx
- mov byte [ebp-8H], al
- mov eax, dword [ebp+0CH]
- mov edx, eax
- mov eax, dword [ebp+8H]
- mov word [IDT+eax*8], dx
- mov eax, dword [ebp+8H]
- movzx edx, word [ebp-4H]
- mov word [IDT+2H+eax*8], dx
- mov eax, dword [ebp+8H]
- mov byte [IDT+4H+eax*8], 0
- mov eax, dword [ebp+8H]
- movzx edx, byte [ebp-8H]
- mov byte [IDT+5H+eax*8], dl
- mov eax, dword [ebp+0CH]
- shr eax, 16
- mov edx, eax
- mov eax, dword [ebp+8H]
- mov word [IDT+6H+eax*8], dx
- nop
- leave
- ret
- ; void ieg (uint32_t entrynum)
- ; Enable interrupt gate for entrynum in the IDT
- ;
- ; Parameters pushed on stack
- ;
- ieg:
- push ebp
- mov ebp, esp
- mov eax, dword [ebp+8H]
- movzx eax, byte [IDT+5H+eax*8]
- or eax, 0FFFFFF80H
- mov edx, eax
- mov eax, dword [ebp+8H]
- mov byte [IDT+5H+eax*8], dl
- nop
- pop ebp
- ret
- ; void idg (uint32_t entrynum)
- ; Disable interrupt gate for entrynum in the IDT
- ;
- ; Parameters pushed on stack
- ;
- idg:
- push ebp
- mov ebp, esp
- mov eax, dword [ebp+8H]
- movzx eax, byte [IDT+5H+eax*8]
- and eax, 7FH
- mov edx, eax
- mov eax, dword [ebp+8H]
- mov byte [IDT+5H+eax*8], dl
- nop
- pop ebp
- ret
- imake:
- push IDTDescriptor ; Argument 1 is address of IDT record
- call i686IDTLoad
- pop eax ; Remove arguments
- ret
- IDTDescriptor:
- dw IDT_SIZE - 1
- dd IDT
- start_prog:
- mov al, 2
- mov ax, 1004
- mov [Xval], eax
- mov ax, 700
- mov [Yval], eax
- mov ax, 10
- mov [Wval], eax
- mov ax, 20
- mov [Hval], eax
- lea si, [PySpr]
- call draw_spr
- .halt:
- cli
- hlt
- draw_spr:
- xor edx, edx
- mov ecx, [Hval]
- draw_sprite:
- mov eax, [Yval]
- mov ebx, 1024
- mul ebx
- add eax, [Xval]
- add edi, eax
- push ecx
- mov ecx, [Wval]
- draw_pixels:
- mov al, [si]
- cmp al, 5
- je skip_pixel_2
- cld
- movsb
- jmp skip_pixel
- skip_pixel_2:
- inc edi
- skip_pixel:
- inc si
- loop draw_pixels
- pop ecx
- mov eax, [Yval]
- inc eax
- mov [Yval], eax
- loop draw_sprite
- ret
- start_sixteen: db 'KERNAL START', 0x0D, 0x0A, 0
- Xval dd 0
- Yval dd 0
- Wval dd 0
- Hval dw 0
- PySpr db 5,5,5,2,2,2,2,5,5,5
- db 5,5,2,2,2,2,2,2,5,5
- db 5,2,2,2,2,2,2,2,2,5
- db 2,2,2,2,2,2,2,2,2,2
- db 5,8,8,8,8,8,8,8,8,5
- db 5,8,15,0,8,8,0,15,8,5
- db 5,8,8,8,8,8,8,8,8,5
- db 5,8,8,0,0,0,0,8,8,5
- db 5,5,8,8,8,8,8,8,5,5
- db 2,2,2,2,2,2,2,2,2,2
- db 2,2,2,2,2,2,2,2,2,2
- db 2,5,2,2,2,2,2,2,5,2
- db 2,5,2,2,2,2,2,2,5,2
- db 2,5,2,2,2,2,2,2,5,2
- db 8,5,2,2,2,2,2,2,5,8
- db 5,5,6,6,6,6,6,6,5,5
- db 5,5,6,6,5,5,6,6,5,5
- db 5,5,6,6,5,5,6,6,5,5
- db 5,5,6,6,5,5,6,6,5,5
- db 5,7,7,7,5,5,7,7,7,5
- section .bss
- _bss_start:
- IDT resq 256 ; Create an IDT with 256 entries
- IDT_SIZE equ $-IDT
- _bss_end:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement