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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:10:09+00:00 2026-05-30T08:10:09+00:00

Maybe I have missed something about functions or something else? I use VS 2010.

  • 0

Maybe I have missed something about functions or something else?
I use VS 2010.
Here is my code:

#include <iostream>

void swap (int& a, int& b)
{
     __asm push a
     __asm push b
     __asm pop a
     __asm pop b
     std::cout << "Inline function check: a=" << a << " b=" << b << "\n\n";
}

void main()
{
    int arg1=1, arg2=9000;
    std::cout << "Before swap we have: a=" << arg1 << " b=" << arg2 << "\n\n";
    swap(arg1, arg2);
    std::cout << "After swap we have: a=" << arg1 << " b=" << arg2;
    std::cin.get();
}
  • 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-30T08:10:10+00:00Added an answer on May 30, 2026 at 8:10 am

    When you pass by reference with Visual Studio, the compiler passes the address of the referenced variable. Your swap is actually swapping the addresses that were passed to the swap function; it’s not actually swapping the contents.

    In order to swap the contents, you would need to dereference the pointers. Unfortunately, the compiler won’t allow you to do this directly, so you’ll have to use registers.

    Here is a short example. This is just a demonstration; swapping the two variables can be done with far fewer instructions if you access the stack directly, instead of using a and b. It’s important to note that memory can only be modified by using a register.

    void swap(int& a, int& b) {
    __asm {
        // Preserve our registers
        push eax 
        push ebx 
        push ecx 
        push edx 
        // Store the address of a in eax and b in ebx
        mov eax, a
        mov ebx, b
        // Store the value of a in ecx and b in edx
        mov ecx, [eax]
        mov edx, [ebx]
        // Swap the values pointed to by a and b
        mov [eax], edx
        mov [ebx], ecx
        // Restore the registers
        pop edx
        pop ecx
        pop ebx
        pop eax
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I have missed something with the code below... Maybe you can help
Maybe I have missed something, but what are wrong with this regular expresion? var
i have looked everywhere(but maybe i've missed something) I'm currently creating a website where
Maybe I'm doing something wrong but I have a textarea where I've specified textAlign
I have some C++ source code with templates maybe like this - doxygen runs
If I have sequence of sequences (maybe a list of tuples) I can use
I think I have run into a bug in Mojarra 2.1.0. Maybe I missed
I'm thinking maybe I missed something in JavaScript that I'm just picking up now.
I have a Spring rest service using Spring 3.1.0.RELEASE. Here is the relevant code
Maybe i have the wrong context but... Is there a corelib site for mysql

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.