HITEQUEST
technical interview Q & A for high-tech professionals
 
 
Interview Questions
Electronics Hardware
Computer Software
General questions
Brain teasers

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

Break time stories
Tomato company

About Hitequest
About Hitequest
Join us
Home page

 
 
 
 
 
 
   

 
=Electronics Hardware Questions=

     
   

 
  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:
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
 
 
 

A:
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