Microprocessor Design and the ARM
Logical Operations
Logical operation are performed bit by bit on the input operands (which may be values in registers or constants) and the result placed in a destination register.
AND r0, r1, r2 ; r0 := r1 and r2 - the result bit is 1 if both input bits are 1
r1: 0101 0011 1010 1111 1101 1010 0110 1011
r2: 1101 0110 1010 0000 0111 0101 1010 0011
r0: 0101 0010 1010 0000 0101 0000 0010 0011
AND instructions are useful for "masking" parts of values that you are not interested in currently.
The diagrams below show the state of the microprocessor before and after executing the example AND instruction. In the first diagram, the Program Counter is pointing at the address in memory where the next instruction can be found (this is address 100016). The registers r1 and r2 already contain the bit values to be manipulated. The values in memory addresses 100016 to 100316 are the hexadecimal representation of the instruction "AND r0, r1, r2".
Before executing the instruction
After the instruction has been executed, the result of the AND instruction has been placed in the r0 register and the Program Counter has been incremented such that its new value is 100416.
After executing the instruction
ORR r0, r1, r2 ; r0 := r1 or r2 - the result bit is 1 if either input bit is 1
r1: 0101 0011 1010 1111 1101 1010 0110 1011
r2: 1101 0110 1010 0000 0111 0101 1010 0011
r0: 1101 0111 1010 1111 1111 1111 1110 1011
ORR instructions are useful for ensuring that certain bits are set.
EOR r0, r1, r2 ; r0 := r1 xor r2 - the result bit is set to 1 if one and only one of the inputs bits is 1
r1: 0101 0011 1010 1111 1101 1010 0110 1011
r2: 1101 0110 1010 0000 0111 0101 1010 0011
r0: 1000 0101 0000 1111 1010 1111 1100 1000
EOR instructions can be useful for inverting specific bits.
BIC r0, r1, r2 ; r0 := r1 and not r2
BIC stands for 'bit clear', where every '1' in the second operand clears the corresponding bit in the first:
r1: 0101 0011 1010 1111 1101 1010 0110 1011
r2: 1101 0110 1010 0000 0111 0101 1010 0011
r0: 0000 0001 0000 1111 1000 1010 0100 1000
BIC instructions can be considered as kind of reverse OR. They can be used to clear specific regions of a word, e.g.
r2: 1111 1111 1111 1111 0000 0000 0000 0000
would clear the upper halfword leaving the bits in the lower two bytes untouched.