-1

I'm trying to write a XOR operation in assembly language, but the only operations we're allowed to use are AND and NOT, not OR and definitely not XOR. I have looked everywhere online and I can't seem to find the answer. I know: XOR = (P or Q) and ~(P and Q) But I need to rewrite (P or Q) with an AND operation instead. Is this possible?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
M. Erkan
  • 19
  • 2

1 Answers1

6

One of De Morgan's laws(a) states that (using ~ for negation (not), for conjunction (and) and for disjunction (or)):

~A ∨ ~B = ~(A ∧ B)

In your case or P ∨ Q, P is ~A and Q is ~B. So:

P ∨ Q = ~(~P ∧ ~Q)

That right side is therefore the equivalent of P ∨ Q, using only ~ and operations.


(a) And a big thanks for letting me use this knowledge for about the third time since I left University in 1986 :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Ahh thank you! I had (~P and ~Q) but I did not get the same truth table as (P or Q). I was missing the extra 'not.' – M. Erkan Oct 30 '19 at 01:58
  • Side note: in electronics you commonly use [nand logic to implement all kinds of functions](https://en.wikipedia.org/wiki/NAND_logic#OR) and you are allowed to use just that. Of course it's not the simplest solution. – Jester Oct 30 '19 at 02:19
  • 2
    @Jester, I remember doing that in the early days because the 74LS chips came in chunks (like several NAND gates to a chip). It was more efficient to use any spares to build an `OR` function than drop in *another* chip to do the work. Surely nowadays, you can get a million of each gate on a single chip though :-) – paxdiablo Oct 30 '19 at 02:27
  • 1
    Yes, you could put a million gates on each chip, but you wouldn't have pins for each of them. And the outside-facing ones need large enough transistors to drive TTL or CMOS levels over the capacitance of external wiring, and be at least somewhat resistant to ESD and short circuits... There's a reason we have FPGAs and ASICs instead of 6-million-pin 74LS chips :) But yeah good point. A design might also want to use 2 of the same chip instead of 2 different chips to keep the bill-of-materials shorter. – Peter Cordes Oct 30 '19 at 05:00
  • Good point, @Peter, I totally forgot about the potentially large pin count :-) – paxdiablo Oct 30 '19 at 09:24