int A = x;
int B = y;
// Some code that swaps A and B without using a temporary variable
A == y;
B == x;
The more interesting the answer the better.
int A = x;
int B = y;
// Some code that swaps A and B without using a temporary variable
A == y;
B == x;
The more interesting the answer the better.
Compare A with y and throw the result in the bin.
Compare B with x and throw the result in the bin.
Push x onto stack. Copy y to x. Pop y back from stack.
On a platform that has a register to spare of the appropriate size, load x in this spare register. Copy memory contents of y to address of x. Write spare register contents back to y.
One more way to cheat: malloc sizeof int and memcpy contents of x there. Memcpy contents of y to x. Memcpy contents of malloc'ed memory back to y. This of course is really the same as using 3rd variable, but hey...the variable is never named...
Or use whatever kind of peripheral that has a user-writable memory that can hold the int and use it as a cache...
Two examples at: Bit Twiddling Hacks
3 steps of XOR.
Should not be as follows to comply with the Title of the thread?:
A == B
B == A
The micro code in the Burroughs Medium System main frame created an instruction that could have either two or three addresses. It was called "SWAP" in the BPL language. Mostly used to rearrange the values of month, day, year.
Did you understand the question?
This is in Bar Sport, I'm more interested in interesting answers than trying to find a definitive right answer.
How would that work?
I could argue that doing so is using the stack as the forbidden temporary variable, but as noted already this is Bar Sport.
Here is my solution, with example values for A and B:
A = 3
B = 4
B = B - A
A = A + B
B = A - B
Here is the solution from the person who posed the question to me:
a = a + b - (b = a)
Here is another solution based on the above:
a += b
b = a - b
a -= b
If I want move B into A and A into B, I must use a temporary register which you are not allowing.
Searching for: XCHG direct, direct DMA instruction for both 8051 and AVR without luck! There is SWAP instruction for nibbles!!
X = X ^ Y;
Y = X ^ Y;
X = X ^ Y;
I was taught this in an IBM System 360 OS field engineering course. The reason it is often favoured is the XOR instruction is a single machine language instruction of 1 or 2 clock cycles in length. (I am 82, it's been over 50 years so I might be a wee bit off on the details, but I think it's pretty close.)
A = 3
B = 4
B = B - A
A = A + B
B = A - B
This works for AVR: SUB, SBC, ADD, ADC instructions. 6 instructions, 1 cycle each, 4 registers.
Same with XOR.
In actual execution, the result of B-A must be placed in an invisible temporary variable/register before putting into B. That means that the CPU is allowed to use temporary variable and not the Programmer.
No. AVR instructions SUB, SBC work with registers.
Define A and B as (for example) 16 bit integers occupying the space of a 32 bit integer.
Use a logical rotate arithmetic instruction to rotate the 32 bit integer by 16 bits.
Do you want mean that sub r16, r17 instruction follows what has been depicted in Fig-1? If yes, then ALU itself is working as a temporary variable/storage.

Figure-1:
This is a pseudo code. The actual operation could be described by Fig-1 of post #18.
The result of r16-r17 must have been stabilized before it is put into destination register, r16. So, there must be a delay element (aka memory element) which is the ALU.