home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progasm / protex.arj / PROT2.ASM < prev    next >
Assembly Source File  |  1989-03-20  |  21KB  |  437 lines

  1.                                PAGE    ,132
  2.                                 TITLE   Protected mode programming #2
  3.  
  4.                                 .286P
  5.  
  6. MakeDescriptor  MACRO   Limit,BaseHword,BaseLbyte,Access
  7.                 Dw      Limit
  8.                 Dw      BaseHword
  9.                 Db      BaseLbyte
  10.                 Db      Access
  11.                 Dw      0
  12.                 ENDM
  13.  
  14. Descriptor      STRUC
  15. Lim             Dw      ?               ; Limit
  16. BaseW           Dw      ?               ; Base address high
  17. BaseB           Db      ?               ; Base address low (byte)
  18. Acc             Db      ?               ; Access bytes right
  19.                 Dw      0               ; Reserved
  20. Descriptor      ENDS
  21.  
  22.  
  23. ; Access rights typical equates
  24. CODE_SEGMENT    EQU     10011011b       ; Code Segmnet Access Byte
  25. DATA_SEGMENT    EQU     10010011b       ; Data Segment Access byte
  26. TRAP_GATE       EQU     10000111b       ; Trap gate
  27. INT_GATE        EQU     10000110b       ; Interrupt gate access
  28.  
  29. Null_Selector           EQU     0h      ;
  30. Gdt_Alias_Selector      EQU     8h      ;
  31. Idt_Alias_Selector      EQU     10h     ;
  32. Ds_Selector             EQU     18h     ;
  33. Es_Selector             EQU     20h     ;
  34. Ss_Selector             EQU     28h     ;
  35. Main_Code_Selector      EQU     30h     ;
  36. BiosCs_Selector         EQU     38h     ;
  37. Int_Code_Selector       EQU     40h     ;
  38.  
  39. GDT             Segment
  40. ;=============================================================================;
  41. ; The GDT.                                                                    ;
  42. ;=============================================================================;
  43. Gdt_Start       LABEL   BYTE
  44. MakeDescriptor  0,0,0,0                     ; Null descriptor
  45. MakeDescriptor  Gdt_End,0,0,DATA_SEGMENT    ; Gdt Alias
  46. MakeDescriptor  Idt_End,0,0,DATA_SEGMENT    ; IDT Alias
  47. MakeDescriptor  0,0,0,DATA_SEGMENT          ; Ds Descriptor
  48. MakeDescriptor  4000h,8000h,0Bh,DATA_SEGMENT; Es (Color Screen)
  49. MakeDescriptor  0,0,0,DATA_SEGMENT          ; Stack
  50. MakeDescriptor  0,0,0,CODE_SEGMENT          ; Code to execute
  51. MakeDescriptor  0,0,0,CODE_SEGMENT          ; Bios Temp
  52. MakeDescriptor  0,0,0,CODE_SEGMENT          ; Ex/Int code seg
  53. Gdt_End         LABEL   BYTE
  54. GDT             Ends
  55.  
  56. IDT             Segment
  57. ;=============================================================================;
  58. ; The IDT.                                                                    ;
  59. ;=============================================================================;
  60. Idt_Start       LABEL   BYTE
  61. MakeDescriptor  I00,Int_Code_Selector,0,TRAP_GATE ; divide by 0
  62. MakeDescriptor  I01,Int_Code_Selector,0,TRAP_GATE ; Single Step
  63. MakeDescriptor  I02,Int_Code_Selector,0,INT_GATE  ; NMI
  64. MakeDescriptor  I03,Int_Code_Selector,0,TRAP_GATE ; Breakpoint trap
  65. MakeDescriptor  I04,Int_Code_Selector,0,TRAP_GATE ; INTO trap
  66. MakeDescriptor  I05,Int_Code_Selector,0,TRAP_GATE ; BOUNDS trap
  67. MakeDescriptor  I06,Int_Code_Selector,0,TRAP_GATE ; Invalid Opcode
  68. MakeDescriptor  I07,Int_Code_Selector,0,TRAP_GATE ; Math Cp Not Avail
  69. MakeDescriptor  I08,Int_Code_Selector,0,TRAP_GATE ; Double Fault
  70. MakeDescriptor  I09,Int_Code_Selector,0,TRAP_GATE ; Cp operand past lim
  71. MakeDescriptor  I0A,Int_Code_Selector,0,TRAP_GATE ; Invalid TSS
  72. MakeDescriptor  I0B,Int_Code_Selector,0,TRAP_GATE ; Cs,Ds,Es Not Present
  73. MakeDescriptor  I0C,Int_Code_Selector,0,TRAP_GATE ; Ss not present
  74. MakeDescriptor  I0D,Int_Code_Selector,0,TRAP_GATE ; Gen Prot Violation
  75. MakeDescriptor  0,0,0,0                           ; Reserved
  76. MakeDescriptor  0,0,0,0                           ; Reserved
  77. MakeDescriptor  I10,Int_Code_Selector,0,TRAP_GATE ; Calculation error x287
  78.                 Db  (32-17)*8 Dup(0)              ; 17-31 reserved/unused
  79. MakeDescriptor  I20,Int_Code_Selector,0,INT_GATE  ; Int 20h
  80. MakeDescriptor  I21,Int_Code_Selector,0,INT_GATE  ; Int 21h
  81. MakeDescriptor  I22,Int_Code_Selector,0,INT_GATE  ; Int 22h
  82. MakeDescriptor  I23,Int_Code_Selector,0,INT_GATE  ; Int 23h
  83. MakeDescriptor  I24,Int_Code_Selector,0,INT_GATE  ; Int 24h
  84. MakeDescriptor  I24,Int_Code_Selector,0,INT_GATE  ; Int 25h
  85. MakeDescriptor  I26,Int_Code_Selector,0,INT_GATE  ; Int 26h
  86. MakeDescriptor  I27,Int_Code_Selector,0,INT_GATE  ; Int 27h
  87.                 Db  (64-40)*8 Dup(0)              ;
  88. Idt_End         LABEL   BYTE
  89. IDT             Ends
  90.  
  91.  
  92. Int_Seg         Segment
  93. ;=============================================================================;
  94. ; This is where all the interruptts go to!!                                   ;
  95. ;=============================================================================;
  96.                 Assume  Cs:Int_Seg
  97.  
  98. I00:
  99.                 Mov     Ax,Es_Selector          ;
  100.                 Mov     Es,Ax                   ; Make the screen acessable
  101.                 Mov     Al,'0'                  ; Int  number calling
  102.                 Mov     Ah,7                    ;
  103.                 Mov     Di,0                    ;
  104.                 Cld                             ;
  105.                 Stosw                           ;
  106.                 Iret                            ; Back!!
  107.  
  108. I01:
  109.                 Mov     Ax,Es_Selector          ;
  110.                 Mov     Es,Ax                   ; Make the screen acessable
  111.                 Mov     Al,'1'                  ; Int  number calling
  112.                 Mov     Ah,7                    ;
  113.                 Mov     Di,0                    ;
  114.                 Cld                             ;
  115.                 Stosw                           ;
  116.                 Iret                            ; Back!!
  117.  
  118. I02:
  119.                 Mov     Ax,Es_Selector          ;
  120.                 Mov     Es,Ax                   ; Make the screen acessable
  121.                 Mov     Al,'2'                  ; Int  number calling
  122.                 Mov     Ah,7                    ;
  123.                 Mov     Di,0                    ;
  124.                 Cld                             ;
  125.                 Stosw                           ;
  126.                 Iret                            ; Back!!
  127.  
  128. I03:
  129.                 Mov     Ax,Es_Selector          ;
  130.                 Mov     Es,Ax                   ; Make the screen acessable
  131.                 Mov     Al,'3'                  ; Int  number calling
  132.                 Mov     Ah,7                    ;
  133.                 Mov     Di,0                    ;
  134.                 Cld                             ;
  135.                 Stosw                           ;
  136.                 Iret                            ; Back!!
  137.  
  138. I04:
  139.                 Mov     Ax,Es_Selector          ;
  140.                 Mov     Es,Ax                   ; Make the screen acessable
  141.                 Mov     Al,'4'                  ; Int  number calling
  142.                 Mov     Ah,7                    ;
  143.                 Mov     Di,0                    ;
  144.                 Cld                             ;
  145.                 Stosw                           ;
  146.                 Iret                            ; Back!!
  147.  
  148. I05:
  149.                 Mov     Ax,Es_Selector          ;
  150.                 Mov     Es,Ax                   ; Make the screen acessable
  151.                 Mov     Al,'5'                  ; Int  number calling
  152.                 Mov     Ah,7                    ;
  153.                 Mov     Di,0                    ;
  154.                 Cld                             ;
  155.                 Stosw                           ;
  156.                 Iret                            ; Back!!
  157.  
  158. I06:
  159.                 Mov     Ax,Es_Selector          ;
  160.                 Mov     Es,Ax                   ; Make the screen acessable
  161.                 Mov     Al,'6'                  ; Int  number calling
  162.                 Mov     Ah,7                    ;
  163.                 Mov     Di,0                    ;
  164.                 Cld                             ;
  165.                 Stosw                           ;
  166.                 Iret                            ; Back!!
  167.  
  168. I07:
  169.                 Mov     Ax,Es_Selector          ;
  170.                 Mov     Es,Ax                   ; Make the screen acessable
  171.                 Mov     Al,'7'                  ; Int  number calling
  172.                 Mov     Ah,7                    ;
  173.                 Mov     Di,0                    ;
  174.                 Cld                             ;
  175.                 Stosw                           ;
  176.                 Iret                            ; Back!!
  177.  
  178. I08:
  179.                 Mov     Ax,Es_Selector          ;
  180.                 Mov     Es,Ax                   ; Make the screen acessable
  181.                 Mov     Al,'8'                  ; Int  number calling
  182.                 Mov     Ah,7                    ;
  183.                 Mov     Di,0                    ;
  184.                 Cld                             ;
  185.                 Stosw                           ;
  186.                 Iret                            ; Back!!
  187.  
  188. I09:
  189.                 Mov     Ax,Es_Selector          ;
  190.                 Mov     Es,Ax                   ; Make the screen acessable
  191.                 Mov     Al,'9'                  ; Int  number calling
  192.                 Mov     Ah,7                    ;
  193.                 Mov     Di,0                    ;
  194.                 Cld                             ;
  195.                 Stosw                           ;
  196.                 Iret                            ; Back!!
  197.  
  198. I0A:
  199.                 Mov     Ax,Es_Selector          ;
  200.                 Mov     Es,Ax                   ; Make the screen acessable
  201.                 Mov     Al,'A'                  ; Int  number calling
  202.                 Mov     Ah,7                    ;
  203.                 Mov     Di,0                    ;
  204.                 Cld                             ;
  205.                 Stosw                           ;
  206.                 Iret                            ; Back!!
  207.  
  208. I0B:
  209.                 Mov     Ax,Es_Selector          ;
  210.                 Mov     Es,Ax                   ; Make the screen acessable
  211.                 Mov     Al,'B'                  ; Int  number calling
  212.                 Mov     Ah,7                    ;
  213.                 Mov     Di,0                    ;
  214.                 Cld                             ;
  215.                 Stosw                           ;
  216.                 Iret                            ; Back!!
  217.  
  218. I0C:
  219.                 Mov     Ax,Es_Selector          ;
  220.                 Mov     Es,Ax                   ; Make the screen acessable
  221.                 Mov     Al,'C'                  ; Int  number calling
  222.                 Mov     Ah,7                    ;
  223.                 Mov     Di,0                    ;
  224.                 Cld                             ;
  225.                 Stosw                           ;
  226.                 Iret                            ; Back!!
  227.  
  228. I0D:
  229.                 Mov     Ax,Es_Selector          ;
  230.                 Mov     Es,Ax                   ; Make the screen acessable
  231.                 Mov     Al,'D'                  ; Int  number calling
  232.                 Mov     Ah,7                    ;
  233.                 Mov     Di,0                    ;
  234.                 Cld                             ;
  235.                 Stosw                           ;
  236.                 Iret                            ; Back!!
  237.  
  238. I10:
  239.                 Mov     Ax,Es_Selector          ;
  240.                 Mov     Es,Ax                   ; Make the screen acessable
  241.                 Mov     Al,'Q'                  ; Int  number calling
  242.                 Mov     Ah,7                    ;
  243.                 Mov     Di,0                    ;
  244.                 Cld                             ;
  245.                 Stosw                           ;
  246.                 Iret                            ; Back!!
  247.  
  248. I20:
  249.                 Mov     Ax,Es_Selector          ;
  250.                 Mov     Es,Ax                   ; Make the screen acessable
  251.                 Mov     Al,'R'                  ; Int  number calling
  252.                 Mov     Ah,7                    ;
  253.                 Mov     Di,0                    ;
  254.                 Cld                             ;
  255.                 Stosw                           ;
  256.                 Iret                            ; Back!!
  257.  
  258. I21:
  259.                 Mov     Ax,Es_Selector          ;
  260.                 Mov     Es,Ax                   ; Make the screen acessable
  261.                 Mov     Al,'S'                  ; Int  number calling
  262.                 Mov     Ah,7                    ;
  263.                 Mov     Di,0                    ;
  264.                 Cld                             ;
  265.                 Stosw                           ;
  266.                 Iret                            ; Back!!
  267.  
  268. I22:
  269.                 Mov     Ax,Es_Selector          ;
  270.                 Mov     Es,Ax                   ; Make the screen acessable
  271.                 Mov     Al,'T'                  ; Int  number calling
  272.                 Mov     Ah,7                    ;
  273.                 Mov     Di,0                    ;
  274.                 Cld                             ;
  275.                 Stosw                           ;
  276.                 Iret                            ; Back!!
  277.  
  278. I23:
  279.                 Mov     Ax,Es_Selector          ;
  280.                 Mov     Es,Ax                   ; Make the screen acessable
  281.                 Mov     Al,'V'                  ; Int  number calling
  282.                 Mov     Ah,7                    ;
  283.                 Mov     Di,0                    ;
  284.                 Cld                             ;
  285.                 Stosw                           ;
  286.                 Iret                            ; Back!!
  287.  
  288. I24:
  289.                 Mov     Ax,Es_Selector          ;
  290.                 Mov     Es,Ax                   ; Make the screen acessable
  291.                 Mov     Al,'W'                  ; Int  number calling
  292.                 Mov     Ah,7                    ;
  293.                 Mov     Di,0                    ;
  294.                 Cld                             ;
  295.                 Stosw                           ;
  296.                 Iret                            ; Back!!
  297.  
  298. I25:
  299.                 Mov     Ax,Es_Selector          ;
  300.                 Mov     Es,Ax                   ; Make the screen acessable
  301.                 Mov     Al,'X'                  ; Int  number calling
  302.                 Mov     Ah,7                    ;
  303.                 Mov     Di,0                    ;
  304.                 Cld                             ;
  305.                 Stosw                           ;
  306.                 Iret                            ; Back!!
  307.  
  308. I26:
  309.                 Mov     Ax,Es_Selector          ;
  310.                 Mov     Es,Ax                   ; Make the screen acessable
  311.                 Mov     Al,'Y'                  ; Int  number calling
  312.                 Mov     Ah,7                    ;
  313.                 Mov     Di,0                    ;
  314.                 Cld                             ;
  315.                 Stosw                           ;
  316.                 Iret                            ; Back!!
  317.  
  318. I27:
  319.                 Mov     Ax,Es_Selector          ;
  320.                 Mov     Es,Ax                   ; Make the screen acessable
  321.                 Mov     Al,'Z'                  ; Int  number calling
  322.                 Mov     Ah,7                    ;
  323.                 Mov     Di,0                    ;
  324.                 Cld                             ;
  325.                 Stosw                           ;
  326.                 Iret                            ; Back!!
  327.  
  328. End_Of_Ints     EQU $
  329. Int_Seg         Ends
  330.  
  331.  
  332. Run_Code        Segment PARA    PUBLIC  'EXEC'
  333. ;=============================================================================;
  334. ; This is where control is transfered to in Prot. Mode.                       ;
  335. ;=============================================================================;
  336.                     Assume  Cs:Run_Code
  337.  
  338. Msg1        db      'Have entered protected mode',0
  339.  
  340. Initialize:         Mov     Ax,GDT              ; Get segment of GDT table
  341.                     Mov     Es,Ax               ; ES Contains the GDT
  342.                     Mov     Ds,Ax               ;
  343.                     Call    Make_24bit          ; Do it
  344.                     Mov     Di,Gdt_Alias_Selector      ; Get the selector
  345.                     Mov     BaseW[Di],Dx        ; Base segmemnt of GDT
  346.                     Mov     BaseB[Di],Al        ;
  347.                     Mov     Ax,IDT              ; The IDT setup
  348.                     Call    Make_24bit          ;
  349.                     Mov     Di,Idt_Alias_Selector       ; Get it
  350.                     Mov     BaseW[Di],Dx        ;
  351.                     Mov     BaseB[Di],Al        ;
  352.                     Mov     Ax,Cs               ; Get the Thing here
  353.                     Call    Make_24bit          ;
  354.                     Mov     Di,Main_Code_Selector       ; Get this
  355.                     Mov     BaseW[Di],Dx        ;
  356.                     Mov     BaseB[Di],Al        ;
  357.                     Lea     Si,Main_Code_End    ;
  358.                     Mov     Lim[Di],Si          ; Limit on main code
  359.                     Mov     Di,Es_Selector      ;
  360.                     Mov     BaseW[Di],Dx        ;
  361.                     Mov     BaseB[Di],Al        ;
  362.                     Mov     Lim[Di],Si          ;
  363.                     Mov     Di,Ds_Selector      ;
  364.                     Mov     BaseW[Di],Dx        ;
  365.                     Mov     BaseB[Di],Al        ;
  366.                     Mov     Lim[Di],Si          ;
  367.                     Mov     Ax,StackSeg         ;
  368.                     Call    Make_24bit          ;
  369.                     Mov     Di,Ss_Selector      ;
  370.                     Mov     BaseW[Di],Dx        ;
  371.                     Mov     BaseB[Di],Al        ;
  372.                     Lea     Si,TOS              ;
  373.                     Mov     Lim[Di],Si          ;
  374.                     Mov     Ax,Int_Seg          ; Get the interrupt segment
  375.                     Call    Make_24bit          ; Do it
  376.                     Mov     Di,Int_Code_Selector        ;
  377.                     Mov     BaseW[Di],Dx        ;
  378.                     Mov     BaseB[Di],Al        ;
  379.                     Lea     Si,End_Of_Ints      ;
  380.                     Mov     Lim[Di],Si          ;
  381.                                                 ;
  382.                     Mov     Ah,89h              ; Call to do it
  383.                     Mov     Al,0                ;
  384.                     Mov     Bx,40h              ;
  385.                     Xor     Si,Si               ;
  386.                     Int     15h                 ; By By!!
  387.  
  388. Main_Program:       Sti                         ;
  389.                     Mov     Di,0                ;
  390.                     Lea     Si,Msg1             ;
  391.                     Cld                         ;
  392.                     Mov     Ah,07h              ;
  393. Tl1:                Lodsb                       ;
  394.                     Cmp     Al,0                ;
  395.                     Je      Done                ;
  396.                     Stosw                       ;
  397.                     Jmp     Tl1                 ;
  398. Done:                                           ;
  399.                                                 ;
  400. Forever:            Jmp     Forever             ; Loop Forever
  401.  
  402.  
  403. Make_24bit          PROC    near
  404. ;-----------------------------------------------------------------------------;
  405. ; Makes a 24 bit address retrned in DX (The word) and Al (The byte)           ;
  406. ; Receives a 16 bit num,ber in AX to convert!.                                ;
  407. ;-----------------------------------------------------------------------------;
  408.                     Push    Bx                  ;
  409.                     Push    Cx                  ;
  410.                     Mov     Dx,Ax               ;
  411.                     Mov     Bx,0F000h           ; The Mask
  412.                     Mov     Cl,4                ; The shift
  413.                     And     Ax,Bx               ; The upper 4 bits here
  414.                     Not     Bx                  ; Flip
  415.                     And     Dx,Bx               ; The lower here
  416.                     Rol     Ax,Cl               ; Rotate (Upper byte)
  417.                     Shl     Dx,Cl               ; This (Lower word)
  418.                     Pop     Cx                  ;
  419.                     Pop     Bx                  ;
  420.                     Ret
  421. Make_24bit          Endp
  422.  
  423.  
  424. InitialEntry:       Jmp     Initialize          ; Go initialize things
  425. Main_Code_End       EQU     $
  426. Run_Code            Ends
  427.  
  428.  
  429.  
  430. StackSeg            Segment PARA    STACK   'STACK'
  431.                     Db  1024    DUP(0)
  432. TOS EQU $
  433. StackSeg            ENDS
  434.  
  435.  
  436.                 End     InitialEntry
  437.