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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:32:33+00:00 2026-06-15T20:32:33+00:00

GCC inline assembly error: Error: junk `(%esp)’ after expression I’m studying gcc inline assembly.

  • 0

GCC inline assembly error: Error: junk `(%esp)’ after expression

I’m studying gcc inline assembly. My environment is Win 7 32bit, mingw-gcc 4.6.1.

I have got a problem about the ‘m’ constraint. Here is my c function code:

static int asm_test(int a, int b)
{

    int c = 0;
    __asm__ __volatile__(".intel_syntax\n"
            "mov eax, %1\n" //error
            "mov edx, %2\n" //error
            "add eax, edx\n"
            "mov %0, eax\n" //error
            ".att_syntax"
            :"=m"(c)\
            :"m"(a),"m"(b)\
            :"eax","edx"
            );
    return c;
}

For at&t code, it is like this:

static int asm_test(int a, int b)
{

    int c = 0;
    __asm__ __volatile__(
            "movl %1, $eax\n" //error
            "movl %2, $edx\n" //error
            "addl $edx, $eax\n"
            "movl $eax, %0\n" //error
            :"=m"(c)\
            :"m"(a),"m"(b)\
            :"eax","edx"
            );
    return c;
}

For each of the three lines which operate input/output operands, gcc generate an error when compiling, read like this:

C:\Users\farta\AppData\Local\Temp\cc99HxYj.s:22: Error: junk `(%esp)’ after expression

If i use ‘r’ for input/output constraint, the code will work. But I cannot understand why it works and what the error stands for. Can anyone tell me? As far as I know ‘m’ is just telling gcc not to allocate registers but directly access them in memory if inline asm code try to access input/output operands. Is this correct?

Thanks a lot.

  • 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-15T20:32:34+00:00Added an answer on June 15, 2026 at 8:32 pm

    The problem here is the GCC generates AT&T syntax construct for %0, %1 and %2. If you look at the generated assembly, it looks like:

    .intel_syntax
    mov eax, 8(%ebp)
    mov edx, 12(%ebp)
    add eax, edx
    mov -4(%ebp), eax
    

    which is not a valid Intel syntax.

    Generally, you don’t need to include in the inline assembly explicit load/store operation – just specify register constraint and the compiler will generate loads/stores by itself. This has the advantage that even if your variables (parameters, locals) do not reside in memory at all, but are in registers your code will still be correct – unlike in the case if you explicitly put memory load/stores there.

    For your example, try the following code, look at the assembly (gcc -S) and notice how the compiler will perform moves from argument area (e.g. stack on x86) all by itself.

    int asm_test(int a, int b)
    {
      __asm__ __volatile__ (
                            ".intel_syntax\n"
                            "add %0, %1 \n"
                            ".att_syntax \n"
                            :"+r"(a)
                            :"r"(b));
      return a;
    

    }

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

Sidebar

Related Questions

I'm still fighting with GCC - compiling the following inline assembly code (with -fasm-blocks
Oh man, I've got yet another error while compiling the following inline assembly code
Can I somehow use inline assembly in Haskell (similar to what GCC does for
In gcc inline assembly for x86, How can I move the address of a
I'm playing around with inline assembly in C++ using gcc-4.7 on 64-bit little endian
I have trouble with some inline assembly code. I'm trying to load items from
I have several .asm files with non-inline assembly, and if I include them in
So I just found out GCC could do inline assembly and I was wondering
I'm using Apple's llvm-gcc to compile some code with inline assembly. I wrote what
How to write this assembly code as inline assembly? Compiler: gcc(i586-elf-gcc). The GAS syntax

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.