Advertisement
MichaelPetch

pmdosmasm.asm

May 13th, 2020
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. .386P
  2. CGROUP GROUP _TEXT, _TEXT32, _DATA, STACK
  3. _TEXT SEGMENT USE16 BYTE PUBLIC 'CODE'
  4. _TEXT ENDS
  5. _TEXT32 SEGMENT USE32 BYTE PUBLIC 'CODE'
  6. _TEXT32 ENDS
  7. _DATA SEGMENT USE16 WORD PUBLIC 'DATA'
  8. _DATA ENDS
  9. STACK SEGMENT USE16 PARA STACK 'STACK'
  10. STACK ENDS
  11.  
  12. _DATA SEGMENT
  13. gdt label byte
  14. dq 0
  15.  
  16. c_limit_lo dw 0ffffh
  17. c_base_lo dw 0
  18. c_base_mid db 0
  19. c_priv db 10011110b ;present set, highest priv, type set, conforming, read
  20. c_limit_hi db 11001111b ;granularity set, operand size 32
  21. c_base_hi db 0
  22.  
  23. d_limit_lo dw 0ffffh
  24. d_base_lo dw 0
  25. d_base_mid db 0
  26. d_priv db 10010010b ;present set, highest priv, type clr, expand dn, write
  27. d_limit_hi db 11001111b ;granularity set, big set
  28. d_base_hi db 0
  29.  
  30. ; Data selector so we can access memory in a flat memory model
  31. dz_limit_lo dw 0ffffh
  32. dz_base_lo dw 0
  33. dz_base_mid db 0
  34. dz_priv db 10010010b ;present set, highest priv, type clr, expand dn, write
  35. dz_limit_hi db 11001111b ;granularity set, big set
  36. dz_base_hi db 0
  37. gdt_end label byte
  38.  
  39. gdtr label fword
  40. gdt_limit dw gdt_end-gdt-1
  41. gdt_addr dd ?
  42.  
  43. vidmem_ptr dd 0b8000h
  44. in_pmode_str db "Processor already in protected mode - exiting", 0ah, 0dh, "$"
  45. banner_str db "The quick brown fox jumps over the lazy dog", 0
  46.  
  47. _DATA ENDS
  48.  
  49. _TEXT SEGMENT
  50. assume cs:CGROUP, ds:CGROUP
  51. start:
  52. mov ax, cs
  53. mov ds, ax
  54. mov ss, ax
  55. mov sp, offset CGROUP:stack_top
  56. ;Set the stack pointer relative to beginning of group
  57. ;calc phys address of current code segment and
  58. ;insert it into code and data descriptors
  59.  
  60. call check_pmode ; Check if we are already in protected mode
  61. ; This may be the case if we are in a VM8086 task.
  62. ; EMM386 and other expanded memory manager often
  63. ; run DOS in a VM8086 task. DOS extenders will have
  64. ; the same effect
  65.  
  66. jz not_prot_mode ; If not in protected mode proceed to switch
  67. mov dx, offset CGROUP:in_pmode_str
  68. ; otherwise print an error and exit back to DOS
  69. mov ah, 9h
  70. int 21h ; Print Error
  71.  
  72. mov ax, 4c00h ; DOS Exit program
  73. int 21h
  74.  
  75. not_prot_mode:
  76. xor eax, eax
  77. mov ax, cs
  78. shl eax, 4 ;multiply cs by 16 to get phys address of seg
  79. mov edx, eax
  80. mov c_base_lo, ax
  81. mov d_base_lo, ax ;low word
  82. shr eax, 16
  83. mov c_base_mid, al
  84. mov d_base_mid, al ;middle byte
  85. mov c_base_hi, ah
  86. mov d_base_hi, ah ;high byte
  87. add edx, offset CGROUP:gdt ;add offset of gdt
  88. mov gdt_addr, edx ;gdt address set
  89.  
  90. ;attempt to enter protected mode
  91. cli ;disable interrupts
  92. in al, 70h
  93. or al, 80h
  94. out 70h, al ;turn off nonmasked interrupts
  95. in al, 92h
  96. or al, 2
  97. out 92h, al ;enable A20 line
  98.  
  99. lgdt [gdtr]
  100. mov eax, cr0
  101. or eax, 1
  102. mov cr0, eax ;enter protected mode
  103.  
  104. db 66h ;specify 32-bit operand
  105. db 0eah ;manually encoded "jmp 8h:enter_32"
  106. dw near ptr CGROUP:enter_32
  107. dw 0
  108. dw 8 ; In protected mode after executed
  109.  
  110. ; 16-bit functions that run in real mode
  111.  
  112. ; Check if protected mode is enabled, effectively checking if we are
  113. ; in in a VM8086 task. Set ZF to 1 if in protected mode
  114.  
  115. check_pmode PROC
  116. smsw ax
  117. test ax, 1
  118. ret
  119. check_pmode ENDP
  120.  
  121. _TEXT ENDS
  122.  
  123. _TEXT32 SEGMENT
  124. enter_32:
  125. ; Set FS to a flat memory selector (18h) to more easily
  126. ; reference data from the start of memory
  127. mov eax, 18h
  128. mov fs, eax
  129.  
  130. mov eax, 10h ; Set SS=ES=DS= selector 10h
  131. mov ds, eax
  132. mov es, eax
  133. mov ss, eax
  134.  
  135. movzx esp, sp ; Zero extend SP to 32 bits (zero upper 16-bits of ESP)
  136.  
  137. ; Print banner strong in upper left of screen with white on magenta colors
  138. mov esi, offset CGROUP:banner_str
  139. mov ah, 5fh ; Attribute WHite on Magenta.
  140. call print_string_pm
  141.  
  142. back:jmp back ; but always triple faults on mov
  143.  
  144. ; Function: print_string_pm
  145. ; Display a string to the console on display page 0 in protected mode.
  146. ; Very basic. Doesn't update hardware cursor, doesn't handle scrolling,
  147. ; LF, CR, TAB. FS must be a flat memory selector.
  148. ;
  149. ; Inputs: ESI = Offset of address to print
  150. ; AH = Attribute of string to print
  151. ; Clobbers: EAX
  152. ; Returns: None
  153.  
  154. print_string_pm PROC
  155. push edi
  156. push esi
  157.  
  158. mov edi, [vidmem_ptr] ; Start from video address stored at vidmem_ptr
  159. jmp .getchar
  160. .outchar:
  161. mov fs:[edi], ax
  162. add edi, 2 ; Output character to video display
  163. .getchar:
  164. lodsb ; Load next character from string
  165. test al, al ; Is character NUL?
  166. jne .outchar ; If not, go back and output character
  167.  
  168. mov [vidmem_ptr], edi ; Update global video pointer
  169. pop esi
  170. pop edi
  171. ret
  172. print_string_pm ENDP
  173.  
  174. _TEXT32 ENDS
  175.  
  176. STACK SEGMENT
  177. the_stack db 64 dup (?) ;64 byte stack
  178. stack_top label byte
  179. STACK ENDS
  180.  
  181. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement