MIPS Logic & Shift
Bitwise Logic
• Bitwise operations : logical operations applied to each bit
• Bitwise OR:
Immediate Value
• Immediate ValueValue hard coded into instruction
Immediate Value
• Immediate ValueValue 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 instructionsori, andi, xoriinstruction $dest, $source, immediate
• Register-Register Instructionsor, and, xor, norinstruction $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
• ORILoad a value to register by ori $0 and value
Common Tricks
• ORClear a register by oring $0 with self
Common Tricks
• ORCopy register by or with $0
Common Tricks
• ORCombine non-overlapping patterns:
Common Tricks
• ANDKeep only specified bits byanding with 1's in desireddigits
Common Tricks
• NORDo 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• 0x00000000 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:1010 1011 1111 0110 1010 1011 1111 0110
• Shift left 8 bits:1111 0110 1010 1011 1111 0110 0000 0000
• Shift back right 8 bits:0000 0000 1111 0110 1010 1011 1111 0110
Bit Isolation
• Getting particular bits out of pattern• Strategy 1:– Shift to wipe out others
• Now want to isolate green five bits0000 0000 1111 0110 1010 1011 1111 0110
• Shift right 190000 0000 0000 0000 0000 0000 0001 1110
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 (0000 0001)
0x04 : keep bit 3 (0000 0100)
0x05 : keep bit 1 & 3 (0000 0101)
0xF0 : keep bit 5-8 (1111 0000)
Using Masks
• AND with a mask 0's out unmasked portions:
0000 1111 Mask 0011 00000101 1011 Data 0101 10110000 1011 AND result 0001 1011
Hex Mask
• F (1111) mask a whole hex digit
0x0000000f Mask 0xfff000000x12345678 Data 0x123456780x00000008 AND result 0x12300000
Bit Isolation
• Getting particular bits out of pattern• Strategy 2:– Masking : binary pattern showing bits to keep
• Want to keep just green bits1010 1011 1111 0110 1010 1011 1010 0110
• Create mask… 0x01F00000 0000 0000 0000 0000 0001 1111 0000
• AND0000 0000 0000 0000 0000 0001 1010 0000
Bit Isolation
• Want to keep just green bits1010 1011 1111 0110 1010 1011 1010 0110
• Create mask… 0x01F00000 0000 0000 0000 0000 0001 1111 0000
• AND0000 0000 0000 0000 0000 0001 1010 0000