Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Help with Asm in C code (GCC)

  1. #1
    Join Date
    Feb 2014
    Beans
    8

    Help with Asm in C code (GCC)

    Hello guys,
    Im trying to understand the how to code asm inside a C code. So, i want to use the "cpuid" assembly instruction, but im doing something wrong on the code:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
       unsigned int maxBasicCPUID;
       char vendorString[13];
       char *vendorStringPtr=(char *)vendorStringPtr;
    
    
            __asm__("movl %a,%%edi\n\t"
                    "movl $0,%eax\n\t"
                    "cpuid\n\t"
                    "movl %%eax,%0\n\t"
                    "movl %ebx,(%edi)\n\t"
                    "movl %edx,4(%edi)\n\t"
                    "movl %ecx,8(%edi)\n\t"
                    :"=g" (maxBasicCPUID)
                    :"m" (vendorStringPtr)
                    :"memory"
                    );
    
    
            vendorString[12]=9;
            printf("maxBasicCPUID=%x, vendorString=%s\n", maxBasicCPUID,vendorString);
    
    
    return 0;          
    }
    When i try to compile, i got the following error:

    # gcc -o cpuid cpuid.c
    cpuid.c: In function ‘main’:
    cpuid.c:9: error: invalid 'asm': operand number missing after %-letter
    cpuid.c:9: error: invalid 'asm': operand number missing after %-letter
    cpuid.c:9: error: invalid 'asm': operand number missing after %-letter
    cpuid.c:9: error: invalid 'asm': operand number missing after %-letter
    cpuid.c:9: error: invalid 'asm': operand number missing after %-letter
    cpuid.c:9: error: invalid 'asm': operand number missing after %-letter
    cpuid.c:9: error: invalid 'asm': operand number missing after %-letter
    cpuid.c:9: error: invalid 'asm': operand number missing after %-letter

    Can anyone help me with that!?
    Tks guys!

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,294
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Help with Asm in C code (GCC)

    What is %a supposed to represent?
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Dec 2013
    Beans
    Hidden!

    Re: Help with Asm in C code (GCC)

    I'm going to suggest something that might get me lambasted by the purists here.

    The AT&T syntax that you're trying to use has always been confusing. And it's never been popular, especially in the x86 world. And it's obsolete now that gcc supports the Intel syntax for inline assembly.

    My reasons for using Intel syntax is that it's much easier to learn. Most of the assembly code in the world is written with it so you'll find many, many more examples of it. And I suspect that given the same level of expertise with both syntaxes you'll always code much quicker using the Intel syntax.

    And don't think that you are forced to use the AT&T syntax becaue a C file you're modifying already has inline assembly with that syntax. You can add Intel style inline assembly without having to rewrite the existing inline assembly.

    I really do believe that it could actually be quicker for you to start over with Intel syntax than trying to debug what you have.

    Let me apologize for not making an effort to debug your code. I've forgotten whatever little I knew about the AT&T syntax as I purposley haven't used it for years.

    Here is a link if you're interested the Intel syntax:
    http://www.rohitab.com/discuss/topic...-intel-syntax/

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,294
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Help with Asm in C code (GCC)

    Quote Originally Posted by xb12x2 View Post
    I'm going to suggest something that might get me lambasted by the purists here.
    Yes, but not for the reason you are probbly thinking of: it's just completely irrelevant to the problem here.
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Dec 2013
    Beans
    Hidden!

    Re: Help with Asm in C code (GCC)

    Quote Originally Posted by Bachstelze View Post
    Yes, but not for the reason you are probbly thinking of: it's just completely irrelevant to the problem here.
    Totally uncalled for, Bachstelze, since it's not completely true.

    It's true I didn't debug his code, but how did your question "What is %a supposed to represent?" help him? If you know what the bug is why didn't you just tell him? Now that would have been relevant.
    Last edited by xb12x2; February 16th, 2014 at 12:09 AM.

  6. #6
    Join Date
    Feb 2014
    Beans
    8

    Re: Help with Asm in C code (GCC)

    Quote Originally Posted by xb12x2 View Post
    I'm going to suggest something that might get me lambasted by the purists here.

    The AT&T syntax that you're trying to use has always been confusing. And it's never been popular, especially in the x86 world. And it's obsolete now that gcc supports the Intel syntax for inline assembly.

    My reasons for using Intel syntax is that it's much easier to learn. Most of the assembly code in the world is written with it so you'll find many, many more examples of it. And I suspect that given the same level of expertise with both syntaxes you'll always code much quicker using the Intel syntax.

    And don't think that you are forced to use the AT&T syntax becaue a C file you're modifying already has inline assembly with that syntax. You can add Intel style inline assembly without having to rewrite the existing inline assembly.

    I really do believe that it could actually be quicker for you to start over with Intel syntax than trying to debug what you have.

    Let me apologize for not making an effort to debug your code. I've forgotten whatever little I knew about the AT&T syntax as I purposley haven't used it for years.

    Here is a link if you're interested the Intel syntax:
    http://www.rohitab.com/discuss/topic...-intel-syntax/
    I actually dont mind using the Intel syntax, but can you help me with the code for intel ( and how to compile it)!?

  7. #7
    Join Date
    Dec 2013
    Beans
    Hidden!

    Re: Help with Asm in C code (GCC)

    Quote Originally Posted by dan60 View Post
    I actually dont mind using the Intel syntax, but can you help me with the code for intel ( and how to compile it)!?
    Since I made a suggestion it's only fair that I try to help with it. But first let me give you a bit of code, AT&T syntax, that does the cpuid instruction, and some C code that calls it. Just to try to help you get quickly beyond the bug. I know it worked at the time, but it's been several years since I wrote it. If you're still interested in an Intel syntax version let me know and I'll try to help with it.
    Code:
     /******************************************************************************  ** GetCpuID        Executes a CPUID instruction  *  *  parameters:    The CPUID function to execute  *  *  returns:           results in eax, ebx, ecx, edx returned via pointer  *  */ volatile DWORD _eax, _ebx, _ecx, _edx; void GetCpuID(DWORD func) {     asm      (            "cpuid"         : "=a" (_eax),           "=b" (_ebx),           "=c" (_ecx),           "=d" (_edx)         : "a"  (func)     ); }
    The following code was written for unique, one-off hardware: [CODE] /************************************************** **************************** ** ClearEccAbove4MB Clears memory above 4MB in the first four dimms. * The last two dimms are battery backed. * * parameters: none * * returns: none * * This routine clears memory that is NOT battery-backed. * */ extern volatile DWORD _eax, _ebx, _ecx, _edx; void ClearEccAbove4MB( void )[CODE] { // // Just for fun check for PSE-36 feature bit: EDX[17] for CPUID 1 // GetCpuID( 1 ); // If PSE-36 not supported then return (this should never happen on Slammer). if( !(_edx & (1
    Last edited by xb12x2; February 16th, 2014 at 01:08 AM.

  8. #8
    Join Date
    Dec 2013
    Beans
    Hidden!

    Re: Help with Asm in C code (GCC)

    Quote Originally Posted by dan60 View Post
    I actually dont mind using the Intel syntax, but can you help me with the code for intel ( and how to compile it)!?
    I screwed-up my previous attemtp to post some code. Let me try again:

    This is AT&T syntax that does cpuid, and following it is some code that calls it.
    This code worked at the time I wrote it several years ago, and was written for unique one-off hardware.


    If you still want to write it in Intel syntax I be glad to try and help.



    Code:
    /******************************************************************************
     ** GetCpuID        Executes a CPUID instruction
     *
     *  parameters:    The CPUID function to execute
     *
     *  returns:        results in eax, ebx, ecx, edx returned via pointer
     *
     */
    volatile DWORD _eax, _ebx, _ecx, _edx;
    void GetCpuID(DWORD func)
    {
        asm 
        (    
           "cpuid"
            : "=a" (_eax),
              "=b" (_ebx),
              "=c" (_ecx),
              "=d" (_edx)
            : "a"  (func)
        );
    }


    Code:
    /******************************************************************************
     ** ClearEccAbove4MB    Clears memory above 4MB in the first four dimms. 
     *                            The last two dimms are battery backed.
     *
     *  parameters:    none
     *
     *  returns:        none
     *
     *  This routine clears memory that is NOT battery-backed.
     *
     */
    extern volatile DWORD _eax, _ebx, _ecx, _edx;
    
    void ClearEccAbove4MB( void )
    {
        //
        // Just for fun check for PSE-36 feature bit: EDX[17] for CPUID 1
        //
        GetCpuID( 1 );
    
        // If PSE-36 not supported then return (this should never happen on Slammer).
        if( !(_edx & (1 << 17)) )
        {
            return;
        }
    
        // Clear 4MB to 2GB.
        ClearMemory( (_int64)0x400000, (_int64)0x80000000 );
    
        // If there is only 3GB total ram then return.
        if( CONFIG_512MB == ReadExCMOS( CMOS2_MEM_CONFIG ) )
        {
            return;
        }
    
    
        //
        // Must be either 6GB or 12GB at this point.
        //
    
    
        // Clear 2GB to (4GB - 64MB).
        ClearMemory( (_int64)0x80000000LL, (_int64)(0x100000000LL - 0x4000000LL) );
    
    
        // If there is 6GB total ram then clear the 64MB
        // in the first 4 dimms that is remapped above 6GB.
        if( CONFIG_1GB == ReadExCMOS( CMOS2_MEM_CONFIG ) )
        {
            // Clear 6GB to (6GB + 64MB).
            ClearMemory( (_int64)0x180000000LL, (_int64)(0x180000000LL + 0x4000000LL) );
            return;
        }
    
    
        //
        // 12GB Memory Configuration
        //
    
        // Clear 4GB to 6GB.
        ClearMemory( (_int64)0x100000000LL, (_int64)0x180000000LL );
    
        // Clear 6GB to 8GB
        ClearMemory( (_int64)0x180000000LL, (_int64)0x200000000LL );
    
        // Clear the 64MB in the first 4 dimms that is remapped above 12GB.
        ClearMemory( (_int64)0x300000000LL, (_int64)(0x300000000LL + 0x4000000LL) );
    
        // Clear the ERS register
        WritePciWord( 0, 0, 0, 0x4C, 0x3FFF );
    
        return;
    }

  9. #9
    Join Date
    Feb 2014
    Beans
    8

    Re: Help with Asm in C code (GCC)

    If you're still interested in an Intel syntax version let me know and I'll try to help with it.
    If you could, i'll appreciate your help

    Regading the:
    Code:
     asm 
        (    
           "cpuid"
            : "=a" (_eax),
              "=b" (_ebx),
              "=c" (_ecx),
              "=d" (_edx)
            : "a"  (func)
        );


    I didnt understand how exactly you passed the C variables into the asm code, what does "a" "b" "c" and "d" means!?

    Oh...how do i compile it (using the intel syntax) !?

    Thank you so much for the help!
    Last edited by dan60; February 16th, 2014 at 01:46 AM.

  10. #10
    Join Date
    Dec 2013
    Beans
    Hidden!

    Re: Help with Asm in C code (GCC)

    Quote Originally Posted by dan60 View Post
    If you could, i'll appreciate your help

    Regading the:
    Code:
     asm 
        (    
           "cpuid"
            : "=a" (_eax),
              "=b" (_ebx),
              "=c" (_ecx),
              "=d" (_edx)
            : "a"  (func)
        );


    I didnt understand how exactly you passed the C variables into the asm code, what does "a" "b" "c" and "d" means!?

    Oh...how do i compile it (using the intel syntax) !?

    Thank you so much for the help!
    This is what I was afraid of. This is AT&T syntax. The GNU Assembler, known as gas or as, has built-in higher-level pseudo ops, or macros, so to speak. And this just adds to the overall dificulties and confusion when using AT&T syntax. And inline assembly adds it's own little bit of complexity.

    Again, I know this isn't what you want to do at this particular time, and I apologize for that, but I'll suggest it anyway:

    I found that my quickest way to code (and debug) things like this was to write all my assembly routines inside a stand-alone assembly file (a *.asm file) using an assembler like nasm (open source, based on Microsoft's masm assemblers) And then link it to my gcc C files.

    The reason nasm was created is because not only is most of the assemly code in the world in Intel syntax, it's also mostly in the masm style.

    I'm obvously trying to avoid not only AT&T syntax, but the Gas assembler as well. I found that having a stand-alone nasm file always worked out better, no matter the circumstances.

    I will try to provide an example of what I just suggested in my next post.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •