User:Johnburger/Demo/x86/x86

From OSDev Wiki
Jump to: navigation, search

The following is a large number of definitions, all for the '386. There's no code for any of this: it's all just definitions. The designers of the '386 have burned silicon that strictly defines its operation - indeed, you could almost consider it the DNA of the processor.

As well as defining mere constants, though, an even better practice that I have followed is to define %macros - and use them. For example: in Real Mode, a Segment Register holds a value that is 4 right-shifts from the memory that it references. For any code that loads a Segment Register, it is (relatively) obvious what it's going to do (you have defined labels for the Segment values for Interrupt Vector Table, BIOS Data Area, and Screen, haven't you?)

But in Protected Mode, a Segment Register holds a selector to a Segment Descriptor that is comprised of three parts - and it is inordinately easy to forget to load all the parts, causing a General Protection Fault, or worse!

       1111110000000 0 00
       FEDCBA9876543 2 10
       ^^^^^^^^^^^^^ ^ ^^
       ||||||||||||| | `+-- Requested Privilege Level: (00-03)
       ||||||||||||| `----- Table Indicator:            0=GDT, 1=LDT
       `++++++++++++------- Actual selector into Indicated Descriptor Table

To save myself lots of grief (can you tell I learned this the hard way?), I defined a %macro called Selector with all three parameters:

             MOV     AX,Selector(Index,LDT,RPL3)

That way the assembler can warn me that I've forgotten one of them - now all I have to do is:

  1. Remember to use it (easy!); and
  2. Think when I use it, that I have every parameter correct!

Demo/x86/x86.inc

;
; x86/x86.inc
;
 
; The following are definitions for the 80386 and later CPUs.
;
; Note that they're defined by Intel as part of the 'DNA' of the chip: almost
; literally, if you think of the silicon as defining the CPU's operation.
 
;-------------------------------------------------------------------------------
%include        "x86/EFlags.inc"        ; Definitions for EFlags register
;-------------------------------------------------------------------------------
%include        "x86/Seg.inc"           ; Definitions for Segment register
;-------------------------------------------------------------------------------
%include        "x86/CR0.inc"           ; Definitions for CR0 register
;-------------------------------------------------------------------------------
%include        "x86/Desc.inc"          ; Definitions for Descriptors
;-------------------------------------------------------------------------------
%include        "x86/TSS.inc"           ; Definitions for Task State Segment
;-------------------------------------------------------------------------------
%include        "x86/IVT.inc"           ; Definitions for Interrupt Vector Table
Personal tools
Namespaces
Variants
Actions
Navigation
About
Toolbox