technical interview Q & A for high-tech professionals
Interview Questions
Electronics Hardware
Computer Software
Quick Thinking Tests
General questions
Phone screening questions
Submit your Q or A

Technical articles
Technical discussion
Resume and interview
How to get a job in Silicon Valley
How much are you worth on market?
Do you need an agent?

Break point
Written Exam
Logic Tests
Professional Test
Tomato company
Cup of coffee
How stock market works
Engineering jokes

About Hitequest
About Hitequest
Home page


=Electronics Hardware Questions=


Your system has CPU, ALU and two 8 bit registers. There is no external memory. Can you swap the content of the registers ?

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.
REG1       REG2
A       B
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

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 subtractor.
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