1 of 7

Lab 9

3/23 COMP 411

Shifting in MIPS

Connor & Michael

2 of 7

In this lab we will:

Understand how to combine shifts with AND

Review XOR

Quiz

3 of 7

Shift operation

  • Used to hone in on a window of bits that you care about
    • Usually the window you care about is the right most bit
  • Ex (assume 4 bit registers, in MIPS they are 32 bit):
    • 12 = 1100
    • 12 <<= 1
      • Result: 1000
    • 12 >>= 1
      • Result: 0110

4 of 7

AND operation

  • ANDing with 0 -> make all bits 0 / “masking”
  • ANDing with 1 -> Used to copy over the value of a bit into another variable or buffer
    • Ex:

int bit = 1;

int myVar = bit & 1;

(value of myVar is 1)

int bit = 0;

int myVar = bit & 1;

(value of myVar is 0)

5 of 7

Exercise 1: using Shift with AND

  • In this lab, integers are ALREADY represented as binary
    • You just have to tease the binary out using these two operations
  • Think of shift and AND as your tools:
    • Shift changes the bit you’re focused on
    • AND copies the value of a bit into another variable
  • In this lab, the challenge is to use these 2 tools to obtain the binary representation of an integer in C (part 1)

6 of 7

Exercise 2: using XOR and shift

  • Carefully understand how the algorithm works at each step
  • B = R4 XOR R2
    • Understand how XOR works
  • You will need to use your knowledge of exercise 1, and also append bits while shifting
  • Make sure to clearly understand C code, understand implementation

7 of 7

Other cool tricks w/ AND

  • Pulling out the bits you want:
    • Given 2 bytes: 11111111 10101010
    • If you just want the second byte, use (AND + a mask)
      • In this case, mask = 00000000 11111111
      • 11111111 10101010 AND mask = 00000000 10101010
  • Remainder from power of 2
    • Using (AND + shift), but with division
    • A way to copy the remaining bit after division
  • Remainder from power of 2
    • Using (AND + shift), but with division
    • How division is actually implemented in processor: