Advertisement
TerrificTable55

Untitled

Oct 2nd, 2022
2,922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. section .boot
  2. bits 16         ; tell NASM this is 16 bit code
  3. global boot
  4.  
  5.  
  6. boot:
  7.     mov ax, 0x2401
  8.     int 0x15            ; enable A20 bit
  9.  
  10.     mov ax, 0x3
  11.     int 0x10            ; set vga text mode 3
  12.  
  13.     mov [disk], dl
  14.  
  15.     mov ah, 0x2         ; read sectors
  16.     mov al, 6           ; sectors to read
  17.     mov ch, 0           ; cylinders idx
  18.     mov dh, 0           ; head idx
  19.     mov cl, 2           ; sectors idx
  20.     mov dl, [disk]      ; disk idx
  21.     mov bx, copy_target ; target pointer
  22.     int 0x13            ; read sectors from drive to target location
  23.  
  24.     cli
  25.    
  26.     lgdt [gdt_pointer]  ; load gdt table
  27.     mov eax, cr0
  28.     or  eax, 0x1        ; set the protected mode bit on special CPU reg cr0
  29.     mov cr0, eax
  30.    
  31.     mov ax, DATA_SEG
  32.     mov ds, ax
  33.     mov es, ax
  34.     mov fs, ax
  35.     mov gs, ax
  36.     mov ss, ax
  37.  
  38.  
  39.     jmp CODE_SEG:boot2  ; long jump to the code segment
  40.  
  41.  
  42.  
  43. gdt_start:
  44.     dq 0x0
  45. gdt_code:
  46.     dw 0xffff
  47.     dw 0x0
  48.     db 0x0
  49.     db 10011010b
  50.     db 11001111b
  51.     db 0x0
  52. gdt_data:
  53.     dw 0xffff
  54.     dw 0x0
  55.     db 0x0
  56.     db 10010010b
  57.     db 11001111b
  58.     db 0x0
  59. gdt_end:
  60.  
  61. gdt_pointer:
  62.     dw gdt_end - gdt_start
  63.     dd gdt_start
  64.  
  65. disk:
  66.     db 0x0
  67.  
  68. CODE_SEG equ gdt_code - gdt_start
  69. DATA_SEG equ gdt_data - gdt_start
  70.  
  71.  
  72.  
  73. times 510 - ($-$$) db 0     ; pad remaining 510 bytes with zeroes
  74. dw 0xaa55                   ; magic bootloader magic - marks this 512 byte sector bootable!
  75.  
  76.  
  77. ; ========================
  78. ; beyond 512 bytes
  79. ; ========================
  80.  
  81.  
  82. copy_target:
  83. bits 32
  84.  
  85.  
  86. hello: db "Hello world!", 0
  87.  
  88.  
  89.  
  90. boot2:
  91.     mov esi, hello
  92.     mov ebx, 0xb8000
  93.  
  94. .loop:
  95.     lodsb
  96.     or  al,al   ; is al == 0 ?
  97.     jz  halt    ; if (al == 0) jmp halt
  98.     or  eax, 0x0f00
  99.     mov word [ebx], ax
  100.     add ebx, 2
  101.     jmp .loop
  102.  
  103. halt:
  104.     mov esp, kernel_stack_top
  105.     cli     ; clear interrupt flag
  106.     hlt     ; halt execution
  107.  
  108.  
  109. section .bss
  110. align 4
  111. kernel_stack_bottom: equ $
  112.     resb 16384  ; 16 KB
  113. kernel_stack_top:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement