您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

bootsect.asm 2.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ; Boot sector, freely inspired by http://jojo.ouvaton.org/dossiers/boot_sector/tutorial02.html
  2. ; Sets a GDT and protected mode
  3. ; Copy the kernel (or stage2) to 0x1000 (linear)
  4. ; Max kernel size is 25600 bytes (50 sectors)
  5. %define BASE 0x100 ; 0x0100:0x0 = 0x1000
  6. %define KSIZE 50
  7. [BITS 16]
  8. [ORG 0x0]
  9. ; Code starts here, jump to our real entry point, skipping print
  10. jmp start
  11. ; print string using BIOS
  12. print:
  13. push ax
  14. push bx
  15. .start:
  16. lodsb ; ds:si -> al
  17. cmp al,0
  18. jz .fin
  19. mov ah,0x0E ; 0x0e function of BIOS 0x10 interruption
  20. mov bx,0x07 ; bx -> attribute, al -> ascii
  21. int 0x10
  22. jmp .start
  23. .fin:
  24. pop bx
  25. pop ax
  26. ret
  27. ; Entry point
  28. start:
  29. mov [bootdrv],dl ; boot drive
  30. ; Stack and section initialization at 0x07C0
  31. mov ax,0x07C0
  32. mov ds,ax
  33. mov es,ax
  34. mov ax,0x8000 ; stack at 0xFFFF
  35. mov ss,ax
  36. mov sp, 0xf000
  37. ; print 'Loading kernel ...'
  38. mov si, ldKernel
  39. call print
  40. ; Load kernel
  41. xor ax,ax
  42. int 0x13
  43. push es
  44. mov ax,BASE
  45. mov es,ax
  46. mov bx,0
  47. mov ah,2
  48. mov al,KSIZE
  49. mov ch,0
  50. mov cl,2
  51. mov dh,0
  52. mov dl,[bootdrv]
  53. int 0x13
  54. pop es
  55. ; GDT pointer
  56. mov ax,gdtend ; GDT limit
  57. mov bx,gdt
  58. sub ax,bx
  59. mov word [gdtptr],ax
  60. xor eax,eax ; GDT linear address
  61. xor ebx,ebx
  62. mov ax,ds
  63. mov ecx,eax
  64. shl ecx,4
  65. mov bx,gdt
  66. add ecx,ebx
  67. mov dword [gdtptr+2],ecx
  68. ; pmode
  69. cli
  70. lgdt [gdtptr] ; load GDT
  71. mov eax, cr0
  72. or ax,1
  73. mov cr0,eax ; enter Protected Mode
  74. jmp next
  75. next:
  76. mov ax,0x10 ; data segment
  77. mov ds,ax
  78. mov fs,ax
  79. mov gs,ax
  80. mov es,ax
  81. mov ss,ax
  82. mov esp,0x9F000
  83. jmp dword 0x8:0x1000 ; jump to kernel _start at 0x1000 of 'second' selector (*8)
  84. ;--------------------------------------------------------------------
  85. bootdrv: db 0
  86. ldKernel db "Loading kernel ...",13,10,0
  87. ;--------------------------------------------------------------------
  88. gdt:
  89. db 0,0,0,0,0,0,0,0
  90. gdt_cs:
  91. db 0xFF,0xFF,0x0,0x0,0x0,10011011b,11011111b,0x0
  92. gdt_ds:
  93. db 0xFF,0xFF,0x0,0x0,0x0,10010011b,11011111b,0x0
  94. gdtend:
  95. ;--------------------------------------------------------------------
  96. gdtptr:
  97. dw 0 ; limit
  98. dd 0 ; base
  99. ;--------------------------------------------------------------------
  100. ;; fill remaining space with NOPs
  101. times 510-($-$$) db 144
  102. dw 0xAA55