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 8035143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:13:37+00:00 2026-06-05T02:13:37+00:00

Consider the following code: #include <stdio.h> void main() { uint32_t num = 2; __asm__

  • 0

Consider the following code:

#include <stdio.h>

void main() {
    uint32_t num = 2;

    __asm__ __volatile__ ("CPUID");
    __asm__ __volatile__ ("movl $1, %%ecx":);
    __asm__ __volatile__ ("andl $0, %%ecx": "=r"(num));

    printf("%i\n", num);
}

My initial expectation was that this code would print 0, and it does if I comment out the CPUID line, but as-is it was giving me garbage. After some trial, error, and research I realized that I was getting the value of a random register. Apparently GCC doesn’t assume that I want the result of the statement being executed.

The problem is that I’ve seen (other people’s) code that relies on that statement properly getting the result of the AND, regardless of what is going on with the other registers. Obviously such code is broken, given my observations, and the "=r" should be replaced with "=c".

My question is, can we ever rely on the "=r" constraint behaving consistently or according to the obvious expectation? Or is GCC’s implementation too opaque/weird/other and it’s best just to avoid it in every situation?

  • 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-05T02:13:38+00:00Added an answer on June 5, 2026 at 2:13 am

    In order to use the =r output specifier you need to give gcc the freedom to pick the register that it wants to use. You do that by specifying the inputs and outputs generically with %0 for the output and the inputs starting with %1 for the first input.

    In your case you are saying that num can be in a register. But there is nothing in the asm instruction that uses the output register. So gcc will essentially ignore this.

    The reason that you are getting a different value if you comment or don’t comment the CPUID instruction is that CPUID can write to eax,ebx,ecx, and edx. I tried your example on my system and got 0 as the result in both cases. But I noticed that the assembly that is generated is printing the value of eax. So I guess when I ran this program CPUID was writing 0 to eax.

    If you did want to use the =r constraint you would need to do something like this:

      asm("CPUID \n\t"
          "movl $1, %0 \n\t"
          "andl $0, %0 \n\t"
          :"=r"(num) );
    

    Otherwise if your asm code specifically mentions a register then you will need to specify it in the constraint list. In your example that means using =c.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following code: #include <stdio.h> int main(void) { int a[10]; printf(%d,(int)sizeof(a)); //prints 10*sizeof(int)
Consider the following code which shows compile time error : #include <stdio.h> int main(int
Consider following code: main.cpp: #include <iostream> typedef void ( * fncptr)(void); extern void externalfunc(void);
Consider the following code: #include <stdio.h> #include <ctype.h> char* Mstrupr(char* szCad); int main() {
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following code: #include <stdio.h> int main() { printf(%d, 300 * 300 /
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code: #include <iostream> struct foo { // (a): void bar() {
Consider the following code: #include <iostream> #include <vector> #include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/io.hpp> int main()

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.