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, The truth table for not a is: The basic binary logical operations are: The complements of these binary logical operations are also commonly used: 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: Notes: The operands x, y, and z must be declared as .word. Logical operations are performed bitwise on the operands. Although listed in the text, the operations not, nand, and xnor are not implemented in the SPIM simulator. 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: Shift Operations Up: Arithmetic and Logical Operations Previous: Arithmetic and Logical Operations

CS 301 Class Account
Mon Sep 13 11:15:41 ADT 1999