# var | register # ---------------- # f | $s0 # g | $s1 # h | $s2 # i | $s3 # j | $s4 # p | $s5 # q | $s6 # Convert this C statement to MIPS assembly # f = *p; # which is equivalent to: # f = p[0]; lw $s0, 0($s5) # Convert this C statement to MIPS assembly # *q = f + g; add $t1, $s0, $s1 sw $t1, 0($s6) # Convert this similar C statement: # *q = f + 5; addi $t1, $s0, 5 # Convert this C statement to MIPS assembly # *p = *p + 1; lw $t0, 0($s5) # Load the value pointed to by p addi $t0, $t0, 1 # Increment the loaded value sw $t0, 0($s5) # Store the incremented value at address p # Convert this C statement to MIPS assembly # p = q; add $s5, $zero, $s6 --- # Convert this block of C code to MIPS assembly # while(1) { # f = f + *p; # } loop: lw $t0, 0($s5) add $s0, $s0, $t0 j loop # Convert this block of C code to MIPS assembly # f = 1 # g = 1 # while(1) { # int temp = f + g; # f = g; # g = temp; # } li $s0, 1 li $s1, 1 loop: add $t0, $s0, $s1 add $s0, $zero, $s1 add $s1, $zero, $t0 j loop --- # What constructs do we use to make decisions in C? # - if/else if/else blocks # - while loops # - for loops # - do-while loops # - switch/case blocks # - (other languages: try/catch blocks) # Continue, break, and goto don't make decisions, but do change # control flow. # What instructions do we use to make decisions in MIPS? # - bne: compare two values, and jump if they are different # - beq: compare two values, and jump if they are the same # - slt: compare two values to see if one is less than the other # Instructions that change control flow, but don't "make a decision" # - j: changes control flow, but doesn't depend on a value # --- # Convert these C statements to MIPS assembly # bool f = g < h; slt $s0, $s1, $s2 # bool f = g <= h; # we can rewrite this as: # bool f = !(h < g); slt $t0, $s2, $s1 # temp = h < g nor $s0, $t0, $zero # f = !temp # this isn't as nice, but it works: bne $s1, $s2, else li $s0, 1 j end else: slt $s0, $s1, $s2 end: ... # we prefer the one that does not use branches and jumps # bool f = g > h; # we can rewrite this as: # bool f = h < g; slt $s0, $s2, $s1 # bool f = g >= h # we can rewrite this as: # bool f = !(g < h) slt $t0, $s1, $s2 # temp = g < h nor $s0, $t0, $zero # f = !temp --- # Convert this block of C code to MIPS assembly # if (f == g) { # h = 1; # } else { # h = 2; # } bne $s0, $s1, else addi $s2, $zero, 1 j exit else: li $s2, 2 exit: # An alternative: beq $s0, $s1, if li $s2, 2 j exit if: li $s2, 1 exit: # What if the `if` conditional was "f != g"? How would the assembly change? # Use the same structure, except change `bne` to `beq` --- # Convert this C while loop to MIPS assembly. # while (f != g) { # f++; # } loop: beq $s0, $s1, exit addi $s0, $s0, 1 j loop exit: # Convert this similar C block as well. # Try to change as little as possible from your previous translation. # Do not use ble, blt, bge, or bgt. # while (f < g) { # f++; # } # We'll come back to this one on Wednesday