Q: Your system has CPU, ALU and two 8 bit
registers. There is no external memory. Can you swap the
content of the registers ?
A: solution by hbegin
XOR has the unique property of giving you the original value
if you XOR it twice: ((A XOR B) XOR B) = A
So... the answer:
Note: values on the left side, showing contents of the
registers, happen after the step listed at right.
A XOR B B Step 1:"XOR R1,R2" #R1 <= R1 XOR R2
A XOR B B XOR (A XOR B) = A Step 2:"XOR R2,R1" #R2 <= R2 XOR R1
(A XOR B) XOR A = B A Step 3:"XOR R1,R2" #R1 <= R1 XOR R2
A: solution by unsigned
I think another solution is possible using the ALU.
It is possible to swap the two registers using the following
lines of assembly. This requires only the adder and
The two registers to swap are R1 and R2.
add R1, R1, R2 // R1 = R1 + R2
sub R2, R1, R2 // R2 = R1 - R2
sub R1, R1, R2 // R1 = R1 - R2