home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progasm / whatcpu.arj / WHATCPU.DOC < prev    next >
Encoding:
Text File  |  1990-07-13  |  4.4 KB  |  128 lines

  1. WHATCPU
  2. =======
  3.  
  4. From PC Magazine, July 1990, pp425-426
  5. --------------------------------------
  6.  
  7.    You can differentiate between protected-mode CPUs (286, 386,
  8. 486 and 386SX) and real-mode CPUs (8088, 8086, 80188, 80186 and
  9. the NEC V20 and V30) with a simple test that determines whether
  10. SP is decremented before or after a PUSH.  Real-mode CPUs
  11. decrement SP before a value is PUSHed; protected-mode CPUs
  12. decrement it after they're running in real-mode.  (The terms
  13. real-mode and protected-mode are used here only to distinguish
  14. between chip families.)  The simple code sequence
  15.  
  16.         PUSH    SP
  17.         POP     AX
  18.         CMP     SP,AX
  19.         JNE     IS_8086
  20.  
  21. separates 286/386/486-class chips from 8086-class chips.  If a
  22. branch occurs, then the CPU belongs to the 8086 family; if it
  23. doesn't, the CPU is a 286,386 or 486.
  24.  
  25.    Once you've gotten this far, another test is required to
  26. distinguish 286s from 386s and 486s.  Here's the key: the NT
  27. (Nested Task) bit (bit 14) in the flags register can't be set in
  28. real mode on a 286, but it can on a 386 or 486.  If the test
  29.  
  30.         PUSHP
  31.         POP     AX
  32.         OR      AX,4000
  33.         PUSH    AX
  34.         POPF
  35.         PUSHP
  36.         POP     AX
  37.         TEST    AX,4000
  38.         JZ      IS_286
  39.  
  40. branches on the last line, then the CPU is a 286.  If not, it's
  41. 386 or a 486.
  42.  
  43.    Differentiating between a 386 and a 486 requires one more
  44. step.  If you're writing 386-specific software, you won't need
  45. this, because anything that will run on a 386 will also run on a
  46. 486.  But soon we'll see 486-specific software as well.
  47.  
  48.    The following code sequence, which tests the CPU's ability to
  49. retain data in register CR0's cache write-through bit (bit 29),
  50. separates 386s from 486s.
  51.  
  52.         MOV     EAX,CR0
  53.         MOV     EBX,EAX
  54.         XOR     EAX,20000000H
  55.         MOV     CR0,EAX
  56.         MOV     EAX,CR0
  57.         CMP     EAX,EBX
  58.         JE      IS_386
  59.  
  60. The cache write-through bit isn't implemented on the 386 and thus
  61. can't be written to: on the 486, where it is implemented, it can.
  62.  
  63.    A branch in response to the JE (jump-if-equal) instruction on
  64. the last line means the CPU is a 386; if the execution falls
  65. through, then it's a 486.  If you use this code sequence with
  66. MASM, remember to include a .386P directive in your source code
  67. to enable assembly of privileged 386 instructions.
  68.  
  69.    The following DEBUG listing (WHATCPU.SCR) creates a short
  70. program called WHATCPU.COM:
  71.  
  72.         A 0100
  73.         JMP     0123
  74.         DB      0D,0A,"CPU is an 80$"
  75.         DB      "86$"
  76.         DB      "286$"
  77.         DB      "386$"
  78.         DB      "486$"
  79.         DB      0D,0A,"$"
  80.         MOV     AH,09                   ;Print opening message
  81.         MOV     DX,0102
  82.         INT     21
  83.         MOV     DX,0111                 ;Test for 8088/8086
  84.         PUSH    SP
  85.         POP     AX
  86.         CMP     SP,AX
  87.         JNZ     0166                    ;Branch if test positive
  88.         MOV     DX,0114                 ;Test for 286
  89.         PUSHF
  90.         POP     AX
  91.         OR      AX,4000
  92.         PUSH    AX
  93.         POPF
  94.         PUSHF
  95.         POP     AX
  96.         TEST    AX,4000
  97.         JZ      0166                    ;Branch if test positive
  98.         MOV     DX,0118                 ;Separate 386s from 486s
  99.         DB      0F,20,C0                ;mov eax,cr0
  100.         DB      66,8B,D8                ;mov ebx,eax
  101.         DB      66,35,00,00,00,20       ;xor eax,20000000
  102.         DB      0F,22,C0                ;mov cr0,eax
  103.         DB      0F,20,C0                ;mov eax,cr0
  104.         DB      66,3B,C3                ;cmp eax,ebx
  105.         DB      0F,84,06,00             ;je  0166
  106.         DB      BA,1C,01                ;mov dx,011C
  107.         DB      0F,22,C3                ;mov cr0,ebx
  108.         MOV     AH,09                   ;Print CPU type
  109.         INT     21
  110.         MOV     AH,09                   ;Terminate line and exit
  111.         MOV     DX,0120
  112.         INT     21
  113.         RET
  114.  
  115.         N WHATCPU.COM
  116.         RCX
  117.         72
  118.         W
  119.         Q
  120.  
  121. This identifies a CPU as an 8086, 286, 386 or 486.  An 8088,
  122. 8086, 80188, 80186, V20 or V30 is generically identified as an
  123. 8086.  Since DEBUG won't assemble the 386 instructions necessary
  124. to distinguish between the 386 and the 486, 386-specific
  125. instructions are entered manually, using DEBUG's DB command.
  126. Alsom don't type in the comments on the right; doing so will
  127. cause errors on lines that begin with DB.
  128.