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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:09:20+00:00 2026-06-12T09:09:20+00:00

The latest version of gcc is producing assembly that doesn’t make sense to me.

  • 0

The latest version of gcc is producing assembly that doesn’t make sense to me. I compiled the code using no optimization; but, some parts of this code don’t make sense, even with no optimization.

Here is the C source:

  #include <stdio.h>

   int main()
   {
     int a = 1324;
     int b = 5657;
     int difference = 9876;
     int printf_answer = 2221;

     difference = a - b;

     printf_answer = printf("%d + %d = %d\n", a, b, difference);

     return difference;
   }

It produces this assembly:

    .file   "exampleIML-1b.c"
    .section    .rodata
.LC0:
    .string "%d + %d = %d\n"
    .text
.globl main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    pushq   %rbx
    subq    $24, %rsp
    movl    $1324, -32(%rbp)
    movl    $5657, -28(%rbp)
    movl    $9876, -24(%rbp)
    movl    $2221, -20(%rbp)
    movl    -28(%rbp), %eax
    movl    -32(%rbp), %edx
    movl    %edx, %ecx
    subl    %eax, %ecx
    movl    %ecx, %eax
    movl    %eax, -24(%rbp)
    movl    $.LC0, %eax
    movl    -24(%rbp), %ecx
    movl    -28(%rbp), %edx
    movl    -32(%rbp), %ebx
    .cfi_offset 3, -24
    movl    %ebx, %esi
    movq    %rax, %rdi
    movl    $0, %eax
    call    printf
    movl    %eax, -20(%rbp)
    movl    -24(%rbp), %eax
    addq    $24, %rsp
    popq    %rbx
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (GNU) 4.4.6 20120305 (Red Hat 4.4.6-4)"
    .section    .note.GNU-stack,"",@progbits

Several things don’t make sense:

(1) Why are we pushing %rbx? What is in %rbx that needs to be saved?

(2) Why are we moving %edx to %ecx before subtracting? What doesn’t it just do sub %eax, %edx?

(3) Similarly, why the move from %ecx back to %eax before storing the value?

(4) The compiler is putting the variable a in memory location -32(%rbp). Unless I’m adding wrong, isn’t -32(%rbp) equal to the stack pointer? Shouldn’t all local variables be stored at values less than the current stack pointer?

I’m using this version of gcc:

[eos17:~/Courses/CS451/IntelMachineLanguage]$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure –prefix=/usr –mandir=/usr/share/man –infodir=/usr/share/info –with-bugurl=http://bugzilla.redhat.com/bugzilla –enable-bootstrap –enable-shared –enable-threads=posix –enable-checking=release –with-system-zlib –enable-__cxa_atexit –disable-libunwind-exceptions –enable-gnu-unique-object –enable-languages=c,c++,objc,obj-c++,java,fortran,ada –enable-java-awt=gtk –disable-dssi –with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre –enable-libgcj-multifile –enable-java-maintainer-mode –with-ecj-jar=/usr/share/java/eclipse-ecj.jar –disable-libjava-multilib –with-ppl –with-cloog –with-tune=generic –with-arch_32=i686 –build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)

  • 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-12T09:09:21+00:00Added an answer on June 12, 2026 at 9:09 am
    GCC dictates how the stack is used. Contract between caller and callee on x86:
    
        * after call instruction:
              o %eip points at first instruction of function
              o %esp+4 points at first argument
              o %esp points at return address 
        * after ret instruction:
              o %eip contains return address
              o %esp points at arguments pushed by caller
              o called function may have trashed arguments
              o %eax contains return value (or trash if function is void)
              o %ecx, %edx may be trashed
              o %ebp, %ebx, %esi, %edi must contain contents from time of call 
        * Terminology:
              o %eax, %ecx, %edx are "caller save" registers
              o %ebp, %ebx, %esi, %edi are "callee save" registers
    

    The main function is like any other function in this context. gcc decided to use ebx for intermediate calculations, so it preserves its value.

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

Sidebar

Related Questions

I'm using XAMPP (latest version) on OS 10.6.2, that's bundled with PHP 5.3.0 I
I'm using latest version of NDK android-ndk-r8b I have some files that were builded
It seems that since I installed the latest version of GCC, I am no
I'm using the latest MinGW: MINGWBASEDIR=C:\mingw gcc version 4.7.0 (GCC) gcc version 4.7.0 (GCC)
Mingw.org obviously isn't completely dead, but the latest version there is gcc 4.5.2, while
I'm using the latest version of the jquery plugin DataTables and I tried to
I use MinGW latest version to compile the following code. I get the folowing
So Hibernate Supports the latest Version of Firebird, which is really great. But... I
Using the latest version of Resharper (4.5.x) with VS2008. Every now and then (pretty
I am using the latest version of protobuf-net with VS2008 integration. I have created

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.