ece232 hardware organization and design
play

ECE232: Hardware Organization and Design Lecture 5: MIPs - PowerPoint PPT Presentation

ECE232: Hardware Organization and Design Lecture 5: MIPs Decision-Making Instructions Adapted from Computer Organization and Design , Patterson & Hennessy, UCB Overview Computers need to make decisions Microprocessors support


  1. ECE232: Hardware Organization and Design Lecture 5: MIPs Decision-Making Instructions Adapted from Computer Organization and Design , Patterson & Hennessy, UCB

  2. Overview  Computers need to make decisions • Microprocessors support “conditional” operations  Operations typically called branch operations  Connections with common Java and C language constructs • If, else, switch, for loops, do loops  Conditional instructions have a specific format ECE232: Decision Making Instructions 2

  3. Example: Compiling C if-then-else Example  if (i==j) f = g + h; C Code else f = g - h; bne $s3, $s4, Else Assembly add $s0, $s1, $s2 j Exit; # new: unconditional jump Else: sub $s0, $s1, $s2 Exit: New Instruction: Unconditional jump  j LABEL # goto Label ECE232: Decision Making Instructions 3

  4. Compiling Loop Statements C code:  while (save[i] == k) i += 1; • i in $s3, k in $s5, address of save in $s6 Compiled MIPS code:  Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: … ECE232: Decision Making Instructions 4

  5. while statement while ( condition ) { statements } while_start_label : # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, while_end_label # MIPS code for the statements j while_start_label while_end_label : ECE232: Decision Making Instructions 5

  6. do-while statement do { statements } while ( condition ); do_start_label : # MIPS code for the statements do_cond_label : # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, do_end_label j do_start_label do_end_label : ECE232: Decision Making Instructions 6

  7. for loop for ( init ; condition ; incr ) { statements } # MIPS code for the init expression for_start_label : # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, for_end_label # MIPS code for the statements # MIPS code for the incr expression j for_start_label for_end_label : ECE232: Decision Making Instructions 7

  8. Basic Blocks A basic block is a sequence of instructions with  • No embedded branches (except at end) • No branch targets (except at beginning) A compiler identifies basic  blocks for optimization An advanced processor can  accelerate execution of basic blocks ECE232: Decision Making Instructions 8

  9. More Conditional Operations Set result to 1 if a condition is true  • Otherwise, set to 0  slt rd, rs, rt • if (rs < rt) rd = 1; else rd = 0;  slti rt, rs, constant • if (rs < constant) rt = 1; else rt = 0; Use in combination with beq , bne  slt $t0, $s1, $s2 # if ($s1 < $s2) bne $t0, $zero, L # branch to L ECE232: Decision Making Instructions 9

  10. Comparisons - What about <,  , >,  ? bne, beq provide equality comparison  slt ( s et on l ess t han) provides magnitude comparison  slt $t0,$s3,$s4 # if $s3<$s4 $t0=1; # else $t0=0; Combine with bne or beq to branch:  slt $t0,$s3,$s4 # if (a<b) condition register bne $t0,$zero,Less # goto Less; Why not include a blt instruction in hardware?  • Supporting in hardware would lower performance • Assembler provides this function if desired (by generating the two instructions) ECE232: Decision Making Instructions 10

  11. switch statement switch ( expr ) { case const1 : statement1 case const2 : statement2 ... case constN : statementN default: default-statement } ECE232: Decision Making Instructions 11

  12. MIPS code for switch statement # MIPS code for $t0= expr beq $t0, const1 , switch_label_1 beq $t0, const2 , switch_label_2 ... beq $t0, constN , switch_label_N j switch_default switch_label_1 : # MIPS code to compute statement1 switch_label_2 : # MIPS code to compute statement2 ... switch_default : # MIPS code to compute default-statement switch_end_label : ECE232: Decision Making Instructions 12

  13. Switch Example switch (i) { //Assume i is in $s1 and j is in $s2; case 0: j = 3; break; case 1: j = 5; break; case 2: ; case 3: j = 11; break; case 4: j = 13; break; default: j = 17; } main: add $t0, $zero, $zero # $t0 = 0, temp. variable beq $t0, $s1, case0 # go to case0 addi $t0, $t0, 1 # $t0 = 1 beq $t0, $s1, case1 # go to case1 addi $t0, $t0, 1 # $t0 = 2 beq $t0, $s1, case2 # go to case2 addi $t0, $t0, 1 # $t0 = 3 beq $t0, $s1, case3 # go to case3 addi $t0, $t0, 1 # $t0 = 4 beq $t0, $s1, case4 # go to case4 j default # go to default case case0: addi $s2, $zero, 3 # j = 3 j finish # exit switch block ECE232: Decision Making Instructions 13

  14. Unconditional branches Unconditional branch:  j L1 jr $s5 (useful for large case statements and big jumps) ECE232: Decision Making Instructions 14

  15. Example 2 Convert to assembly:  while (save[i] == k)  Loop: sll $t1, $s3, 2 i += 1; add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit i and k are in $s3 and $s5  addi $s3, $s3, 1 and j Loop base of array save[] is in Exit:  $s6 ECE232: Decision Making Instructions 15

  16. SPIM Example switch (i) { //Assume i is in $s1 and j is in $s2; case 0: j = 3; break; case 1: j = 5; break; case 2: ; case 3: j = 11; break; case 4: j = 13; break; default: j = 17; } main: add $t0, $zero, $zero # $t0 = 0, temp. variable used in switch beq $t0, $s1, case0 # go to case0 addi $t0, $t0, 1 # $t0 = 1 beq $t0, $s1, case1 # go to case1 addi $t0, $t0, 1 # $t0 = 2 beq $t0, $s1, case2 # go to case2 addi $t0, $t0, 1 # $t0 = 3 beq $t0, $s1, case3 # go to case3 addi $t0, $t0, 1 # $t0 = 4 beq $t0, $s1, case4 # go to case4 j deflt # go to default case case0: addi $s2, $zero, 3 # j = 3 j fin # exit switch block ECE232: Decision Making Instructions 16

  17. Summary  Conditional operations affect program flow based on data values  Microprocessor makes decision based on results of arithmetic and logic operation  Know the difference between branch and jump  Useful for C/Java loops, if-else, and switch statements ECE232: Decision Making Instructions 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend