GATE / CS ARCHIVE

GATE CSE 1994 / QUESTION 12(a)

XOR register exchange

Computer Organization & Architecture · Instructions & Addressing Modes · Full GATE CSE 1994 paper

GATE 1994, Q12(a) Descriptive
Report issue

Instructions & Addressing Modes

Question 12(a), GATE 1994: A CPU has only R1,R2 and instruction XOR Ri,Rj, meaning Ri←Ri XOR Rj for i,j∈{1,2}. Give a sequence that exchanges R1 and R2.
Tap to enlarge
Answer & solution

Answer: XOR R1,R2; XOR R2,R1; XOR R1,R2. (no official key was published)

1. Execute XOR R1,R2; XOR R2,R1; XOR R1,R2, in that order.

2. Let the original contents be a,b. After the first instruction the pair is (a XOR b,b).

3. After the second it is (a XOR b,b XOR (a XOR b))=(a XOR b,a).

4. After the third it is ((a XOR b) XOR a,a)=(b,a), using associativity and x XOR x=0. No temporary register is needed.

Original paper, page 5