Skip to main content
gamedev.net
🔒 Locked

vc++ 2003 inline asm with pointers

Started by a2ps Oct 31, 2004 at 8:43 PM 3 replies 2.2k views
Original Post
a2ps
a2ps
hi there, ive been learning inline asm for the visual c++ 2003 compiler, and so for starters ive tried to make a very simple int swap function:

void swap_asm( int *a, int *b )
{
  __asm
  {
    push a;
    push b;
    pop a;
    pop b;
  }
}
i call this function from my main function and in the end A and B remain with the same value, but if i define A and B localy in the swap function, it does swap the values. hows that? thanks in advance
yet, another stupid signature..
Archi
Archi
Quote:
Original post by a2ps
void swap_asm( int *a, int *b ){  __asm  {    push a;    push b;    pop a;    pop b;  }}


i call this function from my main function and in the end A and B remain with the same value, but if i define A and B localy in the swap function, it does swap the values. hows that?
thanks in advance


There could be too many answers, beginning from "local scope" compiler variable turned on and finishing with stack pointers, and the reason could be any of them. Get yourself "Art of Assembly Programming", and look for "stack", "pointer" and "call". Sorry for such an answer but I'm not very good in english so I fear tou can misunderstand my explanation.
a2ps
a2ps
thanks for the reply, ill look up on those topics.
one thing i dont get is, if i declare 2 pointers that point to those given variables, and do the swap with them, it works, but the original ones remain the same.
example:
void swap_asm( int *a, int *b ){  int *t_a = a; //temp pointer  int *t_b = b; //temp pointer  __asm  {    push t_a;    push t_b;    pop t_a;    pop t_b;  }  //here t_a and t_b were swapped, but a and b remained the same!  //but if t_a points to the same adress as a  //and t_b points to the same adress as b, how can this be possible??}

yet, another stupid signature..
Winograd
Winograd
So you want to switch the place of two ints and not pointers? Then you should push and pop those ints!
Somethin like this:
void swap_asm( int *a, int *b ){  __asm  {    push [a];    push ;    pop [a];    pop ;  }}


I'm not too sure if that's going to work, because I'm not familiar with the syntax vc2003 is using for inline asm. But I think you got the idea. If not, think it in C.. you would have to use * in front of your variables, right? You have to tell assembler also that you're using pointers. I think now your code just swaps the local variables a and b and at the end the variables a and b were pointing to, remain the same.
TarinX
TarinX
The problem with the code is not strictly with the assembler code you've written. The arguments passed to the swap function are pointers so only copies of the pointers are passed. You can use a pointer to change the value/attributes of the pointed at thing, but since the actual pointer is a copy you can't change the pointer itself in this way. The code posted here switches the pointers in the function, so this change does not effect the actual pointers you passed into the function. What you want to do is swap the values a and b are pointing at.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.