protected_mode_so.asm 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ; http://stackoverflow.com/a/28645943/895245
  2. [bits 16]
  3. [org 0x7c00]
  4. mov ax, 0
  5. mov ss, ax
  6. mov sp, 0xFFFC
  7. mov ax, 0
  8. mov ds, ax
  9. mov es, ax
  10. mov fs, ax
  11. mov gs, ax
  12. cli
  13. lgdt[gdt_descriptor]
  14. mov eax, cr0
  15. or eax, 0x1
  16. mov cr0, eax
  17. ; TODO why is this needed?
  18. jmp CODE_SEG:b32
  19. [bits 32]
  20. print32:
  21. pusha
  22. ; Video memory address.
  23. mov edx, 0xb8000
  24. .loop:
  25. mov al, [ebx]
  26. ; White on black.
  27. mov ah, 0x0f
  28. cmp al, 0
  29. je .done
  30. mov [edx], ax
  31. add ebx, 1
  32. add edx, 2
  33. jmp .loop
  34. .done:
  35. popa
  36. ret
  37. b32:
  38. mov ax, DATA_SEG
  39. mov ds, ax
  40. mov es, ax
  41. mov fs, ax
  42. mov gs, ax
  43. mov ss, ax
  44. mov ebp, 0x2000
  45. mov esp, ebp
  46. mov ebx, message
  47. call print32
  48. jmp $
  49. gdt_start:
  50. gdt_null:
  51. dd 0x0
  52. dd 0x0
  53. gdt_code:
  54. dw 0xffff
  55. dw 0x0
  56. db 0x0
  57. db 10011010b
  58. db 11001111b
  59. db 0x0
  60. gdt_data:
  61. dw 0xffff
  62. dw 0x0
  63. db 0x0
  64. db 10010010b
  65. db 11001111b
  66. db 0x0
  67. gdt_end:
  68. gdt_descriptor:
  69. dw gdt_end - gdt_start
  70. dd gdt_start
  71. CODE_SEG equ gdt_code - gdt_start
  72. DATA_SEG equ gdt_data - gdt_start
  73. message db 'hello world', 0
  74. [SECTION signature start=0x7dfe]
  75. dw 0AA55h