MIPS Logic & Shift
Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:
Immediate Value Immediate Value Value hard coded into instruction
Immediate Value Immediate Value Value hard coded into instruction 16 bits available to store immediate:
Register Registers = 32 bits, Immediate 16 bits Must extend immediate
Max Immediate Immediate value can't require 17+ bits Unless you use Pseudo instructions – Must be UNCHECKED for now
OR vs ORI OR $6, $5, $4 ORI $6, $0, 0xffff
Logical Ops to Know Immediate instructions ori, andi, xori instruction $dest, $source, immediate Register-Register Instructions or, and, xor, nor instruction $dest, $source1, $source2
Quick Reminders A or 0 = A – Anything or'd with 0 is that thing A and 1 = A – Anything and'd with 1 is that thing A and 0 = 0 – Anything and'd with 0 is 0 A xor 1 = not(A) – Xoring with 1 flips bit
Common Tricks ORI Load a value to register by ori $0 and value
Common Tricks OR Clear a register by oring $0 with self
Common Tricks OR Copy register by or with $0
Common Tricks OR Combine non-overlapping patterns:
Common Tricks AND Keep only specified bits by anding with 1's in desired digits
Common Tricks NOR Do NOT by NOR with $0
Logical Shift Logical Shifts – Add 0's to fill empty space sll, srl instructions: instruction $dest, $source, number of bits
NOP NOP : No operation – Done with sll $0, $0, $0 – Opcode for sll is 0 0x is nop!
Loading Large Value Loading 32bit immediate takes 3 steps: Load (ori) 16bits (in right part of register) Shift left 16 bits (move to left half of register) Load (ori) 16bits (in right part of register)
Bit Isolation Getting particular bits out of pattern Strategy 1: – Shift to wipe out others Want to clear left 8 bits: Shift left 8 bits: Shift back right 8 bits:
Bit Isolation Getting particular bits out of pattern Strategy 1: – Shift to wipe out others Now want to isolate green five bits Shift right
Bit Isolation Getting particular bits out of pattern Strategy 2: – Masking : binary pattern showing bits to keep 0x00 : keep no bits 0x01 : keep bit 1 ( ) 0x04 : keep bit 3 ( ) 0x05 : keep bit 1 & 3 ( ) 0xF0 : keep bit 5-8 ( )
Using Masks AND with a mask 0's out unmasked portions: Mask Data AND result
Hex Mask F (1111) mask a whole hex digit 0x fMask0xfff x Data0x x AND result0x
Bit Isolation Getting particular bits out of pattern Strategy 2: – Masking : binary pattern showing bits to keep Want to keep just green bits Create mask… 0x01F AND
Bit Isolation Want to keep just green bits Create mask… 0x01F AND