1 / 23

Chapter 2

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

Download Presentation

Chapter 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 LOGICAL OPERATIONS (Instructions: Language of the Computer Part IV)

  2. 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

  3. R-format Instructions opcode rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Decimal values

  4. I-format Instructions opcode rs rt constant or address 6 bits 5 bits 5 bits 16 bits Decimal values

  5. 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

  6. 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)

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. R-format Instructions opcode rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Decimal values

  13. 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;)

  14. 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)

  15. 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

  16. 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.

  17. 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

  18. 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

  19. 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.

  20. 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

  21. 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

  22. R-format Instructions opcode rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Decimal values

  23. I-format Instructions opcode rs rt constant 6 bits 5 bits 5 bits 16 bits Decimal values

More Related