Thread: type MP with C ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    11

    Post type MP with C ?

    hi everyone , i write a program in C to determine the type of microprocessor ,so for this task i include in c code source a assembler code ,,,,

    this my code
    Code:
    // Compile by BC++version 3.1
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void main() {
      char VendorSign[13];   // for to store our vendorstring
      unsigned long MaxEAX;  //This will be used to store the maximum EAX
      char* Comp1[32];  //This is the array that will hold the short names
                        //for our features bitmap.
      unsigned long REGEAX, REGEBX, REGECX, REGEDX;
      int dFamily, dModel, dStepping, dFamilyEx, dModelEx;
      char dType[10];
      int dComp1Supported[32];
      int dBrand, dCacheLineSize, dLogicalProcessorCount, dLocalAPICID;
      asm {
        XOR EAX, EAX
        //An efficient alternatvie to MOV EAX, 0x0
        db 0fh,0a2h
        //=cpuID,This instruction will load our registers.
        MOV dword ptr [VendorSign], EBX
        //Copy the first 4 bytes in the VendorString from EBX.
        MOV dword ptr [VendorSign+4], EDX
        //Copy the next 4 bytes.
        MOV  dword ptr [VendorSign+8],   ECX
        //Copy the next 4 bytes.
        MOV  dword ptr MaxEAX,           EAX
        //EAX contains the maximum value to call CPUID with. Copy 
        //it to the MaxEAX variable.
      }
      VendorSign[12]=0;  
    //The last character in the VendorSign can be anything.
    //To make sure that it stops at the last character we add
    //a zero character at the end
      printf("Vendor string: %s\n", VendorSign);
      printf("Maximum EAX value: %i\n", MaxEAX);
      if(strcmp(VendorSign, "GenuineIntel")==0) {
        Comp1[0]="FPU";   //Floating Point Unit
        Comp1[1]="VME";   //Virtual Mode Extension
        Comp1[2]="DE";    //Debugging Extension
        Comp1[3]="PSE";   //Page Size Extension
        Comp1[4]="TSC";   //Time Stamp Counter
        Comp1[5]="MSR";   //Model Specific Registers
        Comp1[6]="PAE";   //Physical Address Extesnion
        Comp1[7]="MCE";   //Machine Check Extension
        Comp1[8]="CX8";   //CMPXCHG8 Instruction
        Comp1[9]="APIC";  //On-chip APIC Hardware
        Comp1[10]="";     //Reserved
        Comp1[11]="SEP";  //SYSENTER SYSEXIT
        Comp1[12]="MTRR"; //Machine Type Range Registers
        Comp1[13]="PGE";  //Global Paging Extension
        Comp1[14]="MCA";  //Machine Check Architecture
        Comp1[15]="CMOV"; //Conditional Move Instrction
        Comp1[16]="PAT";  //Page Attribute Table
        Comp1[17]="PSE-36"; //36-bit Page Size Extension
        Comp1[18]="PSN";  //96-bit Processor Serial Number
        Comp1[19]="CLFSH"; //CLFLUSH Instruction
        Comp1[20]="";     //Reserved
        Comp1[21]="DS";   //Debug Trace Store
        Comp1[22]="ACPI"; //ACPI Support
        Comp1[23]="MMX";  //MMX Technology
        Comp1[24]="FXSR"; //FXSAVE FXRSTOR (Fast save and restore)
        Comp1[25]="SSE";  //Streaming SIMD Extensions
        Comp1[26]="SSE2"; //Streaming SIMD Extensions 2
        Comp1[27]="SS";   //Self-Snoop
        Comp1[28]="HTT";  //Hyper-Threading Technology
        Comp1[29]="TM";   //Thermal Monitor Supported
        Comp1[30]="IA-64"; //IA-64 capable
        Comp1[31]="";     //Reserved
      }
      else if(strcmp(VendorSign, "AuthenticAMD")==0) {
        Comp1[0]="FPU";   //Floating Point Unit
        Comp1[1]="VME";   //Virtual Mode Extension
        Comp1[2]="DE";    //Debugging Extension
        Comp1[3]="PSE";   //Page Size Extension
        Comp1[4]="TSC";   //Time Stamp Counter
        Comp1[5]="MSR";   //Model Specific Registers
        Comp1[6]="PAE";   //Physical Address Extesnion
        Comp1[7]="MCE";   //Machine Check Extension
        Comp1[8]="CX8";   //CMPXCHG8 Instruction
        Comp1[9]="APIC";  //On-chip APIC Hardware
        Comp1[10]="";     //Reserved
        Comp1[11]="SEP";  //SYSENTER SYSEXIT
        Comp1[12]="MTRR"; //Machine Type Range Registers
        Comp1[13]="PGE";  //Global Paging Extension
        Comp1[14]="MCA";  //Machine Check Architecture
        Comp1[15]="CMOV"; //Conditional Move Instrction
        Comp1[16]="PAT";  //Page Attribute Table
        Comp1[17]="PSE-36"; //36-bit Page Size Extension
        Comp1[18]="";     //?
        Comp1[19]="MPC";  //MultiProcessing Capable
        Comp1[20]="";     //Reserved
        Comp1[21]="";     //?
        Comp1[22]="MIE";  //AMD Multimedia Instruction Extensions
        Comp1[23]="MMX";  //MMX Technology
        Comp1[24]="FXSR"; //FXSAVE FXRSTOR (Fast save and restore)
        Comp1[25]="SSE";  //Streaming SIMD Extensions
        Comp1[26]="";     //?
        Comp1[27]="";     //?
        Comp1[28]="";     //?
        Comp1[29]="";     //?
        Comp1[30]="3DNowExt"; //3DNow Instruction Extensions
        Comp1[31]="3DNow"; //3DNow Instructions 
      }
      if(MaxEAX>=1) {
        asm {
          MOV     EAX,                    1
          db 0fh,0a2h
          MOV     [REGEAX],               EAX
          MOV     [REGEBX],               EBX
          MOV     [REGECX],               ECX
          MOV     [REGEDX],               EDX
        }
        dFamily=((REGEAX>>8)&0xF);
        dModel=((REGEAX>>4)&0xF);
        dStepping=(REGEAX&0xF);
        dFamilyEx=((REGEAX>>20)&0xFF);
        dModelEx=((REGEAX>>16)&0xF);
        switch(((REGEAX>>12)&0x7)) {
          case 0:
            strcpy(dType, "Original");
            break;
          case 1:
            strcpy(dType, "OverDrive");
            break;
          case 2:
            strcpy(dType, "Dual");
            break;
        }
    
        for(unsigned long C=1, Q=0;Q<32;C*=2, Q++) {
          dComp1Supported[Q]=(REGEDX&C)!=0?1:0;
        }
    
    
        dBrand=REGEBX&0xFF;
        dCacheLineSize=((strcmp(Comp1[19], "CLFSH")==0)
    	&&(dComp1Supported[19]==1))?((REGEBX>>8)&0xFF)*8:-1;
        dLogicalProcessorCount=((strcmp(Comp1[28], "HTT")==0)
    	&&(dComp1Supported[28]==1))?((REGEBX>>16)&0xFF):-1;
        dLocalAPICID=((REGEBX>>24)&0xFF); //This works on P4 or later
      }
      printf("%s\n", dType);
      printf("Family %i, Model %i, Stepping %i\n", dFamily, dModel, dStepping);
      printf("Extended Family %i, Extended Model %i\n", dFamilyEx, dModelEx);
      printf("Supported flags: ");
      for(unsigned long Q=0;Q<27;Q++) {
        if(dComp1Supported[Q]) {
          printf("%s ", Comp1[Q]);
        }
      }
      printf("\n");
      printf("CacheLineSize: %i\n", dCacheLineSize);
      printf("Logical processor count: %i\n", dLogicalProcessorCount);
      printf("Local APIC ID: %i\n", dLocalAPICID);
    }
    so in resulr i have one error in 17 line ,undefined symbole EAX ,,,so perhap i need to include another header file or what? thanks before for all helps

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What compiler are you using?

    Unfortunately, inline assembler syntax is different for different compilers.

    Also, any compiler/assembler produced in the last 8 years or so should know the CPUID instruction, so you don't need "db 0x0f, 0xa2" to form the instruction by hand.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    11
    Quote Originally Posted by matsp View Post
    What compiler are you using?

    Unfortunately, inline assembler syntax is different for different compilers.

    Also, any compiler/assembler produced in the last 8 years or so should know the CPUID instruction, so you don't need "db 0x0f, 0xa2" to form the instruction by hand.

    --
    Mats
    i already indicated that i use compile bc++ v3.1 ,it`s for DOS, and i know ,,,exactly i hear that the suitable compiler is bc++ v 3.1 for that code ... i don`t understand how resolve this error

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you use a 16-bit compiler, chances are that it doesn't know about 386 instructions, such as XOR EAX, EAX.

    It's been a long time [say 15+ years] since I used any Borland compiler, so I can't really say what it can and can't do.

    Why do you want to do this in DOS? I'm guessing you are actually running Windows on the machine, so compiling this as a console app for Windows would relieve any problems of using 32-bit register instructions.

    And why do you have to set the array for EVERY bit of every field twice, when there are only a few that are different (and SSE2 for example is definitely available in AMD processors, so that shouldn't be made different). Certainly, the first 18 entries can be set without any conflict, and of the remaining 14, only a few are actually supposed to be different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    11
    Quote Originally Posted by matsp View Post
    If you use a 16-bit compiler, chances are that it doesn't know about 386 instructions, such as XOR EAX, EAX.

    It's been a long time [say 15+ years] since I used any Borland compiler, so I can't really say what it can and can't do.

    Why do you want to do this in DOS? I'm guessing you are actually running Windows on the machine, so compiling this as a console app for Windows would relieve any problems of using 32-bit register instructions.
    --
    Mats
    i already tried to compile this code with VC++ as console app for windows but i have the probleme and someone advice me to use BC++ so for this reason ,,,,
    and for the code how i can resolve it ?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Exactly what problem do you have in VC++?

    Assuming you have no particular reason to use this in DOS, I would say that using VC should be the goal, and finding the reason for the error would be a step in that direction. Compiling it in a DOS compiler serves no purpose.

    If I compile this with visual studio 2008 Express Ed as C++ code, all I need to do is:
    1. replace asm with _asm
    2. replace db .. with CPUID

    [I may have missed some minor fix, but in essence certainly that works].

    And as far as I can tell, it shows reasonable values in the output.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM