Advertisement
MichaelPetch

79315836 - Fix OPs kernel.asm

Dec 29th, 2024 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. [org 0x8000]
  2. [bits 16]
  3.  
  4. jmp short start
  5.  
  6. gdt_start:
  7. dq 0x0000000000000000
  8.  
  9. dq 0x00CF9A000000FFFF
  10. dq 0x00CF92000000FFFF
  11.  
  12. dq 0x00CF92000000FFFF
  13.  
  14. gdt_end:
  15.  
  16. gdt_descriptor:
  17. dw gdt_end - gdt_start - 1
  18. dd gdt_start
  19.  
  20. print:
  21. push si
  22. push ax
  23. push bx
  24.  
  25. .loop:
  26. lodsb
  27. or al, al
  28. jz .done
  29.  
  30. mov ah, 0x0E
  31. mov bh, 0
  32. int 0x10
  33.  
  34. jmp .loop
  35.  
  36. .done:
  37. pop bx
  38. pop ax
  39. pop si
  40. ret
  41.  
  42. start:
  43. cli
  44. xor ax, ax ; Set ES=DS=0
  45. mov es, ax
  46. mov ds, ax
  47.  
  48. mov ss, ax ; Set SS:SP (stackpointer) to 0x0000:0x7c00
  49. ; Will grow down beneath bootloader
  50. mov sp, 0x7c00
  51.  
  52. mov si, start_sixteen
  53. call print
  54.  
  55. in al, 0x64
  56. .wait_ibe:
  57. test al, 0x02
  58. jnz .wait_ibe
  59. mov al, 0xD1
  60. out 0x64, al
  61. .wait_ibe2:
  62. in al, 0x64
  63. test al, 0x02
  64. jnz .wait_ibe2
  65. mov al, 0xDF
  66. out 0x60, al
  67.  
  68. first_sixteen:
  69. cli
  70. lgdt [gdt_descriptor]
  71.  
  72. mov eax, cr0
  73. or eax, 1
  74. mov cr0, eax
  75.  
  76. jmp dword 0x08:.afterstart
  77.  
  78. [bits 32]
  79.  
  80. .afterstart:
  81. mov ax, 0x10
  82. mov ds, ax
  83. mov es, ax
  84. mov fs, ax
  85. mov gs, ax
  86. mov ss, ax
  87. mov esp, 0x7c00 ; Set protected mode stack to 0x7c00
  88.  
  89. ; Initialize the entire BSS area to zero
  90. mov ecx, _bss_end-_bss_start ; Length of region in ECX
  91. xor eax, eax ; AL=0 (also used for rep stos)
  92. mov edi, _bss_start ; Offset of BSS region
  93. rep stosb ; Set ECX bytes of data at ES:[EDI] to AL(0)
  94.  
  95. ; Remember parameters are passed right to left. Push in opposite order
  96.  
  97. ; Setup exception 0 (div by zero)
  98. push 0x8E ; Attribute for 32-bit interrupt gate (enabled)
  99. push 0x08 ; Kernel code selector (from GDT)
  100. push isr_divby0 ; Handler for exception
  101. push 0x00 ; div by 0 (arithmetic error) is exception 0x00
  102. call isg
  103. add esp, 4*4 ; Remove the 4 parameters
  104.  
  105. ; Setup exception 6 (invalid opcode)
  106. push 0x8E ; Attribute for 32-bit interrupt gate (enabled)
  107. push 0x08 ; Kernel code selector (from GDT)
  108. push isr_invopcode ; Handler for invalid opcode
  109. push 0x06 ; Invalid code is exception 0x06
  110. call isg
  111. add esp, 4*4 ; Remove the 4 parameters
  112.  
  113. call imake ; Set IDTR (via lidt)
  114.  
  115. ; Cause invalid opcode exception
  116. ; ud2
  117.  
  118. ; Cause division by 0 exception
  119. xor ax, ax
  120. div al
  121.  
  122. cli
  123. .hltloop:
  124. hlt
  125. jmp .hltloop ; Infinite loop
  126.  
  127. isr_divby0:
  128. ; Replace with a proper interrupt/exception handler
  129. hlt ; Inifinite loop
  130. iret
  131.  
  132. isr_invopcode:
  133. ; Replace with a proper interrupt/exception handler
  134. hlt ; Inifinite loop
  135. iret
  136.  
  137. i686IDTLoad:
  138. mov eax, [esp + 4]
  139. lidt [eax]
  140. ret
  141.  
  142. ; void isg (uint32_t entrynum, void *handler, uint16_t selector, uint8_t attribs)
  143. ; Setup interrupt gate entrynum in the IDT with given handler function, selector, and attributes
  144. ;
  145. ; Parameters pushed on stack
  146. ;
  147. isg:
  148. push ebp
  149. mov ebp, esp
  150. sub esp, 8
  151. mov edx, dword [ebp+10H]
  152. mov eax, dword [ebp+14H]
  153. mov word [ebp-4H], dx
  154. mov byte [ebp-8H], al
  155. mov eax, dword [ebp+0CH]
  156. mov edx, eax
  157. mov eax, dword [ebp+8H]
  158. mov word [IDT+eax*8], dx
  159. mov eax, dword [ebp+8H]
  160. movzx edx, word [ebp-4H]
  161. mov word [IDT+2H+eax*8], dx
  162. mov eax, dword [ebp+8H]
  163. mov byte [IDT+4H+eax*8], 0
  164. mov eax, dword [ebp+8H]
  165. movzx edx, byte [ebp-8H]
  166. mov byte [IDT+5H+eax*8], dl
  167. mov eax, dword [ebp+0CH]
  168. shr eax, 16
  169. mov edx, eax
  170. mov eax, dword [ebp+8H]
  171. mov word [IDT+6H+eax*8], dx
  172. nop
  173. leave
  174. ret
  175.  
  176. ; void ieg (uint32_t entrynum)
  177. ; Enable interrupt gate for entrynum in the IDT
  178. ;
  179. ; Parameters pushed on stack
  180. ;
  181. ieg:
  182. push ebp
  183. mov ebp, esp
  184. mov eax, dword [ebp+8H]
  185. movzx eax, byte [IDT+5H+eax*8]
  186. or eax, 0FFFFFF80H
  187. mov edx, eax
  188. mov eax, dword [ebp+8H]
  189. mov byte [IDT+5H+eax*8], dl
  190. nop
  191. pop ebp
  192. ret
  193.  
  194. ; void idg (uint32_t entrynum)
  195. ; Disable interrupt gate for entrynum in the IDT
  196. ;
  197. ; Parameters pushed on stack
  198. ;
  199. idg:
  200. push ebp
  201. mov ebp, esp
  202. mov eax, dword [ebp+8H]
  203. movzx eax, byte [IDT+5H+eax*8]
  204. and eax, 7FH
  205. mov edx, eax
  206. mov eax, dword [ebp+8H]
  207. mov byte [IDT+5H+eax*8], dl
  208. nop
  209. pop ebp
  210. ret
  211.  
  212. imake:
  213. push IDTDescriptor ; Argument 1 is address of IDT record
  214. call i686IDTLoad
  215. pop eax ; Remove arguments
  216. ret
  217.  
  218. IDTDescriptor:
  219. dw IDT_SIZE - 1
  220. dd IDT
  221.  
  222. start_prog:
  223. mov al, 2
  224. mov ax, 1004
  225. mov [Xval], eax
  226. mov ax, 700
  227. mov [Yval], eax
  228. mov ax, 10
  229. mov [Wval], eax
  230. mov ax, 20
  231. mov [Hval], eax
  232. lea si, [PySpr]
  233.  
  234. call draw_spr
  235.  
  236. .halt:
  237. cli
  238. hlt
  239.  
  240. draw_spr:
  241. xor edx, edx
  242. mov ecx, [Hval]
  243. draw_sprite:
  244. mov eax, [Yval]
  245. mov ebx, 1024
  246. mul ebx
  247. add eax, [Xval]
  248. add edi, eax
  249. push ecx
  250. mov ecx, [Wval]
  251. draw_pixels:
  252. mov al, [si]
  253. cmp al, 5
  254. je skip_pixel_2
  255. cld
  256. movsb
  257. jmp skip_pixel
  258. skip_pixel_2:
  259. inc edi
  260. skip_pixel:
  261. inc si
  262. loop draw_pixels
  263. pop ecx
  264. mov eax, [Yval]
  265. inc eax
  266. mov [Yval], eax
  267. loop draw_sprite
  268. ret
  269.  
  270. start_sixteen: db 'KERNAL START', 0x0D, 0x0A, 0
  271.  
  272. Xval dd 0
  273. Yval dd 0
  274. Wval dd 0
  275. Hval dw 0
  276.  
  277. PySpr db 5,5,5,2,2,2,2,5,5,5
  278. db 5,5,2,2,2,2,2,2,5,5
  279. db 5,2,2,2,2,2,2,2,2,5
  280. db 2,2,2,2,2,2,2,2,2,2
  281. db 5,8,8,8,8,8,8,8,8,5
  282. db 5,8,15,0,8,8,0,15,8,5
  283. db 5,8,8,8,8,8,8,8,8,5
  284. db 5,8,8,0,0,0,0,8,8,5
  285. db 5,5,8,8,8,8,8,8,5,5
  286. db 2,2,2,2,2,2,2,2,2,2
  287. db 2,2,2,2,2,2,2,2,2,2
  288. db 2,5,2,2,2,2,2,2,5,2
  289. db 2,5,2,2,2,2,2,2,5,2
  290. db 2,5,2,2,2,2,2,2,5,2
  291. db 8,5,2,2,2,2,2,2,5,8
  292. db 5,5,6,6,6,6,6,6,5,5
  293. db 5,5,6,6,5,5,6,6,5,5
  294. db 5,5,6,6,5,5,6,6,5,5
  295. db 5,5,6,6,5,5,6,6,5,5
  296. db 5,7,7,7,5,5,7,7,7,5
  297.  
  298. section .bss
  299. _bss_start:
  300. IDT resq 256 ; Create an IDT with 256 entries
  301. IDT_SIZE equ $-IDT
  302. _bss_end:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement