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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:05:18+00:00 2026-06-12T21:05:18+00:00

According to some textbooks, the compiler will use sub* to allocate memory for local

  • 0

According to some textbooks, the compiler will use sub* to allocate memory for local variables.

For example, I write a Hello World program:

int main()
{
    puts("hello world");
    return 0;
}

I guess this will be compiled to some assembly code on the 64 bit OS:

    subq    $8, %rsp
    movq    $.LC0, (%rsp)
    calq    puts
    addq    $8, %rsp

The subq allocates 8 byte memory (size of a point) for the argument and the addq deallocates it.

But when I input gcc -S hello.c (I use the llvm-gcc on Mac OS X 10.8), I get some assembly code.

.section    __TEXT,__text,regular,pure_instructions
.globl  _main
.align  4, 0x90
_main:
Leh_func_begin1:
       pushq    %rbp
Ltmp0:
    movq    %rsp, %rbp
Ltmp1:
    subq    $16, %rsp
Ltmp2:
    xorb    %al, %al
    leaq    L_.str(%rip), %rcx
    movq    %rcx, %rdi
    callq   _puts
    movl    $0, -8(%rbp)
    movl    -8(%rbp), %eax
    movl    %eax, -4(%rbp)
    movl    -4(%rbp), %eax
    addq    $16, %rsp
    popq    %rbp
    ret

   .......

L_.str:
      .asciz     "hello world!"

Around this callq without any addq and subq. Why? And what is the function of addq $16, %rsp?

Thanks for any input.

  • 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-12T21:05:20+00:00Added an answer on June 12, 2026 at 9:05 pm

    You don’t have any local variables in your main(). All you may have in it is a pseudo-variable for the parameter passed to puts(), the address of the "hello world" string.

    According to your last disassembly, the calling conventions appear to be such that the first parameter to puts() is passed in the rdi register and not on the stack, which is why there isn’t any stack space allocated for this parameter.

    However, since you’re compiling your program with optimization disabled, you may encounter some unnecessary stack space allocations and reads and writes to and from that space.

    This code illustrates it:

    subq    $16, %rsp ; allocate some space
    ...
    movl    $0, -8(%rbp) ; write to it
    movl    -8(%rbp), %eax ; read back from it
    movl    %eax, -4(%rbp) ; write to it
    movl    -4(%rbp), %eax ; read back from it
    addq    $16, %rsp
    

    Those four mov instructions are equivalent to just one simple movl $0, %eax, no memory is needed to do that.

    If you add an optimization switch like -O2 in your compile command, you’ll see more meaningful code in the disassembly.

    Also note that some space allocations may be needed solely for the purpose of keeping the stack pointer aligned, which improves performance or avoids issues with misaligned memory accesses (you could get the #AC exception on misaligned accesses if it’s enabled).

    The above code shows it too. See, those four mov instructions only use 8 bytes of memory, while the add and sub instructions grow and shrink the stack by 16.

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

Sidebar

Related Questions

Hello I'm trying to change several controls' property according to some environment variables and
The program below can generate random data according to some specs (example here is
I would like that Mathematica simplifies an expression according to some constraints. For example:
Suppose that there are some hex contents in four-byte memory space. According to some
according to some tutorials i read a while back, the const declaration makes a
I'm trying to set default scope according to some criteria determined by ana ActionController
How to show or hide a field for some records according to condition in
According to Learn you some Erlang : Pretty much any function you can think
According to MSDN BOL (Books Online) description on SOME | ANY (Transact-SQL) , SOME
I have some data from a external party which is encrypted according to them

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.