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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:37:09+00:00 2026-06-13T10:37:09+00:00

The book I’m reading says that this function has a local variable. It also

  • 0

The book I’m reading says that this function has a local variable. It also says that having a local variable for this function is important because it’s recursive. Maybe I’m just blind or I don’t understand how local variables work in assembly, but I don’t see it.

.type factorial, @function
factorial:
    push %rbp               # Save old base pointer.
    mov  %rsp, %rbp         # Copy stack pointer to base pointer.
    mov  16(%rbp), %rax     # Save the argument in %rax.
    cmp  $1, %rax           # End of the factorial.
    je   end_factorial
    dec  %rax               # Decrement %rax.
    push %rax               # Push onto stack for next call to factorial.
    call factorial
    mov  16(%rbp), %rbx     # %rax has return value, so load arg into %rbx.
    imul %rbx, %rax         # Multiply that by result of last call to factorial.

end_factorial:
    # Restore stack pointer and base pointer to where they were
    # before function call.
    mov %rbp, %rsp
    pop %rbp
    ret

Can registers be considered local variables? I though local variables were implemented with something like sub $8, %rsp.

  • 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-13T10:37:10+00:00Added an answer on June 13, 2026 at 10:37 am

    Local variables are not defined through their implementation, but by their semantics.

    A definition

    A variable is local, if every invocation of the function gets its own independent value for that variable, that’s why you could say that the value of the variable is “local to the function”.

    Since this behavior can be achieved by using registers, that’s a perfectly valid implementation.

    Saving and restoring local variables

    However, since certain registers are considered caller-saved, the values may have to be put on the stack before calling nested functions, otherwise the values would be lost. Upon returning to the original function from the nested call, the value can then be restored into the register from the stack.

    As has already been mentioned, a accessing a register is much faster than accessing the stack. Thus, they are preferred to main memory whenever possible.

    However, I do not know why it couldn’t just do imul 16(%rbp), %rax without the additional move operation. After all, the imul instruction allows the source operand to be in-memory.

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

Sidebar

Related Questions

The book I am reading says that after allocating an object I need to
The book Learning jQuery says IE has memory leak for the DOM object having
The book I'm reading says it does. I think it doesn't because it the
The book I am reading says that SQL Server supports two kinds of character
One Book(Object Oriented Programming with C++ by E.Balagurusamy) says that const size = 10;
Book I’ learning from claims that intArray has two dimensions. But since calling intArray.GetLength(1)
The book of CLR via C# has this line at page 76. When you
The book Professional Javascript by Zakas says that 1) IE uses event bubbling 2)
A book I have says this: a) Place each value of the one-dimensional array
The book I'm reading about Erlang has exercises in the back of it and

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.