230 likes | 314 Views
Chapter 2. LOGICAL OPERATIONS (Instructions: Language of the Computer Part IV). Review:. MIPS Assembly Language: Registers replace C++ variables One Instruction (simple operation) per line Design Principles Simplicity favors regularity Smaller is faster Make the common case fast
E N D
Chapter 2 LOGICAL OPERATIONS (Instructions: Language of the Computer Part IV)
Review: • MIPS Assembly Language: • Registers replace C++ variables • One Instruction (simple operation) per line • Design Principles • Simplicity favors regularity • Smaller is faster • Make the common case fast • Good design demands good compromises • Instruction Formats • R-format, I-format Florida A & M University - Department of Computer and Information Sciences
R-format Instructions opcode rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Decimal values
I-format Instructions opcode rs rt constant or address 6 bits 5 bits 5 bits 16 bits Decimal values
Variable Array Index a <= H[b] – d; 1. Compute the address of H[b] <= 4b + base address Add 4b to the base address to get address of H[b] 2. Read from memory location H[b] into temporary register 1 3. Subtract d from the contents of temporary register and place in register of a lw $s3,d # value of d la $s1, H # base reg for H. lw $t1, b # subscript b sla $t1,$t1,2 # offset = 4*b add $t2,$s1,$t1 # $t2 holds addr(H[b]) lw $t1, 0($t2) # $t1 H[b] sub $t3, $t1, $s3 # H[b] – d sw $t3, a
Variable Array Index H[a] <= H[b] + d; lw $s3,d # value of d la $s1, H # base reg for H. lw $t0, a # subscript b lw $t1, b # subscript b sla $t0,$t0,2 # offset = 4*a sla $t1,$t1,2 # offset = 4*b add $t2,$s1,$t0 # $t2 holds addr(H[a]) add $t3,$s1,$t1 # $t3 holds addr(H[b]) lw $t4, 0($t3) # $t4 H[b] add $t5, $t4, $s3 # H[b] + d sw $t5, 0($t2)
Logical Operations Up until now, we’ve done arithmetic (add, sub,addi ) and memory access (lw and sw) Arithmetic/load/store instructions view contents of register as a single quantity (such as a signed or unsigned integer) New Perspective: View register as 32 raw bits rather than as a single 32-bit number Since registers are composed of 32-bit words, we may want to access individual bits (or fields of bits) within the word. Introduce two new classes of instructions: Shift Operations Logical Operations
Logical Operations Instructions for bitwise manipulation • Useful for extracting and inserting groups of bits in a word Florida A & M University - Department of Computer and Information Sciences
Shift Operations Move all bits in a word to the left or right Example: shift right by eight bits 1001 0110 0011 0101 0101 0110 0111 1000 Example: shift left by eight bits 1001 0110 0011 0101 0101 0110 0111 1000 0011 0101 0101 0110 0111 10000000 0000 • 0000 0000 1001 0110 0011 0101 0101 0110
Shift Instruction Format Instruction Syntax: 1 2, 3, 4 where 1 : operation name 2 : register that will receive value 3 : register containing operand 4 : shift amount (constant < 32) Florida A & M University - Department of Computer and Information Sciences
Shift Operations shamt: how many positions to shift Shift left logical Shift left and fill with 0 bits sll by i bits multiplies by 2i Shift right logical Shift right and fill with 0 bits srl by i bits divides by 2i (unsigned only) op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Florida A & M University - Department of Computer and Information Sciences
R-format Instructions opcode rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Decimal values
Logical Operations Basic logical operators: AND: outputs 1 only if both input bits are 1 OR: outputs 1 if at least one input bit is 1 NOT: outputs 1 if input bit is 0, 0 if input bit is 1 Bitwise bit k of output is produced by the corresponding bits in the operands . C++: bitwise AND is & (e.g., z <= x & y;) C++: bitwise OR is | (e.g., z <= x | y;)
Logical Operations Instruction Syntax: 1 2, 3, 4 where 1 : operation name 2 : register that will receive value 3 : register containing operand 4 : register containing second operand or an immediate (numerical constant)
Logical Operations MIPS logical instructions: and, or: Both of these expect the third argument to be a register andi, ori: Both of these expect the third argument to be an immediate nor: (not or) used instead of not in keeping with the two operand format; equivalent to not if one operand is zero 0000 nor 1011 = ^(0+1) ^(0+0) ^(0+1) ^(0+1) = 0100
AND Operation Basic rules for bit and: 0 and b 0 1 and b b This can be used to create a mask. Example: 1011 0110 1010 0100 0011 1101 1001 1010 0000 0000 0000 0000 0000 1111 1111 1111 The result: 0000 0000 0000 0000 0000 1101 1001 1010 mask: MASKING highlights certain bits, cancels others.
AND Operations Useful to mask bits in a word Select some bits, clear others to 0 and $t0, $t1, $t2 $t2 0000 0000 0000 0000 0000 1101 1100 0000 mask: $t1 0000 0000 0000 0000 0011 11000000 0000 $t0 0000 0000 0000 0000 0000 1100 0000 0000 Florida A & M University - Department of Computer and Information Sciences
Masking with AND The second bit string of each example is called a mask. It is used to isolate certain bits of the first bit string by masking out the rest of the string (e.g. setting it to all 0s). Thus, the and operator can be used to set certain portions of a bitstring to 0s, while leaving the rest alone. In particular, if the first bitstring in the first example were in $t0, then the following instruction would mask it: andi $t0,$t0,0xFFF
OR Operations Basic rules for bit or: 0 or b b 1 or b 1 X or can be used to force certain bits to 1s. Example: ori $t0, $t0, 0xFFFF Before: $t0 = 0x12345678 (hex) Operation: 0xFFFF (hex) 0x1234FFFF (hex) or immediate operates on 16 low-order bits.
OR Operations Useful to include bits in a word Set some bits to 1, leave others unchanged or $t0, $t1, $t2 $t2 0000 0000 0000 0000 0000 1101 1100 0000 $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0011 1101 1100 0000 Florida A & M University - Department of Computer and Information Sciences
NOT Operations Basic rules for bit not: not 0 1 not 1 0 Negates, inverts or complements bits MIPS has NOR 3-operand instruction a NOR b NOT ( a OR b ) nor $t0, $t1, $zero Register 0: always read as zero $zero 0000 0000 0000 0000 0000 0000 0000 0000 $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 1111 1111 1111 1111 1100 0011 1111 1111 Florida A & M University - Department of Computer and Information Sciences
R-format Instructions opcode rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Decimal values
I-format Instructions opcode rs rt constant 6 bits 5 bits 5 bits 16 bits Decimal values