Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fernandomalmeida/dac98a3b674fb073374f to your computer and use it in GitHub Desktop.
Save fernandomalmeida/dac98a3b674fb073374f to your computer and use it in GitHub Desktop.
Explorando as Feature Flags da instrução CPUID
/*
* Trabalho prático II
* Data da entrega: 27/05/2014
* Conteúdo:
* Instrução CPUID, explorar featured flag (tabela 5)
* Formato:
* - Apresentação com descrição e funcionalidade da instrução
* - Implementação de código com comentários
* Referência básica:
* http://datasheets.chipdb.org/Intel/x86/CPUID/24161821.pdf
*/
#include <stdio.h>
typedef struct{
unsigned int FPU :1; // bit 0
unsigned int VME :1; // bit 1
unsigned int DE :1; // bit 2
unsigned int PSE :1; // bit 3
unsigned int TSC :1; // bit 4
unsigned int MSR :1; // bit 5
unsigned int PAE :1; // bit 6
unsigned int MCE :1; // bit 7
unsigned int CX8 :1; // bit 8
unsigned int APIC :1; // bit 9
unsigned int :1; // bit 10
unsigned int SEP :1; // bit 11
unsigned int MTRR :1; // bit 12
unsigned int PGE :1; // bit 13
unsigned int MCA :1; // bit 14
unsigned int CMOV :1; // bit 15
unsigned int PAT :1; // bit 16
unsigned int PSE_36 :1; // bit 17
unsigned int PSN :1; // bit 18
unsigned int CLFSH :1; // bit 19
unsigned int :1; // bit 20
unsigned int DS :1; // bit 21
unsigned int ACPI :1; // bit 22
unsigned int MMX :1; // bit 23
unsigned int FXSR :1; // bit 24
unsigned int SSE :1; // bit 25
unsigned int SSE2 :1; // bit 26
unsigned int SS :1; // bit 27
unsigned int HTT :1; // bit 28
unsigned int TM :1; // bit 29
unsigned int :2; // bit 30-31
} CPUINFO;
typedef union{
CPUINFO cpu;
unsigned int i;
} CPUINFO_union;
int aux;
int main(){
CPUINFO_union unionCPU;
asm(".intel_syntax noprefix");
asm("mov %eax, 1");
asm("cpuid");
asm("mov aux, %edx");
asm(".att_syntax noprefix");
unionCPU.i = aux;
printf("%#010x\n", unionCPU.i);
CPUINFO cpu = unionCPU.cpu;
printf("MCE: %d\n", cpu.MCE);
printf("PAE: %d\n", cpu.PAE);
printf("MSR: %d\n", cpu.MSR);
printf("TSC: %d\n", cpu.TSC);
printf("PSE: %d\n", cpu.PSE);
printf("DE: %d\n", cpu.DE);
printf("VME: %d\n", cpu.VME);
printf("FPU: %d\n", cpu.FPU);
printf("CMOV: %d\n", cpu.CMOV);
printf("MCA: %d\n", cpu.MCA);
printf("PGE: %d\n", cpu.PGE);
printf("MTRR: %d\n", cpu.MTRR);
printf("SEP: %d\n", cpu.SEP);
printf("APIC: %d\n", cpu.APIC);
printf("CX8: %d\n", cpu.CX8);
printf("MMX: %d\n", cpu.MMX);
printf("ACPI: %d\n", cpu.ACPI);
printf("DS: %d\n", cpu.DS);
printf("CLFSH: %d\n", cpu.CLFSH);
printf("PSN: %d\n", cpu.PSN);
printf("PSE_36: %d\n", cpu.PSE_36);
printf("PAT: %d\n", cpu.PAT);
printf("TM: %d\n", cpu.TM);
printf("HTT: %d\n", cpu.HTT);
printf("SS: %d\n", cpu.SS);
printf("SSE2: %d\n", cpu.SSE2);
printf("SSE: %d\n", cpu.SSE);
printf("FXSR: %d\n", cpu.FXSR);
return 0;
}
/*
* Trabalho prático II
* Data da entrega: 27/05/2014
* Conteúdo:
* Instrução CPUID, explorar featured flag (tabela 5)
* Formato:
* - Apresentação com descrição e funcionalidade da instrução
* - Implementação de código com comentários
* Referência básica:
* http://datasheets.chipdb.org/Intel/x86/CPUID/24161821.pdf
*/
#include <stdio.h>
int _FPU; // bit 0
int _VME; // bit 1
int _DE; // bit 2
int _PSE; // bit 3
int _TSC; // bit 4
int _MSR; // bit 5
int _PAE; // bit 6
int _MCE; // bit 7
int _CX8; // bit 8
int _APIC; // bit 9
int _SEP; // bit 11
int _MTRR; // bit 12
int _PGE; // bit 13
int _MCA; // bit 14
int _CMOV; // bit 15
int _PAT; // bit 16
int _PSE36; // bit 17
int _PSN; // bit 18
int _CLFSH; // bit 19
int _DS; // bit 21
int _ACPI; // bit 22
int _MMX; // bit 23
int _FXSR; // bit 24
int _SSE; // bit 25
int _SSE2; // bit 26
int _SS; // bit 27
int _HTT; // bit 28
int _TM; // bit 29
int aux;
int main(){
asm(".intel_syntax noprefix");
asm("mov %eax, 1");
asm("cpuid");
asm("mov %eax, 1"); // mascara de 1 bit
asm("mov %ebx, %edx"); // passa o valor de edx para ebx
asm("mov aux, %edx");
asm("and %eax, %ebx"); //verifica o bit 0
asm("mov _FPU, %eax"); // guarda o bit 0 em FPU
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 1 em eax
asm("mov _VME, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 2 em eax
asm("mov _DE, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 3 em eax
asm("mov _PSE, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 4 em eax
asm("mov _TSC, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 5 em eax
asm("mov _MSR, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 6 em eax
asm("mov _PAE, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 7 em eax
asm("mov _MCE, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 8 em eax
asm("mov _CX8, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 9 em eax
asm("mov _APIC, %eax");
asm("shr %ebx, 1"); // (pula o bit reservado 10)
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 11 em eax
asm("mov _SEP, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 12 em eax
asm("mov _MTRR, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 13 em eax
asm("mov _PGE, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 14 em eax
asm("mov _MCA, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 15 em eax
asm("mov _CMOV, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 16 em eax
asm("mov _PAT, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 17 em eax
asm("mov _PSE36, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 18 em eax
asm("mov _PSN, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 19 em eax
asm("mov _CLFSH, %eax");
asm("shr %ebx, 1"); // (pula o bit reservado 20)
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 21 em eax
asm("mov _DS, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 22 em eax
asm("mov _ACPI, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 23 em eax
asm("mov _MMX, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 24 em eax
asm("mov _FXSR, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 25 em eax
asm("mov _SSE, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 26 em eax
asm("mov _SSE2, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 27 em eax
asm("mov _SS, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 28 em eax
asm("mov _HTT, %eax");
asm("mov %eax, 1"); // recupera a mascara de 1 bit
asm("shr %ebx, 1"); // move o bit a ser recuperado
asm("and %eax, %ebx"); // aplica a máscara e guarda o valor do bit 29 em eax
asm("mov _TM, %eax");
asm(".att_syntax noprefix");
printf("%#010x\n", aux);
printf("Floating-point unit on-Chip: %ssupported\n", (_FPU)?(""):("not "));
printf("Virtual Mode Extension: %ssupported\n", (_VME)?(""):("not "));
printf("Debugging Extension: %ssupported\n", (_DE)?(""):("not "));
printf("Page Size Extension: %ssupported\n", (_PSE)?(""):("not "));
printf("Time Stamp Counter: %ssupported\n", (_TSC)?(""):("not "));
printf("Model Specific Registers: %ssupported\n", (_MSR)?(""):("not "));
printf("Pysical Address Extension: %ssupported\n", (_PAE)?(""):("not "));
printf("Machine Check Exception: %ssupported\n", (_MCE)?(""):("not "));
printf("CMPXCHG8 Instruction Supported: %ssupported\n", (_CX8)?(""):("not "));
printf("On-chip APIC Hardware Supported: %ssupported\n", (_APIC)?(""):("not "));
printf("Fast System Call: %ssupported\n", (_SEP)?(""):("not "));
printf("Memory Type Range Registers: %ssupported\n", (_MTRR)?(""):("not "));
printf("Page Global Enable: %ssupported\n", (_PGE)?(""):("not "));
printf("Machine Check Architecture: %ssupported\n", (_MCA)?(""):("not "));
printf("Conditional Move Instruction Supported: %ssupported\n", (_CMOV)?(""):("not "));
printf("Page Attribute Table: %ssupported\n", (_PAT)?(""):("not "));
printf("36-bit Page Size Extension: %ssupported\n", (_PSE36)?(""):("not "));
printf("Processor serial number is present and enabled: %ssupported\n", (_PSN)?(""):("not "));
printf("CLFLUSH Instruction Supported: %ssupported\n", (_CLFSH)?(""):("not "));
printf("Debug Store: %ssupported\n", (_DS)?(""):("not "));
printf("Thermal Monitor and Software Controlled Clock Facilities supported: %ssupported\n", (_ACPI)?(""):("not "));
printf("Intel Architecture MMX technology sported: %ssupported\n", (_MMX)?(""):("not "));
printf("Fast floating point save and restore: %ssupported\n", (_FXSR)?(""):("not "));
printf("Streaming SIMD Exensions supported: %ssupported\n", (_SSE)?(""):("not "));
printf("Streaming SIMD Extensions 2: %ssupported\n", (_SSE2)?(""):("not "));
printf("Self-Snoop: %ssupported\n", (_SS)?(""):("not "));
printf("Hyper-Threading Technology: %ssupported\n", (_HTT)?(""):("not "));
printf("Thermal Monitor supported: %ssupported\n", (_TM)?(""):("not "));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment