[comp.os.msdos.programmer] Find CPU type

arasmith@mathcs.emory.edu (David Arasmith) (10/05/90)

I have a fairly pressing need to learn how to determine CPU type from
inside a C program.  I can look at the equipment list etc. to find all
kinds of other hardware in the box but I have NOT been able to find a
good (or not so good) method for CPU detection.

I would greatly appreciate some assistance: pointers, code fragments,
amusing anecdotes.

Email or posting, whatever you feel like (I will get to email faster).

Thanks
-- 
David M. Arasmith   |  arasmith@mathcs.emory.edu	        Internet
Emory University    |  {sun!sunatl,gatech}!emory!arasmith	UUCP
Dept of Math and CS |  
Atlanta, GA 30322   |  I should be working!  Gee....I wonder what's on TV?

cjdb@ellis.uchicago.edu (Charles Blair) (10/17/90)

In article <6367@emory.mathcs.emory.edu> arasmith@mathcs.emory.edu (David Arasmith) writes:
>
>I have a fairly pressing need to learn how to determine CPU type from
>inside a C program.  I can look at the equipment list etc. to find all
>kinds of other hardware in the box but I have NOT been able to find a
>good (or not so good) method for CPU detection.
>
>I would greatly appreciate some assistance: pointers, code fragments,
>amusing anecdotes.
>
>Email or posting, whatever you feel like (I will get to email faster).

I'll post an assembly language routine that does this, since I'd like
to see an update (this doesn't detect the 486), and since Borland and
Microsoft now support inline assembly language (i.e., this might be
useful in a C program as well). (I notice that some of the jmp's were
better written as jmp short's.)

; GET_CPU - Proc to determine which 80(x)86/8 is installed in a machine.
;	Modified from PROC_TYPE, written by Clif Purkiser, Intel, Santa Clara,
;	Ca. From the original comment: "The attached code sequence returns 
;	the processor type in the AX register.  It should be distributed to any
;	customers and Independent Software vendors who could use it." Also:
;	"Note this test works only in Real Mode." The code was captured 
;	electronically from a posting to Usenet.

get_cpu	proc		near
;
;	Returns the processor type in AX 
;

	pushf				; Save FLAG registers
	xor		ax,ax		; Clear AX and push onto the stack.
	push		ax
	popf				;  Pop a zero into FLAGs register
	pushf				;  Attempt to set bit 12-15 to a zero
	pop		ax		;  Recover FLAG word
	and		ax, 0f000h	;  If Bits 12-15 are then the processor
	cmp		ax, 0f000h	;  is an 8018x or an 808x
	jz		gc_is_0_1		
 
	mov		ax, 07000h     ;  Try to set FLAG bits 12-14 (NT, IOPL)
	push 		ax
	popf				;  put 07000H into flags
	pushf
	pop		ax

	and		ax,07000h	;  if bits 12-14 are cleared then the
	jz		gc_is_80286	;	processor is an 286

gc_is_80386:				;	Else it is a 386 
	mov		ax, 386h	;	return 386 in AX
	jmp		gc_done
gc_is_80286:
	mov		ax, 286h	;  return 286 in AX
	jmp		gc_done
gc_is_0_1:				;  It is a 8086 or a 80186
	push 		cx		;  Save CX the only other register used
	mov		ax, 0ffffh	;  Set AX to all 1s
	mov		cl, 33		;  will shift it 33 times if it is an
					;  808x or 1 time if it is an 8018x
	shl		ax, cl		;  if we shift 33 times all bits are 
	jnz		gc_is_80186	;  zero. If any bits are on it's an 18x
gc_is_8086:				;  Else we have an 8086
	mov		ax,86h		;  Return 86 in AX
	pop		cx		;  restore cx
	jmp		gc_done	
	
gc_is_80186:
	mov 		ax, 186h	;  Return 186 in AX
	pop		cx			

gc_done:
	mov		cpu_type,ax	; Store CPU type
	popf				; Recover original FLAGs register
	ret

get_cpu	endp

--
Bitnet:                 pmrcjdb@uchimvs1
Internet:       cjdb@midway.uchicago.edu