If I have two input, for example: a= 0110 b = 1010 which one is better (in terms of memory consumption, performance):
x = a (XOR) b
x = a (AND) b
x = a (OR) b
And why?
Answer
In general, these will all be the same (take the same amount of code space and execute at the same speed) because these operations -- AND, OR and XOR -- are included as part of the functionality for the ALU (arithmetic logic unit) of most computers, and there are dedicated instructions for each operation.
The formats may include either register to register, memory to register. immediate to register, or immediate to memory depending on the addressing modes of the CPU (a CISC machine like the 80x86 may have all four; a RISC machine like the ARM may only have register to register plus immediate to register). Memory to memory however is seldom provided. The more complicated versions of these instructions (i.e. indexed, indirect, indexed/indirect) may require additional bytes on some machines, but that will be based on the addressing mode, not the instruction type (AND, OR, or XOR).
The time to execute the instructions will also generally be the same, since these are once again fundamental operations of the ALU. On modern processors, they would all be expected to take just one instruction cycle for register to register or immediate to register; on some architectures memory references may require additional cycle(s).
The following is the circuit for a 1-bit slice of the ALU in the 8085, an older microprocessor from the 1980's (but the principles are still the same today). Eight of these circuits were used to make up the entire 8-bit ALU. (For newer machines with larger data paths, you would use 16, 24, 32 or even 64 1-bit slices.)
Note that there are no specific lines for operations such as AND, OR, XOR or even ADD. Instead, there are an number of control lines like select_op1 and force_ncarry_1 that select how the circuit processes the inputs and produces a result. How these lines are set up is shown in the following table for each bit-wise or arithmetic operation:
So you can see, for example, an XOR operation takes fundamentally no longer than an AND or OR instruction; the same circuitry is used for each operation.
No comments:
Post a Comment