comp sci 251 -- bitwise operations 1 ch. 7 bitwise operations

14
Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Upload: annis-henderson

Post on 21-Dec-2015

238 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations1

Ch. 7 Bitwise Operations

Page 2: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations2

Bitwise operations

Shift and rotate– Move bits around within a register– Fast implementation of certain arithmetic

Logical operations (and, or, xor, nor, not)– Combine two bit patterns– Affect selected bits in a word

Page 3: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations3

Shift left instructions

Bits move left Rightmost bits are cleared

sll Rdest, Rsrc1, immediatesllv Rdest, Rsrc1, Rsrc2

Bits from Rsrc1 shifted left by immediate or Rsrc2

Result Rdest (Rsrc1 unchanged)

Sslv ssl variable

1 0 0 1 1 1 0 1

0

Page 4: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations4

Shift left and multiplication

sll $t0, $t0, 1 # multiply by 2

sll $t0, $t0, 2 # multiply by 4

sll $t0, $t0, 3 # multiply by 8

Shift left n multiplies by 2n

Page 5: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations5

Practical use of shift left

int x[50];

x[3] = -1;

.textli $t0, -1li $t1, 3sll $t1, $t1, 2sw $t0, x($t1)

.data

.align 2x:.space 200

Page 6: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations6

Shift right instructions

Bits move right Leftmost bits

– Cleared (logical shift)– Retain original leftmost bit value

(arithmetic shift)

srl Rdest, Rsrc1, immediatesrlv Rdest, Rsrc1, Rsrc2sra Rdest, Rsrc1, immediatesrav Rdest, Rsrc1, Rsrc2

1 0 0 1 1 1 0 1

1 0 0 1 1 1 0 1

0Logical shift right

Arithmetic shift right

Page 7: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations7

Shift right and division

srl $t0, $t0, 1 # divide by 2

srl $t0, $t0, 2 # divide by 4

Shift right logical n divides by 2n (unsigned) Shift right arithmetic n divides by 2n (signed)

Page 8: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations8

Rotate instructions

Similar to shift Bits pushed out wrap around

rol Rdest, Rsrc1, Src2

ror Rdest, Rsrc1, Src2

Bits from Rsrc1 rotated by Src2 Result Rdest

(Rsrc1 unchanged)

1 0 0 1 1 1 0 1

Rotate left

Page 9: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations9

Logical instructions

x y x and y x or y x xor y x nor y not x

0 0 0 0 0 1 1

0 1 0 1 1 0 1

1 0 0 1 1 0 0

1 1 1 1 0 0 0

Page 10: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations10

Logical instructions

and Rdest, Rsrc1, Src2or Rdest, Rsrc1, Src2xor Rdest, Rsrc1, Src2nor Rdest, Rsrc1, Src2not Rdest, Rsrc

Corresponding bits of Rsrc1 and Src2 are combined w/ logical operator

Resulting word Rdest

Page 11: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations11

Logical Instructions

Name Format Operation

And R and Rdest, Rsrc1, Src2

R[rd] = R[rs] & R[rt]

And immediate I

andi R[rt] = R[rs] & ZeroExtImm

Page 12: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Comp Sci 251 -- Bitwise operations12

Uses of shift/logical instructions

Clear: AND with 0 Set: OR with 1 Toggle: XOR with 1

Applications: Hashing Data compression Checksums Graphics

Exercises: Clear low order 4 bits of

$t0

Set the most significant bit of $t1

Count number of "1" bits in $t2

Page 13: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Examples in /shared/huen/251/ch07_bitwise

bit2.a – extracts three fields of different widths from a 32 bit word

xor.a – XOR operation. Use XSPIM or PCSPIM to see how the bit pattern changes in registers.

xori.a – another XOR operation example.

Comp Sci 251 -- Bitwise operations13

Page 14: Comp Sci 251 -- Bitwise operations 1 Ch. 7 Bitwise Operations

Examples in /shared/huen/251/ch07_shift

dec.a - asks user for decimal number, converts to ASCII string, print the result.

hex.a – asks user for decimal number, converts to hexadecimal, print the result

logic1.a – sums the elements that are NOOT multiples of 4 in an array.

Oct.a - asks user for decimal number, converts to octal, print the result

Comp Sci 251 -- Bitwise operations14