Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to XOR on a CPU that doesn't have an XOR instruction

Tags:

assembly

This is more of a just for fun question. I’m working on a SC61860 CPU, which is an 8-bit CPU for a Sharp PC-1360 Pocket Computer from 1987 (also used in PC-1401 & 1403’s). Its instruction set doesn’t actually include an XOR. It does have AND, OR, compare, subtraction and addition instructions.

I have tried a few variations of ANDing and ORing values to get result that XOR would produce, but no luck. I was hoping to avoid comparing, but it looks like I don’t have a choice.

If you're interested, you can check out the instruction set.

BTW, this CPU has been great to learn assembly on. Nice and simple, and slow enough (768kHz) that machine language is noticeably faster then using the computers built in BASIC ;) I usually program in C/C++/Java. Assembly has been a breath of fresh air.

like image 916
patterson7019 Avatar asked Aug 14 '15 20:08

patterson7019


People also ask

What happens if you XOR something with itself?

This means that any value XOR'd with zero is left unchanged. This means that any value XOR'd with itself gives zero.

What does XOR do in x86?

logical xor EditPerforms a bit-wise xor of the two operands, and stores the result in destination .

What type of instruction is XOR?

The XOR instruction performs a bit wise Exclusive OR operation between corresponding bits in the two operands and places the result in the first operand. reg, mem, and immed can be 8, 16, or 32 bits. The XOR instruction can be used to reverse selected bits in an operand while preserving the remaining bits.

Is there a zero register in x86?

While the x86/x64 architectures do not have an architectural zero register it seems likely that the Sandybridge processor has a physical zero register. When the renamer detects one of these special instructions it just renames the architectural register to point at the zero register.


1 Answers

From boolean algebra we know that:

A XOR B = (NOT(A) AND B) OR (A AND NOT(B))

Update: Thank @Brett Hale, @slebetman, as the CPU surprisingly not supporting the NOT instruction, it can be emulated by arithmetic negation and subtraction, assuming 2's complement negatives representation):

NOT(A) = (-1) - A

Or in case of different negative representation the -1 can be replaced by the corresponding storage type maximum value (i.e. 255 for 8 bit registers or 65565 for 16 bit registers).

like image 126
Eugene Sh. Avatar answered Sep 19 '22 13:09

Eugene Sh.