next up previous
Next: Shift Operations Up: Arithmetic and Logical Operations Previous: Arithmetic and Logical Operations

Logical Operations

Arithmetic and logical operations are performed in the CPU by the Arithmetic/Logical Unit (ALU). SAL arithmetic instructions were previously introduced. Logical instructions are bit-oriented manipulations of data based on the mathematical logic operations and, or, and not.

Logical operations are applied to boolean variables which have two values: true and false, or 1 and 0.

Logical operations are defined by a truth table.

The unary not operation applied to a boolean variable, a, is the complement of a, tex2html_wrap_inline261 The truth table for not a is:

tabular26

The basic binary logical operations are:

tabular29

The complements of these binary logical operations are also commonly used:

tabular32

A total of 16 different binary logical operations are possible. In addition to those above, operations which simply produce a, b, or true, and the complements of these operations, may also be defined.

The SAL logical operations are:

tabular35

Notes:

  1. The operands x, y, and z must be declared as .word.
  2. Logical operations are performed bitwise on the operands.
  3. Although listed in the text, the operations not, nand, and xnor are not implemented in the SPIM simulator.
  4. Pascal has no equivalent bitwise operations.

Logical operations are used to set, clear or complement selected bits using a mask.

When four ASCII characters are stored in a 32 bit word, any single character may be extracted using a mask. Suppose the location cell contains the characters 'Char' from left to right. The hexadecimal value of cell is 0x43686172.

The rightmost 'r' of cell may be extracted by performing the and operation on cell with the mask 0x000000FF.

The and operation clears (sets to 0) the bits which correspond to 0's in the mask and leaves the bits corresponding to 1's unchanged.

For example, the SAL instruction

        and      result, cell, 0x000000FF

would assign result the value 0x00000072, which is the character 'r'.

To replace one character with another, the old character is first cleared using a mask and then the new value is merged using the or operation.

The SAL instructions

        and     result, cell, 0xFFFFFF00
        or      result, result, 0x00000074

change the rightmost 'r' of cell to a 't' producing 'Chat' in result.

The or operation sets the bits which correspond to 1's in a mask and leaves the bits corresponding to 0's unchanged.

For example, the SAL instruction

        or     result, cell, 0x20202020

changes all alphabetic characters in cell to lowercase and assigns 'char' to result by turning on the bit for lowercase ASCII characters.

The xor operation reverses the bits which correspond to 1's in a mask and leaves the bits corresponding to 0's unchanged.

For example, the SAL instruction

        xor     result, num, 0x80000000

reverses the sign bit of num and assigns the new value to result.

The SAL instruction

        xor     result, cell, 0x20202020

reverses the case of all alphabetic characters in cell and assigns 'cHAR' to result.

To complement all the bits in a word, the xor operation can be performed with a mask of 0xFFFFFFFF.


next up previous
Next: Shift Operations Up: Arithmetic and Logical Operations Previous: Arithmetic and Logical Operations

CS 301 Class Account
Thu Nov 5 12:25:05 AST 1998