Mail Archives: djgpp/2002/12/20/17:30:13.1
> For XCHG, both variables would be listed in the output section, and
> the input section would have two numeric constraints. Or something
> like that. You end up with four things listed in the constraints, two
> of which are references to the other two.
asm("XCHG %2,%3" : "=r" (localy2), "=r" (localy1) : "0" (localy2), "1" (localy1));
as in
#include "stdio.h"
int main()
{
int localy1=5,localy2=4;
asm("XCHG %2,%3" : "=r" (localy2), "=r" (localy1) : "0" (localy2), "1" (localy1));
printf("%d %d",localy1,localy2);
}
works.
http://212.247.200.167/asm/asm_deformed_us/documentation/gccasmtut.txt is also helpful.
But I didn't really understand what the principle of gcc extended asm syntax on using constraints is
(I would have exchanged "0" and "1" or omitted the "input registers" entirely).
Anyway, one now can use
#include "stdio.h"
inline void swap(int *x, int *y)
{
asm("XCHG %2,%3" : "=r" (*y), "=r" (*x) : "0" (*y), "1" (*x));
}
inline void exchange(int *x, int *y)
{
asm("XCHG %2,%3" : "=r" (*y), "=r" (*x) : "0" (*y), "1" (*x));
}
int main()
{
int localy1=5,localy2=4;
swap(&localy1,&localy2);
printf("%d %d",localy1,localy2);
exchange(&localy1,&localy2);
printf("%d %d",localy1,localy2);
}
did I want to complain about how inlining of functions id perfomed? Yes, but ah...
link: http://www.computing.net/programming/wwwboard/forum/4268.html
title: swap without temporary variable.
- Raw text -