Advertisement
Guest User

Untitled

a guest
Mar 20th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1.  
  2. .code16
  3. .global init # makes our label "init" available to the outside
  4. .global msg
  5. .global boot_disk
  6. .global disk_read
  7. .text
  8.  
  9. #.org 0x7c00
  10.  
  11. .equ PROGRAM_SPACE, 0x7e00
  12.  
  13. #oṃ bhūr bhuvaḥ svaḥ
  14. #tat savitur vareṇyaṃ
  15. #bhargo devasya dhīmahi
  16. #dhiyo yo naḥ pracodayāt
  17.  
  18. init:
  19. mov %dl, (boot_disk) #at boot the disk number is stored in %dl, store it in c har boot_disk for use in disk_read()
  20. .byte 0xcc #hardware interrupt
  21. hlt
  22. mov $msg, %bx
  23. call print
  24. call disk_read
  25. mov $msg, %bx
  26. call print
  27. infiloop_real_mode:
  28. jmp infiloop_real_mode
  29. jmp enter_protected_mode
  30.  
  31. end_loop:
  32. jmp end_loop
  33.  
  34. disk_read:
  35. mov $0x02, %ah
  36. mov $PROGRAM_SPACE, %bx
  37. mov $0x04, %al
  38. mov (boot_disk), %dl
  39. mov $0x00, %ch
  40. mov $0x00, %dh
  41. mov $0x02, %cl
  42.  
  43. int $0x13 #carry flag set on fail
  44. jc disk_read_fail
  45. mov $disk_read_success_msg, %bx
  46. call print
  47. ret
  48.  
  49. disk_read_fail:
  50. mov $disk_read_fail_msg, %bx
  51. call print
  52. ret
  53.  
  54. print: # weirdest phenomena in asm history, made a function run 100% sucessfull y on first assemble
  55.  
  56. mov $0x0e, %ah
  57. print_loop:
  58. mov (%bx), %al # sets AL to the first byte of our message
  59. cmp $0x00, %al je exit_print
  60. int $0x10 # call the function in ah from interrupt 0x10
  61. inc %bx
  62. jmp print_loop
  63. exit_print:
  64. ret
  65.  
  66. enter_protected_mode:
  67. #mov $msg, %bx
  68. call enable_A20
  69. mov $msg, %bx
  70. #call sleep
  71. #call sleep
  72. mov $msg,%bx
  73. call print
  74. #call sleep
  75. mov $msg, %bx
  76. call sleep
  77. #jmp enter_protected_mode
  78. cli #cli clear interupt, interrupts need to be disabled to e nter protected mode
  79. lgdt (gdt_descriptor) #load global table desctiptor
  80. mov %cr0, %eax
  81. or $0x01, %eax
  82. mov %eax, %cr0
  83.  
  84. mov $sector_2_msg, %bx
  85. call print
  86. hlt
  87. # problemer
  88. jmp $0x7e00, $0x0
  89.  
  90. enable_A20:
  91. in $0x92, %al
  92. or %al, 0x02
  93. out %al, $0x92
  94. ret
  95.  
  96. sleep:
  97. mov $0xffff, %ax
  98. xor %bx, %bx #bx = 0x00000
  99. sleep_loop:
  100. dec %ax
  101. cmp %ax, %bx
  102. jne sleep_loop
  103. ret
  104.  
  105. ## Data ------------------------------------
  106. msg:
  107. .asciz "not not\r\n"
  108. disk_read_fail_msg:
  109. .asciz "fail Disk read\r\n"
  110. disk_read_success_msg:
  111. .asciz "sucess Disk read\r\n"
  112. boot_disk:
  113. .byte 0x00
  114.  
  115. #.org 0x7e00
  116.  
  117. #sector 2 data
  118. sector_2_msg:
  119. .asciz "Running in sector 2\r\n"
  120.  
  121. ##global descriptor table (gdt)
  122.  
  123. gdt_nulldesc:
  124. .byte 0x00
  125. .byte 0x00
  126. gdt_codedesc:
  127. .word 0xffff #limit
  128. .word 0x0000 #base (low 16 bit)
  129. .byte 0x00 #base (medium 8 bits)
  130. .byte 0b10011010 #flags(binary)
  131. .byte 0b11001111 #flags(4 bits) limit(4 bits)
  132. .byte 0x00 #base (high 8 bits)
  133. gdt_datadesc:
  134. .word 0xffff #limit
  135. .word 0x0000 #base (low 16 bit)
  136. .byte 0x00 #base (medium 8 bits)
  137. .byte 0b10010010 #flags(binary)
  138. .byte 0b11001111 #flags(4 bits) limit(4 bits)
  139. .byte 0x00 #base (high 8 bits)
  140. gdt_end:
  141. gdt_descriptor:
  142. .word gdt_end - gdt_nulldesc -1
  143. .long gdt_nulldesc
  144.  
  145. .equ CODESEG, gdt_codedesc - gdt_nulldesc
  146. .equ DATASEG, gdt_datadesc - gdt_nulldesc
  147.  
  148. ##ROGRAM_SPACE refers to this portion of memory
  149.  
  150. # mov $sector_2_msg, %bx
  151. # call print
  152.  
  153. # nop
  154. # nop
  155. # nop
  156. # nop
  157.  
  158.  
  159. ## 32 bit protected mode setup
  160. .fill 510-(.-init), 1, 0 # add zeroes to make it 510 bytes long
  161. .word 0xaa55
  162.  
  163. #etter her så blir det 513 til 2048
  164.  
  165. .code32
  166. sector_2:
  167.  
  168. protected_mode_32:
  169. loopinz:
  170. jmp loopinz
  171.  
  172. .byte 0xcc
  173. movl $DATASEG, %eax
  174. movl %eax, %ds
  175. movl %eax, %gs
  176. movl %eax, %ss
  177. movl %eax, %fs
  178. movl %eax, %es
  179. xorl %eax, %eax
  180. #movl %eax, %cs
  181.  
  182. sti
  183.  
  184. movl $0x07690748,0xb8000
  185. # movb $0x41, (0x000b8000)
  186. # movb $0X2A, (0X000B8000)
  187.  
  188. infloop:
  189. jmp infloop
  190.  
  191.  
  192. .fill 2048-(.-protected_mode_32), 1, 0 # zero pad sector 2
  193.  
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement