juls0730

bootloader.asm

Jun 19th, 2023
2,925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 1.89 KB | Source Code | 0 0
  1. [BITS 16]
  2. [org 0x7c00]
  3.  
  4. mov [BOOT_DEVICE], dl
  5.  
  6. ; setup stack
  7. mov bp, 0x9000
  8. mov sp, bp
  9.  
  10. ; Set video mode (640 x 480 @ 16 colors)
  11. mov ah, 0x00
  12. mov al, 0x12
  13. int 0x10
  14.  
  15. call load_kernel
  16. call switch_to_32bit
  17.  
  18. load_kernel:
  19.         mov bx, 0x1000        ; Destination address
  20.         mov dh, 2             ; Sector number
  21.         mov dl, [BOOT_DEVICE] ; Drive Number
  22.  
  23.         mov al, dh
  24.         mov ch, 0x00 ; Cylinder number
  25.         mov cl, 0x02 ; Start from sector 2, sector 1 is our bootloader
  26.         mov ah, 0x02 ; Set read mode
  27.         mov dh, 0x00
  28.  
  29.         int 0x13
  30.         ret
  31.  
  32. gdtp:
  33.         dw gdt_end - gdt_start - 1
  34.         dw gdt_start
  35.  
  36. gdt_start:
  37.         dq 0x0
  38. gdt_code_segment:
  39.         dw 0xFFFF         ; Limit (16-bit) (set to 0xFFFF for maximum)
  40.         dw 0x0            ; Base (16-bit)
  41.         db 0x0            ; Base (8-bit)
  42.         db 10011010b      ; Access flags
  43.         db 11001111b      ; Granularity flags (set to 0xCF for 4 KB granularity)
  44.         db 0x0            ; Segment bits
  45. gdt_data_segment:
  46.         dw 0xFFFF         ; Limit (16-bit) (set to 0xFFFF for maximum)
  47.         dw 0x0            ; Base (16-bit)
  48.         db 0x0            ; Base (8-bit)
  49.         db 10010010b      ; Access flags
  50.         db 11001111b      ; Granularity flags (set to 0xCF for 4 KB granularity)
  51.         db 0x0            ; Segment bits
  52. gdt_end:
  53.  
  54. GDT_CODE_SEG_ADDR equ gdt_code_segment - gdt_start
  55. GDT_DATA_SEG_ADDR equ gdt_data_segment - gdt_start
  56.  
  57. switch_to_32bit:
  58.         ; Disable interrupts and setup gdt
  59.         cli
  60.         lgdt [gdtp]
  61.  
  62.         ; Set PE (Protection Enable) bit in CR0
  63.         mov eax, cr0
  64.         or eax, 0x01
  65.         mov cr0, eax
  66.         jmp GDT_CODE_SEG_ADDR:init_32bit
  67.  
  68. [BITS 32]
  69. init_32bit:
  70.         mov ax, GDT_DATA_SEG_ADDR
  71.     mov ds, ax
  72.     mov es, ax
  73.     mov fs, ax
  74.     mov gs, ax
  75.     mov ss, ax
  76.  
  77.         mov ebp, 0x90000
  78.         mov esp, ebp
  79.  
  80.         call init_kernel
  81.  
  82. init_kernel:
  83.         call 0x1000
  84.         jmp $
  85.  
  86. BOOT_DEVICE db 0x00
  87.  
  88. times 510 - ($ - $$) db 0 ; make sure file is 510 bytes in size
  89. dw 0xAA55                 ; write boot signature
Advertisement
Add Comment
Please, Sign In to add comment