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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:49:50+00:00 2026-05-21T08:49:50+00:00

Hi I was interested in what assembly code will gcc generate from this code

  • 0

Hi
I was interested in what assembly code will gcc generate from this code (this is just dummy code to illustrate my point):

int a = 0;
int foo(void)
{
    int result = a;
    a += 2;
    return result;
}

I was surprized that gcc copies the variable a to the stack and then from the stack to a register so it can return it. When I added register to the declaration of result, it optimized the code not to use the stack but instead to copy the variable directly to a register. I know this doesn’t really make any difference but I was wondering is there any good reason why gcc doesn’t make such optimization implicitly. I hope I made it clear what I am talking about…

Any ideas?

  • 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-21T08:49:51+00:00Added an answer on May 21, 2026 at 8:49 am

    When compiling Debug builds (i.e., with optimizations off), compilers tend to make very straightforward, easily debuggable code. In this case, it might mean keeping all variables in memory / stack, rather than in registers.

    Try compiling with full optimizations (-O3) and see if that makes a difference.

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

Sidebar

Related Questions

I am trying to understand some assembly. The assembly as follows, I am interested
I am interested in moving a number of my projects from Visual Studio and
I've been looking at some LLVM assembly produced by llvm-gcc lately and I've noticed
(EDIT: This question is now outdated for my particular issue, as Google Code supports
I often accidently step into code that I'm not interested in while debugging in
I'm primarily interested in popular and widely used compilers, such as gcc. But if
I'm interested in writing javascript that will programmatically perform a screen capture of what
using the code How to embed a satellite assembly into the EXE file provided
I wonder if it's possible obfuscate only some strings from your source code into
I am really interested in assembly language and I want to learn about how

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.