Guest User

boottwo.asm

a guest
Mar 27th, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [BITS 16] ; Working in 16-bit mode
  2.  
  3. PUSH CS
  4. POP DS
  5. MOV SI, boot_msg ; Sets up to print the boot message
  6. CALL print ; Prints the contents of SI
  7. CALL enable_a20 ; Enables A20 for OVER 4GB of RAM!
  8. MOV SI, pmode_msg ; Sets up to print the Entering PMode message
  9. CALL print ; Prints the contents of SI
  10. CLI ; Clears those pesky interrupts for GDT!
  11. XOR AX, AX ; Logical OR (basically resets, I think?)
  12. MOV DS, AX ; Loads DS register
  13. lgdt[gdt_desc] ; Loads the GDT.
  14. MOV EAX, CR0 ; Setting up PMode, dumps CR0 into EAX
  15. OR EAX, 1 ; Sets bit 0 on CR0
  16. MOV CR0, EAX ; Dumps EAX into CR0, with bits set
  17. JMP 08h:clear_pipe ; Jumps to clear 16-bit instructions
  18.  
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ; STAGE2 SUBROUTINES                                           ;
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22.  
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ; GDT section.                                                 ;
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26. gdt:                    ; Address for the GDT
  27.  
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. ; Null segment, reserved by Intel.                             ;
  30. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  31. gdt_null:               ; Null Segment
  32.         dd 0
  33.         dd 0
  34.  
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ; Code segment, read and execute, nonconforming.               ;
  37. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  38. gdt_code:
  39.   dw 0FFFFh
  40.   dw 0
  41.   db 0
  42.   db 10011010b
  43.   db 11001111b
  44.   db 0
  45.  
  46. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  47. ; Data segment, read and write, expand down.                   ;
  48. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  49. gdt_data:
  50.  dw 0FFFFh
  51.  dw 0
  52.  db 0
  53.  db 10010010b
  54.  db 11001111b
  55.  db 0
  56.  
  57. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  58. ; Calculates the GDT size.                                     ;
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60. gdt_end:
  61.  
  62. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  63. ; GDT descriptor.                                              ;
  64. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  65. gdt_desc:
  66.  dw gdt_end - gdt - 1    ; Limit (size)
  67.  dd gdt                  ; Address of the GDT
  68.  
  69. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  70. ; Prints a string to the screen.                               ;
  71. ; Takes the value of SI to do this.                            ;
  72. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  73. print:          ; Routine: output string in SI to screen
  74.     mov ah, 0Eh     ; int 10h 'print char' function
  75.  
  76. .repeat:
  77.     lodsb           ; Get character from string
  78.     cmp al, 0
  79.     je .done        ; If char is zero, end of string
  80.     int 10h         ; Otherwise, print it
  81.     jmp .repeat
  82.  
  83. .done:
  84.     ret
  85.  
  86. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  87. ; Sets up the A20 line for up to 4GB of memory.                ;
  88. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  89. enable_a20:
  90.     cli                ;Disables interrupts
  91.     push    ax         ;Saves AX
  92.     mov al, 0xdd  ;Look at the command list
  93.     out 0x64, al   ;Command Register
  94.     pop ax          ;Restore's AX
  95.     sti                ;Enables interrupts
  96.     mov si, a20_msg
  97.     call print
  98.     ret
  99.  
  100. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  101. ; 16-bit Declarations.                                         ;
  102. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  103. boot_msg DB 'Stage Two Loaded.',13,10,0 ; Boot message
  104. pmode_msg DB 'Now entering Protected Mode...',13,10,0 ; PMode message
  105. a20_msg DB 'Address line active.',13,10,0 ; A20 active
  106.  
  107.  
  108. [BITS 32] ; Enter 32-bit mode.
  109. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  110. ; Clears previous 16-bit instructions in PMode.                ;
  111. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  112. clear_pipe:
  113.  MOV AX, 10h ; Sets AX for segments
  114.  MOV DS, AX ; Sets data segment
  115.  MOV SS, AX ; Sets stack segment
  116.  MOV ESP, 090000h ; Sets our stack
  117.  JMP hang
  118.  
  119. hang:
  120.  JMP hang
Advertisement
Add Comment
Please, Sign In to add comment