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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:11:12+00:00 2026-06-02T08:11:12+00:00

Given this piece of code: swap: push ebp ; back up the base pointer,

  • 0

Given this piece of code:

       swap:

            push ebp ; back up the base pointer,
            mov ebp, esp
            ; push the context of the registers on the stack

            push eax
            push ebx
            push ecx
            push edx

            mov eax, [ebp+8] ; address of the first parameter
            mov ebx, [ebp+12] ; address of the second parameter
            mov dl, [eax]
            mov cl, [ebx]

            mov [eax], cl

            mov [ebx], dl

            ; restore the context of the registers from the stack

            pop edx
            pop ecx  
            pop ebx
            pop eax
            ; restore the ebp
            pop ebp
            ret

(This is just the method. Previously we pushed the first and the second parameter on the stack.)

My question is: why do we add 8 to the Base Pointer to get to the address of the first parameter and then 12 ?

I get the fact that they are dword so each of them are 4 bytes..so from ebp + 8 to ebp + 12 it makes sens to make. But why the first one is ebp + 8 ? Because if ESP points to the TOP of the stack, mov ebp, esp means that EBP points to the TOP of the stack. Then we push 4 values on the stack : eax, ebx, ecx and edx. Why is EBP + 8 pointing on the first parameter ?

  • 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-02T08:11:13+00:00Added an answer on June 2, 2026 at 8:11 am

    When the function is called, the stack looks like:

    +-------------+
    | Parameter 2 |
    +-------------+
    | Parameter 1 |
    +-------------+
    | Return Addr |  <-- esp
    +-------------+    
    

    then after the “stack frame” is set up:

    +-------------+
    | Parameter 2 | <-- [ebp + 12]
    +-------------+
    | Parameter 1 | <-- [ebp + 8]
    +-------------+
    | Return Addr |  
    +-------------+    
    | saved ebp   | <-- ebp
    +-------------+ <-- esp
    

    Now the context is saved:

    +-------------+
    | Parameter 2 | <-- [ebp + 12]
    +-------------+
    | Parameter 1 | <-- [ebp + 8]
    +-------------+
    | Return Addr |  
    +-------------+    
    | saved ebp   | <-- ebp
    +-------------+ 
    | saved eax   |  
    +-------------+    
    | saved ebx   |  
    +-------------+    
    | saved ecx   |  
    +-------------+    
    | saved edx   | <-- esp
    +-------------+    
    

    Don’t forget that on many systems the stack grows downward (and that is definitely true of the x86 family), so the top of the stack will have the lowest memory address.

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

Sidebar

Related Questions

Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
I currently use this piece of code to reduce a given text to a
This piece of code returns 1 every time even if there is no given
Quick question (I think). Given this piece of code: SomeObject._gaCallbackStack.each( function(callback) { if ($type(callback)
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
I have this piece of code that searches for a folder given a starting
I have this piece of code which checks whether a given number is prime:
Given this piece of code: -(id) init { self = [super init]; if (self
this piece of code was given in a book. $query=select name, description from widget
In this piece of code, if I match the given pattern on the line,

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.