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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:39:17+00:00 2026-05-27T15:39:17+00:00

I am trying to familiarise myself with x86 assembly using GCC’s inline assembler. I’m

  • 0

I am trying to familiarise myself with x86 assembly using GCC’s inline assembler. I’m trying to add two numbers (a and b) and store the result in c. I have four slightly different attempts, three of which work; the last doesn’t produce the expected result.

The first two examples use an intermediate register, and these both work fine. The third and fourth examples try to add the two values directly without the intermediate register, but the results vary depending on the optimization level and the order in which I add the input values. What am I getting wrong?

Environment is:

i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)

First, the variables are declared as follows:

int a = 4;
int b = 7;
int c;

Example 1:

asm("   movl    %1,%%eax;"
    "   addl    %2,%%eax;"
    "   movl    %%eax,%0;"
    : "=r" (c)
    : "r" (a), "r" (b)
    : "%eax"
   );
printf("a=%d, b=%d, c=%d\n", a, b, c);
// output: a=4, b=7, c=11

Example 2:

asm("   movl    %2,%%eax;"
    "   addl    %1,%%eax;"
    "   movl    %%eax,%0;"
    : "=r" (c)
    : "r" (a), "r" (b)
    : "%eax"
   );
printf("a=%d, b=%d, c=%d\n", a, b, c);
// output: a=4, b=7, c=11

Example 3:

asm("   movl    %2,%0;"
    "   addl    %1,%0;"
    : "=r" (c)
    : "r" (a), "r" (b)
   );
printf("a=%d, b=%d, c=%d\n", a, b, c);
// output with -O0: a=4, b=7, c=11
// output with -O3: a=4, b=7, c=14

Example 4:

// this one appears to calculate a+a instead of a+b
asm("   movl    %1,%0;"
    "   addl    %2,%0;"
    : "=r" (c)
    : "r" (a), "r" (b)
   );
printf("a=%d, b=%d, c=%d\n", a, b, c);
// output with -O0: a=4, b=7, c=8
// output with -O3: a=4, b=7, c=11

SOLVED. Matthew Slattery’s answer is correct. Before, it was trying to reuse eax for both b and c:

movl    -4(%rbp), %edx
movl    -8(%rbp), %eax
movl    %edx, %eax
addl    %eax, %eax

With Matthew’s suggested fix in place, it now uses ecx to hold c separately.

movl    -4(%rbp), %edx
movl    -8(%rbp), %eax
movl    %edx, %ecx
addl    %eax, %ecx
  • 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-05-27T15:39:18+00:00Added an answer on May 27, 2026 at 3:39 pm

    By default, gcc will assume that an inline asm block will finish using the input operands before updating the output operands. This means that both an input and an output may be assigned to the same register.

    But, that isn’t necessarily the case in your examples 3 and 4.

    e.g. in example 3:

    asm("   movl    %2,%0;"
        "   addl    %1,%0;"
        : "=r" (c)
        : "r" (a), "r" (b)
       );
    

    …you have updated c (%0) before reading a (%1). If gcc happens to assign the same register to both %0 and %1, then it will calculate c = b; c += c, and hence will fail in exactly the way you observe:

    printf("a=%d, b=%d, c=%d\n", a, b, c);
    // output with -O0: a=4, b=7, c=11
    // output with -O3: a=4, b=7, c=14
    

    You can fix it by telling gcc that the output operand may be used before the inputs are consumed, by adding the “&” modifier to the operand, like this:

    asm("   movl    %2,%0;"
        "   addl    %1,%0;"
        : "=&r" (c)
        : "r" (a), "r" (b)
       );
    

    (See “Constraint Modifier Characters” in the gcc docs.)

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

Sidebar

Related Questions

I am trying to familiarise myself with using adb from the command prompt. My
I am just trying to familiarise myself with the basics of C++ moving from
In a project I've been trying to familiarise myself with, I ran across a
Trying to add the ability to delete a Folder using FTP and all subfolders
I'm prepping for a simple work project and am trying to familiarize myself with
Trying to make a make generic select control that I can dynamically add elements
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I'm trying to familiarize myself with the STL library, but I'm having trouble understanding
I'm trying to familiarize myself a bit more with database programming, and I'm looking
I'm trying to familiarize myself with Facebook's new Graph API and so far I

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.