mips logic & shift. bitwise logic bitwise operations : logical operations applied to each bit...

25
MIPS Logic & Shift

Upload: barnard-marsh

Post on 05-Jan-2016

226 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

MIPS Logic & Shift

Page 2: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Bitwise Logic

• Bitwise operations : logical operations applied to each bit

• Bitwise OR:

Page 3: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Immediate Value

• Immediate ValueValue hard coded into instruction

Page 4: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Immediate Value

• Immediate ValueValue hard coded into instruction

• 16 bits available to store immediate:

Page 5: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Register

• Registers = 32 bits, Immediate 16 bits

• Must extend immediate

Page 6: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Max Immediate

• Immediate value can't require 17+ bits

• Unless you use Pseudo instructions– Must be UNCHECKED for now

Page 7: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

OR vs ORI

• OR $6, $5, $4

• ORI $6, $0, 0xffff

Page 8: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Logical Ops to Know

• Immediate instructionsori, andi, xoriinstruction $dest, $source, immediate

• Register-Register Instructionsor, and, xor, norinstruction $dest, $source1, $source2

Page 9: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

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

Page 10: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Common Tricks

• ORILoad a value to register by ori $0 and value

Page 11: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Common Tricks

• ORClear a register by oring $0 with self

Page 12: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Common Tricks

• ORCopy register by or with $0

Page 13: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Common Tricks

• ORCombine non-overlapping patterns:

Page 14: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Common Tricks

• ANDKeep only specified bits byanding with 1's in desireddigits

Page 15: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Common Tricks

• NORDo NOT by NOR with $0

Page 16: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Logical Shift

• Logical Shifts– Add 0's to fill empty space

• sll, srl instructions:instruction $dest, $source, number of bits

Page 17: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

NOP

• NOP : No operation– Done with sll $0, $0, $0

– Opcode for sll is 0• 0x00000000 is nop!

Page 18: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

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)

Page 19: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

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

Page 20: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

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

Page 21: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

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)

Page 22: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

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

Page 23: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

Hex Mask

• F (1111) mask a whole hex digit

0x0000000f Mask 0xfff000000x12345678 Data 0x123456780x00000008 AND result 0x12300000

Page 24: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

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

Page 25: MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:

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