Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:01:02+00:00 2026-06-14T21:01:02+00:00

In the following: #include <string.h> struct cpuidOut { long a ; long b ;

  • 0

In the following:

#include <string.h>

struct cpuidOut
{
   long a ;
   long b ;
   long c ;
   long d ;
} ;

void callcpuid( cpuidOut * p, long a )
{
   memset( p, 0xFF, sizeof(*p) ) ;
   p->a = a ;

   __asm__ ( "cpuid"
             : "+a"(p->a), "=b"(p->b), "=c"(p->c), "=d"(p->d)  // output
             :                                                 // no (only) inputs
             : "a", "b", "c", "d"                              // clobbered registers
           ) ;
}

I get a compile error:

t.C:22: error: unknown register name 'd' in 'asm'
t.C:22: error: unknown register name 'c' in 'asm'
t.C:22: error: unknown register name 'b' in 'asm'
t.C:22: error: unknown register name 'a' in 'asm'

(same sort of error from either g++ or clang++)

This suprises me since I see a, b, c, d listed in the i386 clobbers in the gcc documentation

http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints

I can fix the clobber constraints by being explicit:

"%rax", "%rbx", "%rcx", "%rdx"                  // clobbered registers

but I’m suprised I have to do so? I only need this to work on x86_64, but I thought the “a”, “b”, “c”, “d” style constraints would be nicer in case the code is later needed on i386 too.

EDIT: I’d initially posted the wrong asm after tweaking it a couple of times trying to get it to work, and getting mixed up along the way. The asm above is consistent with my initial question, however, causes a compile error with the compiler unable to schedule the A register. This seems to work instead:

void callcpuid( cpuidOut * p, long a, long b )
{
   __asm__ ( "cpuid"
             : "=a"(p->a), "=b"(p->b), "=c"(p->c), "=d"(p->d)  // output
             : "0"(a), "1"(b)                                  // inputs
           ) ;
}

but notice that it avoids the requirement for any clobber constraints at all, since all the clobbers are outputs. That’s probably the right way to do it, although I’m still suprised after reading the gcc docs that I can’t use the generic reg names “a”, “b”, “c”, “d” in the clobber constraints, instead having to use “%eax”, “%rax”, …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:01:03+00:00Added an answer on June 14, 2026 at 9:01 pm

    This suprises me since I see a, b, c, d listed in the i386 clobbers in the gcc documentation

    The clobbers are not constraints.

    Constraints are used when you tell GCC to allocate registers for insn operands, the constraints define the acceptable register class from which to draw registers.

    The clobbers, on the other hand, tell GCC about registers, which are modified by the insns, in the cases where it’s not evident from the input/output constraints, for example an insn, which modifies a fixed register, which is not one of its operands or when you use hardcoded register names in the inline assembly.

    This is needed, so before execution of the inline asm, GCC can stash away values, that it happens to keep in the clobbered registers.

    PS. For input-output operands, you can used the "+" modifier:

    void callcpuid( cpuidOut * p, long a, long b )
    {
       __asm__ ( "cpuid" : "+a"(p->a), "+b"(p->b), "=c"(p->c), "=d"(p->d)) ;
    }
    

    PS. 32-bit code generated:

    movl    (%esi), %eax  ; load p->a
    movl    4(%esi), %ebx ; load p->b
    cpuid
    movl    %ebx, 4(%esi)  ; write back into p->b
    movl    (%esp), %ebx   
    movl    %eax, (%esi)   ; write back into p->a
    movl    %ecx, 8(%esi)  ; write p->c
    movl    %edx, 12(%esi) ; write p->d
    

    64-bit code generated:

    movq    (%rdi), %rax   ; load p->a
    movq    8(%rdi), %rbx  ; load p->b
    cpuid
    movq    %rbx, 8(%rdi)  ; write back p->b
    movq    %rax, (%rdi)   ; write back p->a
    movq    %rcx, 16(%rdi) ; write p->c
    movq    %rdx, 24(%rdi) ; write p->d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following program: #include <string> struct S { S (){} private: void *ptr
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
I have the following code #include <iostream> #include<string> #include <sstream> using namespace std; struct
Lets say we have the following code: #include <iostream> #include <string> struct A {
For the following code: #include<iostream> #include<vector> #include<string> using namespace std; struct Test { string
Consider the following code: #include <unordered_map> struct A {}; struct T { std::unordered_map<std::string, A>
Assume the following code: #include <string> #include <iostream> using namespace std; struct A {
I have the following code: #include <map> #include <string> class policy1 { public: struct
The following code: #include <boost/variant.hpp> #include <iostream> #include <string> struct A { A() {
Given the following program: #include <iostream> #include <string> using namespace std; struct GenericType{ operator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.