Bootloader FAT12


/ Published in: Assembler
Save to your folder(s)

FASM 1.47


Copy this code and paste it in your HTML
  1. use16
  2. org 7C00h
  3.  
  4. fat = 7E00h
  5. main_cat = 9000h
  6. free_mem = 0AC00h
  7.  
  8. jmp codestart
  9. nop
  10.  
  11. db 'FizykOS '
  12. dw 512
  13. db 1
  14. dw 1
  15. db 2
  16. dw 224
  17. dw 2880
  18. db 0F0h
  19. dw 9
  20. dw 18
  21. dw 2
  22. dw 0
  23. db 0 ;0 = FAT12, 1 = FAT16, 2 = FAT32
  24.  
  25. codestart:
  26.  
  27. push cs
  28. push cs
  29. pop ds
  30. pop es
  31.  
  32. ;load FAT
  33.  
  34. mov ax,1
  35. call sec_log_to_phys
  36. mov ah,2
  37. mov al,9
  38. mov bx,fat
  39. int 13h
  40.  
  41. ;load root folder
  42.  
  43. mov ax,19
  44. call sec_log_to_phys
  45. mov ah,2
  46. mov al,14
  47. mov bx,main_cat
  48. int 13h
  49.  
  50. ;search for file kernel.bin
  51. mov cx,224
  52. loop_search_001:
  53. mov si,free_mem
  54. mov ax,32
  55. mul cx
  56. sub si,ax
  57. push si
  58. mov di,filename
  59. push cx
  60. mov cx,11
  61. cld
  62. repe cmpsb
  63. pop cx
  64. pop si
  65. loopne loop_search_001
  66. cmp cx,0
  67. je not_found
  68.  
  69. ;file found
  70.  
  71. add si,1Ah ;point at first cluster number
  72.  
  73. mov ax,[si] ;ax - number of the first cluster
  74. mov bx,main_cat
  75.  
  76. loop_read_file:
  77. push ax
  78. call cluster_to_phys
  79. mov ax,0201h
  80. int 13h ;load cluster (sector)
  81. add bx,512
  82. pop ax
  83. push ax
  84. mov cx,3
  85. mul cx ;multiply number by 3
  86. shr ax,1 ;and divide by 2
  87. mov si,ax
  88. mov cx,[si+fat] ;word at that address
  89. pop ax
  90. and ax,1
  91. cmp ax,0 ;was even?
  92. je next_001
  93. shr cx,4 ;no, shift 4 bits right
  94. jmp next_002
  95. next_001:
  96. and cx,0FFFh ;yes, and 0FFFh
  97. next_002:
  98. mov ax,cx ;result -> ax
  99. cmp ax,0FF7h ;if > 0FF7h, end reading
  100. ja file_loaded
  101. jmp loop_read_file
  102.  
  103. file_loaded:
  104.  
  105. push 900h
  106. push 900h
  107. pop ds
  108. pop es
  109. jmp 900h:0 ;jump to 0900:0000 (where the file was loaded)
  110.  
  111. not_found:
  112. mov si,msg
  113. loop_write:
  114. cmp byte [si],0
  115. je next_003
  116. mov ah,0Eh
  117. mov al,[si]
  118. int 10h
  119. inc si
  120. jmp loop_write
  121. next_003:
  122. cli
  123. hlt
  124.  
  125. ;data area start = 1 + 18 + 14 = 33
  126.  
  127. cluster_to_phys: ;ax = cluster -> ax = logical sector
  128. add ax,31
  129. sec_log_to_phys: ;ax = logical -> cx = cylinder + sector, dh = head
  130. push bx
  131. xor dx,dx
  132. mov bx,18
  133. div bx ;ax = log div 18, dx = log mod 18
  134. inc dx ;dx = physical sector
  135. mov cx,ax ;cx = log div 18
  136. push cx
  137. and cx,1 ;cx = (log div 18) mod 2 = head
  138. mov dh,cl ;dh = head
  139. pop cx
  140. shr cx,1 ;cx = (log div 18) div 2 = cylinder
  141. rol cx,2
  142. shl cl,6
  143. xor cl,cl
  144. or cl,dl ;cx = cylinder & sector
  145. xor dl,dl ;dl = 0 - floppy
  146. pop bx
  147. ret
  148.  
  149. filename db 'KERNEL BIN',0
  150. msg db 'kernel.bin not found.',0
  151.  
  152. times 7c00h+510-$ db 0
  153.  
  154. dw 0AA55h

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.