ece232 hardware organization and design
play

ECE232: Hardware Organization and Design Lecture 4: Logic Operations - PowerPoint PPT Presentation

ECE232: Hardware Organization and Design Lecture 4: Logic Operations and Introduction to Conditionals Adapted from Computer Organization and Design , Patterson & Hennessy, UCB Overview Previously examined arithmetic operations R and


  1. ECE232: Hardware Organization and Design Lecture 4: Logic Operations and Introduction to Conditionals Adapted from Computer Organization and Design , Patterson & Hennessy, UCB

  2. Overview  Previously examined arithmetic operations • R and I instructions formats  Microprocessors also require logic operations • NAND, OR, AND, etc  Operations performed in the ALU • Similar to logic operations  Big picture • Logic and arithmetic operations are fundamental to computer operation  Introduction to conditional operations • How computers make choices ECE232: Logic Operations + Intro to Conditionals 2

  3. Typical Operations (little change since 1960) Data Movement Load (from memory) Store (to memory) register-to-register move input (from I/O device) output (to I/O device) push, pop (to/from stack) Arithmetic integer (binary + decimal) or FP Add, Subtract, Multiply, Divide Shift shift left/right, rotate left/right Logical not, and, or, set, clear Control (Jump/Branch) unconditional, conditional Subroutine Linkage call, return Interrupt trap, return Graphics (MMX) parallel subword ops (e.g., 4-16 bit add) ECE232: Logic Operations + Intro to Conditionals 3

  4. MIPS Logical Instructions Instruction Example Meaning Comment and and $1,$2,$3 $1 = $2 & $3 3 reg. operands; Logical AND or or $1,$2,$3 $1 = $2 | $3 3 reg. operands; Logical OR $1 = $2  $3 3 operands; Logical XOR xor xor $1,$2,$3 and immediate andi $1,$2,10 $1 = $2 & 10 Logical AND reg,constant or immediate ori $1,$2,10 $1 = $2 | 10 Logical OR reg, constant $1 = $2  10 xor immediate xori $1, $2,10 Logical XOR reg, constant shift left logical sll $1,$2,10 $1 = $2 << 10 Shift left by constant shift right logical srl $1,$2,10 $1 = $2 >> 10 Shift right by constant ECE232: Logic Operations + Intro to Conditionals 4

  5. Logical Operations Instructions for bitwise manipulation  Operation C Java MIPS Shift left << << sll Shift right >> >>> srl Bitwise AND & & and, andi Bitwise OR | | or, ori Bitwise NOT ~ ~ nor Useful for extracting and inserting groups of bits in a word  ECE232: Logic Operations + Intro to Conditionals 5

  6. Shift Operations op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits shamt: how many positions to shift  Shift left logical  Shift left and fill with 0 bits • sll by i bits multiplies by 2 i • Shift right logical  Shift right and fill with 0 bits • srl by i bits divides by 2 i (unsigned only) • ECE232: Logic Operations + Intro to Conditionals 6

  7. 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 $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0000 1100 0000 0000 ECE232: Logic Operations + Intro to Conditionals 7

  8. 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 ECE232: Logic Operations + Intro to Conditionals 8

  9. NOT Operations Useful to invert bits in a word  Change 0 to 1, and 1 to 0 • MIPS has NOR 3-operand instruction  a NOR b == NOT ( a OR b ) • nor $t0, $t1, $zero Register 0: always read as zero $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 1111 1111 1111 1111 1100 0011 1111 1111 ECE232: Logic Operations + Intro to Conditionals 9

  10. MIPS Registers and Usage Name Register number Usage 0 the constant value 0 $zero 1 reserved for assembler $at 2-3 values for results and expression evaluation $v0-$v1 4-7 arguments $a0-$a3 8-15 temporary registers $t0-$t7 16-23 saved registers $s0-$s7 24-25 more temporary registers $t8-$t9 26-27 reserved for Operating System kernel $k0-$k1 28 global pointer $gp 29 stack pointer $sp 30 frame pointer $fp 31 return address $ra ECE232: Logic Operations + Intro to Conditionals 10

  11. Conditional Operations Branch to a labeled instruction if a condition is true  Otherwise, continue sequentially •  beq rs, rt, L1 if (rs == rt) branch to instruction labeled L1; •  bne rs, rt, L1 if (rs != rt) branch to instruction labeled L1; •  j L1 unconditional jump to instruction labeled L1 • ECE232: Logic Operations + Intro to Conditionals 11

  12. MIPS Conditional Branch Instructions Conditional branches allow decision making  beq R1, R2, LABEL if R1==R2 goto LABEL bne R3, R4, LABEL if R3!=R4 goto LABEL beq $s1, $s2, 25 if ($s1==$s2) PC = PC + 4 + 4*25 else PC = PC + 4 { Example  Address of next if (i==j) goto L1; C Code sequential instruction f = g + h; Offset in bytes L1: f = f - i; beq $s3, $s4, L1 Assembly  add $s0, $s1, $s2 L1: sub $s0, $s0, $s3 ECE232: Logic Operations + Intro to Conditionals 12

  13. Binary Representation - Branch 6 bits 5 bits 5 bits 16 bits op rs rt offset Branch instructions use I-Format  offset is added to PC when branch is taken  beq r0, r1, offset Conversion to has the effect: byte offset if (r0==r1) pc = pc + 4 + (offset << 2) else pc = pc + 4; Offset is specified in instruction words (why?)  What is the range of the branch target addresses?  ECE232: Logic Operations + Intro to Conditionals 13

  14. Binary Representation - Jump 6 bits 26 bits op address Jump Instruction uses J-Format ( op=2 )  What happens during execution?  PC = PC[31:28] : (IR[25:0] << 2) Conversion to Concatenate upper 4 bits byte offset of PC to form complete 32-bit address ECE232: Logic Operations + Intro to Conditionals 14

  15. if statement if ( condition ) { statements } # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, if_end_label # MIPS code for the statements if_end_label : ECE232: Logic Operations + Intro to Conditionals 15

  16. Compiling If Statements C code:  if (i==j) f = g+h; else f = g-h; • f, g, … in $s0, $s1, … Compiled MIPS code:  bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: … Assembler calculates addresses ECE232: Logic Operations + Intro to Conditionals 16

  17. if else statement if ( condition ) { if-statements } else { else-statements } # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, else_label # MIPS code for the if-statements j if_end_label else_label : # MIPS code for the else-statements if_end_label : ECE232: Logic Operations + Intro to Conditionals 17

  18. Summary  We have now covered the major “operation” instructions  Most programs require lots of arithmetic and logic operations • Can use registers or immediate operands • Be sure to keep instruction formats in mind  Control operations • Modify the flow of the program  What’s next? • Control operations – programs require decisions • Implementing control • Implementing methods/subroutines ECE232: Logic Operations + Intro to Conditionals 18

Recommend


More recommend