Computer Systems and Organisation - Logic Instructions and Bitwise Operations

Logic Instructions

  • Logic instructions in computer systems perform bitwise operations, manipulating individual bits of binary numbers or words.

Types of Logic Instructions

  • AND (and, andi):
    • Resulting bits are set to 1 if both corresponding source bits are 1.
    • Example:
      ```assembly
      li $t0, 0x5555
      andi $t0, $t0, 0x0FC3 # t0 = 5555 & 0FC3 = 0541
- **OR (`or`, `ori`)**:  
  - Resulting bits are set to 1 if either of the corresponding source bits are 1.  
  - Example:  

assembly
li $t0, 0x5555
ori $t0, $t0, 0x0FC3 # t0 = 5555 | 0FC3 = 5FD7

- **XOR (`xor`, `xori`)**:  
  - Resulting bits are set to 1 if exactly one of the corresponding source bits is 1.  
  - Example:  

assembly
li $t0, 0x5555
xori $t0, $t0, 0x0FC3 # t0 = 5555 ^ 0FC3 = 5A96

- **NOT (`not`)**:
  - Inverts the bits of the source number.

## Bitwise Operations 

- Logic instructions apply Boolean operations on pairs of words, focusing individually on each bit. 
- High-level languages feature logical operators (`&&`, `||`, `!`) and bitwise operators (`&`, `|`, `^`, `~`).  
- MIPS architecture employs only bitwise instructions.

## Practical Applications of AND Instruction

- **Parity Check**:  
  - Used to check if a number is even or odd.  

assembly
andi $v0, $a0, 0x01 # if $a0 is even, $v0 = 0; odd, $v0 = 1

- **Checking Specific Bits**:  
  - ```assembly  
  andi $v0, $a0, 0x04  # checks if 3rd bit is set  
  • Extracting Bits:
    • ```assembly
      andi $v0, $a0, 0x03F0 # extracts bits 4-9 of $a0
- **Clearing Bits**:  
  - ```assembly  
  andi $a0, $a0, 0xFFFFFFF3  # Clears specific bits  

Practical Applications of OR Instruction

  • Setting Bits:
    • Use OR to set specific bits in a number.
      ```assembly
      ori $a0, $a0, 0x0078 # Sets bits 3 to 6 in $a0
- **Li Compilation**:  
  - Pseudoinstruction `li` is compiled to load upper immediate (`lui`) and OR instruction.  

assembly
li $t0, 0x13579BDF

  - Translates to:  

assembly
lui $at, 0x1357
ori $t0, $at, 0x9BDF

## Practical Applications of XOR Instruction

- **Toggle Bits**: Use XOR to toggle specific bits in a register.  

assembly
xori $a0, $a0, 0x0007 # Toggles bits 0 to 2

## Exercises for Understanding bitwise operations

- Code snippet to place in `v0‘theremainderwhen‘v0` the remainder when `a0` is divided by 8:  

assembly
andi $v0, $a0, 0x07 # Extracts last 3 bits

## Shifting Operations 

- **Left Shift (`sll`, `sllv`)**: 
  - Bits are shifted left, effectively multiplying by 2 for each shift.  
  - Example:  

assembly
li $t0, 0x03E9
sll $t0, $t0, 1 # Equivalent to $t0 = 03E9 << 1 = 07D2

- **Right Shift (`srl`, `srlv`)**:  
  - Bits are shifted right, effectively dividing by 2 for each shift.  
  - Example:  

assembly
li $t0, 0x270F
srl $t0, $t0, 1 # Equivalent to $t0 = 270F >> 1 = 1387
```

Fast Multiplication/Division
  • Multiplication and Division by a power of 2 is optimized using shift instructions, making them significantly faster than multiplication/division instructions.

Instructions Recap

  • Load/Store: li, move, la, lw, sw, lhu, sh, lbu, sb
  • Arithmetic: add, sub, mul, div, neg
  • Logic: and, or, xor, not, nor
  • Shifting: sll, sllv, srl, srlv