Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 182 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to swap values

#1
In the below code, output remains same in both cases,thanks for pointing what am I missing:-

Before Swap:-
a=10 b=512
After Swap:-
a=10 b=512

Following is the code, It compiles and runs without any problem:-

#include <stdio.h>
int swap(int* x, int* y)
{
if(x != y)
{
_asm
{
mov eax,[x]; x into eax
mov ebx,[y]
mov [x],ebx;swapping now
mov [y],eax
}
}
return 0;
}

int main () {
int a=10,b=512;
printf("Before Swap:- \na=%d\t b=%d\n",a,b);
swap(&a,&b);
printf("After Swap:- \na=%d\t b=%d",a,b);//Value remains same
return 0;
}
Reply

#2
No indirection on variables inside assembly block wont work.Instead take addresses in registers and then only try indirection.It will rather break into something like `mov eax, DWORD PTR _x$[ebp]`

#include <stdio.h>
int swap(int* x, int* y)
{
if(x != y)
{
_asm
{
mov eax,x
mov ebx,y
mov ecx,[ebx]
xchg ecx,[eax]
xchg [ebx],ecx
}
}
return 0;
}

int main () {
int a=10,b=512;
printf("Before Swap:- \na=%d\t b=%d\n",a,b);
swap(&a,&b);
printf("After Swap:- \na=%d\t b=%d",a,b);
getchar();
return 0;
}
Reply

#3
You can swap with Xor operation -

void swap(int *x, int *y)
{
*x = *x ^ *y; /* #1 */
*y = *x ^ *y; /* #2 */
*x = *x ^ *y; /* #3 */
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through