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

  • Home
  • SEARCH
  • 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 8434221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:35:52+00:00 2026-06-10T06:35:52+00:00

I have the following code: #include <stdio.h> void main(){ int x=0, y=0,i=100; for (;i<1000;

  • 0

I have the following code:

#include <stdio.h>
void main(){
    int x=0, y=0,i=100;
    for (;i<1000; i++,x+=32){
        if (x == 25*32) {
            y+=32;
            asm volatile("pushl %%eax\n\t"
                         "movl $0, %%eax\n\t"
                         "popl %%eax\n\t"
                         :"=a"(x)
                         :"a"(0)
                         );
        }
        printf("%d %d\n", x, y);
    }
}

Basically, what I want to do is, set the variable x to 0 but I don’t quite understand what the assembly code does, and it actually does set x to 0 but I’m not sure whats going on. can somebody explain what’s going on? (just for the sake of learning assembly and C).

  • 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-10T06:35:54+00:00Added an answer on June 10, 2026 at 6:35 am

    Here is what your asm construct says:

    • In "=a"(x), you tell the compiler that the assembly will write (=) to the %eax (a) register, and you want the compiler to assign that result to x ((x)).
    • In "a"(0), you tell the compiler you want it to put a 0 ((0)) in %eax (a), and the assembly will read it.
    • Then push %%eax saves %eax on the stack, movl $0, %%eax puts 0 in %eax, and popl %%eax restores the saved value to %eax.

    So, what happens is:

    • The compiler puts 0 in %eax.
    • Your instructions save 0 on the stack, move 0 into %eax, and restore 0 from the stack.
    • The compiler uses the 0 in %eax as the value of x.

    So, this works, but it is inefficient. You can get the same effect with this:

    asm volatile("movl $0, %[MyName]"
        : [MyName] "=r" (x)
    );
    

    What this says is:

    • There are no inputs (because the second “:” is absent).
    • As before, the = tells the compiler these instructions will write a result.
    • The r says the result will be written to a register, but the compiler gets to pick the register.
    • The [MyName] tells the compiler to change %[MyName], where it appears in the assembly code, to the name of the register the compiler picks.
    • As before, the (x) says to use the value in the register after the assembly code as the new value of x.
    • Finally, the instruction movl $0, %[MyName] says to move 0 to the register named by %[MyName].

    Because the compiler gets to pick the register, you do not have to save and restore it in the assembly language. The compiler is responsible for making sure it does not need that register for anything else.

    Being able to name the operands as I have done with [MyName] is a new feature in GCC. If your version does not have it, you can do this instead:

    asm volatile("movl $0, %0"
        : "=r" (x)
    );
    

    Without names, each operand gets a number, starting at 0, and incremented in the order the operands appear in the input/output specifiers. Since we had only one operand, it was %0.

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

Sidebar

Related Questions

i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have the following code: #include <stdio.h> #include <math.h> int main(void) { printf(%f\n, fmax(1.2,
We have the following code: #include <stdio.h> #define LEN 10 int main(void) { int
I have the following snippet of C code: #include <stdio.h> void main(){ int a
I have the following code: #include <stdio.h> int main(void) { int x __attribute__ ((aligned
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
i have following code #include <stdlib.h> #include <stdio.h> int sorter( const void *first_arg,const void*
I have the following code: #include <stdlib.h> #include <stdio.h> #include <errno.h> void main(void) {
I have the following code: #include <stdio.h> void insertion_sort(char[], int); void swap(char*, char*); int
I got the following simple C++ code: #include <stdio.h> int main(void) { ::printf(\nHello,debugger!\n); }

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.