Guest User

Untitled

a guest
Jul 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. [ORG 0x00]
  2. [BITS 16]
  3.  
  4. section .text
  5.  
  6. JMP 0x07C0:START ;
  7.  
  8. START:
  9. mov ax, cs
  10. mov ds, ax
  11. mov es, ax
  12.  
  13. mov ax, 0x0000
  14. mov ss, ax
  15. mov ax, 0xffff
  16. mov sp, ax
  17. mov bp, ax
  18.  
  19. mov si, 0xB800
  20. mov es, si
  21. mov bx, 0x0000
  22. mov cx, 80*25*2
  23.  
  24. CLEAR: ;Clear screen
  25. mov byte[es:bx], 0x00
  26. inc bx
  27. loop CLEAR
  28.  
  29. READ: ;Read disk
  30. mov si, 0x1000
  31. mov es, si
  32. mov bx, 0x0000 ; ES:BX, 0x1000:0000
  33. mov ah, 0x02
  34.  
  35. mov al, 2
  36. mov ch, 0
  37. mov cl, 2
  38. mov dh, 0
  39. mov dl, 0
  40. int 0x13
  41. jnc A20_ENABLE ; No Error -> jump to A20_ENABLE
  42.  
  43. READERROR: ;If failed to read disk,
  44. jmp $
  45.  
  46. A20_ENABLE: ;Enable A20 GATE with BIOS
  47. mov ax, 0x2401
  48. int 0x15
  49. jnc ENTRY
  50.  
  51. A20_FAIL: ;If BIOS Interrupt is not working, use Control Port.
  52. mov al, 2 ;
  53. out 0x92, al
  54.  
  55. ENTRY: ;Entry Point
  56. cli
  57. lgdt [GDTR]
  58. mov eax, cr0
  59. or eax, 1
  60. mov cr0, eax
  61.  
  62. jmp $+2 ; for pipeline
  63. nop
  64. nop
  65.  
  66. jmp dword 0x08:0x10000 ; jump to second stage
  67.  
  68. GDTR: ; GDT
  69. dw GDTEND - GDT - 1
  70. dd GDT+0x7C00
  71.  
  72. GDT:
  73. NULL:
  74. dw 0x00 ;프로세서 예약구간
  75. dw 0x00
  76. db 0x00
  77. db 0x00
  78. db 0x00
  79. db 0x00
  80.  
  81. CODE:
  82. dw 0xFFFF ;LIMIT ADRESS
  83. dw 0x0000 ;BASE ADRESS
  84. db 0x00 ;BASE ADRESS
  85. db 0x9A ;1001 1010 A code segment descriptor (for your kernel, it should have type=0x9A)
  86. db 0xCF ;1100 1111
  87. db 0x00
  88.  
  89. DATA:
  90. dw 0xFFFF
  91. dw 0x0000
  92. db 0x01
  93. db 0x92 ;1001 0010 A data segment descriptor (you cant write to a code segment, so add this with type=0x92)
  94. db 0xCF ;1100 1111
  95. db 0x00
  96.  
  97. VIDEO:
  98. dw 0xFFFF
  99. dw 0x8000
  100. db 0x0B
  101. db 0x92
  102. db 0x40
  103. db 0x00
  104.  
  105. GDTEND:
  106.  
  107. times 510 - ($-$$) db 0x00
  108.  
  109. db 0x55
  110. db 0xAA
  111.  
  112. [ORG 0x10000]
  113. [BITS 32]
  114.  
  115. PROTECTED:
  116. mov ax, 0x10
  117. mov gs, ax
  118. mov ss, ax
  119. mov ds, ax
  120. mov es, ax
  121. mov fs, ax
  122.  
  123. xor esp, esp ; make stack
  124. mov esp, 0xFFFE
  125. mov ebp, 0xFFFE
  126.  
  127. mov si, 0x18 ; setting for print message
  128. mov es, si
  129. mov bx, 0
  130. lea si, [message]
  131. mov ah, 0x0f
  132.  
  133. MESSAGE: ; print loop
  134. lodsb
  135. cmp al, 0
  136. je LOADKERNEL
  137. mov [es:bx], ax
  138. add bx, 2
  139. jmp MESSAGE
  140.  
  141. LOADKERNEL:
  142. jmp dword 0x08:0x10200 ; jump to C kernel
  143.  
  144. message: db 'Protected Mode Entered.', 0
  145.  
  146. times 512 - ($ - $$) db 0x00
  147.  
  148. void write_string();
  149.  
  150. void main(){
  151. write_string(0x09, "helloworld");
  152. }
  153.  
  154. void write_string( int colour, const char *string )
  155. {
  156. volatile char *video = (volatile char*)0xB8000;
  157. while( *string != 0 )
  158. {
  159. *video++ = *string++;
  160. *video++ = colour;
  161. }
  162. }
  163.  
  164. gcc -c -m32 -ffreestanding kernel.c -o kernel.o
  165.  
  166. ld -melf_i386 -Ttext 0x10200 -nostdlib kernel.o -o kernel.tmp
  167.  
  168. objcopy -O binary kernel.tmp kernel.img
  169.  
  170. cat bootloader.img secondstage.img kernel.img > Disk
Add Comment
Please, Sign In to add comment