Guest User

minimal_reproducible_example_stackoverflow

a guest
Sep 22nd, 2020
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %define Kernel_load_position_macro 0x1000
  2.  
  3. [bits 16]
  4. [org 0x0600]
  5.  
  6. ; This section moves the MBR from 0x7C00 to 0x0600
  7.  
  8. __ENTRY:    ; Old entry point
  9.     cli          ; Clear Interrupts
  10.     xor ax, ax   ; Zero out AX (set to 0)
  11.     mov ds, ax   ; Set Data Segment to 0 (AX)
  12.     mov es, ax   ; Set Extra Segment to 0 (AX)
  13.     mov ss, ax   ; Set Stack Segment to 0 (AX)
  14.     mov sp, ax   ; Set Stack Pointer to 0 (AX)
  15. __SET__LOWER__ENTRY:
  16.         mov cx, 0x0100   ; 256 WORDs in MBR
  17.         mov si, 0x7C00   ; Current MBR Address
  18.         mov di, 0x0600   ; New MBR Address
  19.         rep movsw        ; Copy MBR
  20.     jmp 0:__LOWER_ENTRY  ; Jump to new Address
  21.  
  22.  
  23. __LOWER_ENTRY: ; New entry point
  24.     sti
  25.     mov al, 1 ; Load one sector
  26.     call Load_sectors ; Call the loading routine
  27.     jmp Enter_protected_mode ; Enter protected mode
  28.  
  29.  
  30. ; Calls interrupt 13 to load sectors into memory
  31. Load_sectors:
  32.     pusha
  33.     mov ah, 02h
  34.     mov ch, 0x0
  35.     mov cl, 0x02
  36.     mov dh, 0x0
  37.     mov dl, 0x0
  38.     mov bx, Kernel_load_position_macro ; Loads to 0x1000
  39.     int 13h
  40.     jc Infinite_loop    ; Jump to the infinite loop if interrupt set carry flag
  41.     popa
  42.     ret
  43.  
  44.  
  45. Infinite_loop:
  46.     jmp $
  47.  
  48.  
  49. Enter_protected_mode:
  50.     cli             ; Disable interrupts
  51.     lgdt [GDTR]     ; Load GDT
  52.     pusha
  53.  
  54.     mov eax, cr0    ;
  55.     or al, 1        ; Those three instructions should enable protected mode
  56.     mov cr0, eax    ;
  57.  
  58.     popa
  59.     jmp 0x08:Far_Jump_into_kernel ; What does 0x08 mean here?
  60.  
  61. ; Switch to 32 bits
  62. [bits 32]
  63.  
  64. Far_Jump_into_kernel:
  65.     mov ax, 0x10
  66.     mov ds, ax
  67.     mov ss, ax
  68.     mov fs, ax
  69.     mov es, ax
  70.     mov gs, ax
  71.  
  72.     jmp Kernel_load_position_macro ; Jump to the loaded sector
  73.  
  74.  
  75. ; This is the GDT
  76.  
  77. GDT:
  78. GDT_NULL_DESC:
  79.     dd 0            ; null descriptor
  80.     dd 0
  81.  
  82. GDT_CODE_DESC:
  83.     dw 0xFFFF       ; limit low
  84.     dw 0            ; base low
  85.     db 0            ; base middle
  86.     db 10011010b    ; access
  87.     db 11001111b    ; granularity
  88.     db 0            ; base high
  89.  
  90. GDT_DATA_DESC:
  91.     dw 0xFFFF       ; data descriptor
  92.     dw 0            ; limit low
  93.     db 0            ; base low
  94.     db 10010010b    ; access
  95.     db 11001111b    ; granularity
  96.     db 0            ; base high
  97.  
  98. GDTR:
  99.     Limit dw 24         ; length of GDT
  100.     Base dd GDT_NULL_DESC   ; base of GDT
  101.  
  102.  
  103. times (446 - ($-$$)) db 0      ; Pad to Partition table
  104.  
  105. ; This is the partition entry for kernel
  106.  
  107. PT1:                           ; First Partition Entry ( KERNEL HERE )
  108.  
  109.     db 0x80                     ; Bootable ( Active ) ( 8 bits )
  110.  
  111.     db 0x00                     ; Starting Head ( Start at head 0 ) ( 8 bits )
  112.     db 0b00000010               ; Starting Sector ( Start at Sector 2 ) ( 6 bits ) NOTE: 2 LSB are for the following cylinder field
  113.     db 0b00000000               ; Starting Cylinder ( Start at Cylinder 0 ) ( 10 bits )
  114.  
  115.     db 0x21                     ; System ID ( Filysystem ) ( 21, Reserved )
  116.  
  117.     db 0x00                     ; Ending Head ( End at head 0 ) ( 8 bits )
  118.     db 0b00000100               ; Ending Sector ( End at Sector 4 ) ( 6 bits ) NOTE: 2 LSB are for the following cylinder field
  119.     db 0b00000000               ; Ending Cylinder ( End at Cylinder 0 ) ( 10 bits )
  120.  
  121.     db 0x00                     ; | Relative sector ( 32 bits )
  122.     db 0x00                     ; |
  123.     db 0x00                     ; |
  124.     db 0x01                     ; |
  125.  
  126.     db 0x00                     ; | Total sectors in partition ( 32 bits )
  127.     db 0x00                     ; |
  128.     db 0x00                     ; |
  129.     db 0x02                     ; |
  130.  
  131.  
  132. PT2: times 48 db 0
  133.  
  134.  
  135. dw 0xAA55                     ; Magic Word
  136.  
  137. ; Start of second sector here
  138.  
  139. [bits 32]
  140.  
  141. Kernel_Entry_Point:
  142.     mov esi, Hello_World_message_label
  143.     mov ah, 0x0F ; White on black
  144.     call Print_Until_Null ; Print from ESI
  145.  
  146.     jmp $ ; Loop infinitely
  147.  
  148.  
  149. ; This prints a single character from AL at the offset of ECX
  150. Print_Single_Character:
  151.     pushad
  152.     mov ebx, 0xb8000
  153.     add ebx, ecx
  154.     mov [ebx], eax
  155.     popad
  156.     ret
  157.  
  158. ; This subroutine uses LODSB and Print_Single_Character
  159. ; to print until a null (ascii 0) character is loaded
  160. Print_Until_Null:
  161.     pushad
  162. .internal_loop:
  163.     lodsb
  164.     cmp al, 0
  165.     je .internal_end
  166.     call Print_Single_Character
  167.     add ecx, 0x02 ; Move to the next position in video memory
  168.     jmp .internal_loop
  169. .internal_end:
  170.     popad
  171.     ret
  172.  
  173.  
  174. Hello_World_message_label:
  175.     db "Hello, World!", 0
  176.  
  177.  
  178. times (1024 - ($-$$)) db 0      ; Pad to end of 2nd sector
Advertisement
Add Comment
Please, Sign In to add comment