removed clang-format and clang_complete files from the repo
[dosdemo] / src / cpuid_s.asm
1         section .text
2         bits 32
3 ; foo_ are watcom functions, _foo are djgpp functions
4
5 F_ID equ 0x200000
6
7         global read_cpuid
8         global _read_cpuid
9         global read_cpuid_
10 read_cpuid_:
11         push eax
12         call check_cpuid
13         pop eax
14         jnc read_cpuid_nocheck
15         mov eax, -1
16         ret
17
18 _read_cpuid:
19 read_cpuid:
20         call check_cpuid
21         mov eax, [esp + 4]
22         jnc read_cpuid_nocheck
23         mov eax, -1
24         ret
25
26         ; determine if cpuid is available. avail: cf=0, not avail: cf=1
27 check_cpuid:
28         pushf
29         pop eax
30         mov edx, eax    ; keep a copy of the original eflags in edx
31         xor eax, F_ID
32         push eax
33         popf
34         pushf
35         pop eax
36         clc
37         cmp eax, edx
38         jnz .noerr
39         stc
40 .noerr: ret
41
42         ; enter with the cpuid_info structure pointer in eax
43 read_cpuid_nocheck:
44         push ebp
45         mov ebp, esp
46         push ebx
47         push edi
48         push esi
49         push eax        ; save the original struct pointer
50         sub esp, 8
51         mov edi, eax    ; struct pointer -> edi
52
53         ; clear struct
54         cld
55         push edi
56         mov ecx, (32+48)/4
57         xor eax, eax
58         rep stosd
59         pop edi
60
61         xor eax, eax
62         mov [esp], eax  ; current index
63         cpuid
64
65         mov [edi], eax          ; maxidx
66         ; clamp to the size of our cpuid_info structure
67         cmp eax, 1
68         jbe .skipclamp
69         mov eax, 1
70 .skipclamp:
71         mov [esp + 4], eax      ; maximum index
72
73         mov [edi + 4], ebx      ; vendor name
74         mov [edi + 8], edx
75         mov [edi + 12], ecx
76         add edi, 16
77
78 .loop:  mov eax, [esp]
79         inc eax
80         cmp eax, [esp + 4]
81         ja .loopend
82         mov [esp], eax
83         cpuid
84         mov [edi], eax
85         mov [edi + 4], ebx
86         mov [edi + 8], edx
87         mov [edi + 12], ecx
88         add edi, 16
89         jmp .loop
90 .loopend:
91         ; try to retrieve the brand string (avail on P4 or newer)
92         mov eax, 80000000h
93         cpuid
94         test eax, 80000000h
95         jz .done        ; no extended cpuid functions
96         cmp eax, 80000004h
97         jb .done        ; no brand string available
98
99         ; brand string available
100         mov esi, esp            ; save esp to esi
101         mov esp, [esp + 8]      ; esp <- original struct pointer
102         add esp, 32+48          ; offset to end of brandstr
103         mov eax, 80000004h
104         cpuid
105         push edx
106         push ecx
107         push ebx
108         push eax
109         mov eax, 80000003h
110         cpuid
111         push edx
112         push ecx
113         push ebx
114         push eax
115         mov eax, 80000002h
116         cpuid
117         push edx
118         push ecx
119         push ebx
120         push eax
121         mov esp, esi    ; done restore esp
122
123 .done:  add esp, 8
124         pop eax
125         pop esi
126         pop edi
127         pop ebx
128         pop ebp
129         xor eax, eax
130         ret
131         
132 ; vi:ft=nasm: