# Logic Operations and Conditionals --- CS 130 // 2021-10-06 ## Administrivia - # Questions ## ...about anything? # Quiz 2 # Logical Operations ## Applications of Logical Operations - Suppose in Python I declare a `bool` type: ```py b = True ``` - How many bits of memory were just allocated? + An entire word! + `b` is a **memory address** that "points to" `True` ## Applications of Logical Expressions - Since a word can theoretically store 32 (or 64) bits, let's create a function to extract out individual bits ```c /// Treats the int arr as an array of bools /// and returns the bit at the given index int get_bool(int arr, int index) { return arr & (1 << index); } ``` - What do operators `&` and `<<` mean? ## Applications of Logical Expressions - `&` is the **logical and** operator - `<<` is the **shift left** operator - Then what is the following actually doing? ```c int get_bool(int arr, int index) { return arr & (1 << index); } ``` - "Zeroing out" all the bits except the one at `index` ## Logical Operations ```mips and $t0, $s0, $s1 # logical and (x & y) or $t0, $s0, $s1 # logical or (x | y) ``` ```mips # "Immediate" variants, similar to addi andi $t0, $s0, c ori $t0, $s0, c ``` ## Logical Operations ```mips sll $t0, $s0, c # shift left logical (s0 << c) srl $t0, $s0, c # shift right logical (s0 >> c) ``` --- - Shift uses the `shamt` region of the R-type format + `sll` `$t2`, `$s0`, `4` ![Shift left logical instruction](/teaching/2021f/cs130/assets/images/COD/sll-instruction.png) # Exercises ## Exercise 1 - Suppose that `$s0 = 7` - What is stored in `$t0` after executing: ```mips sll $t0, $s0, 3 ``` ## Exercise 2 - Suppose that `$s0 = 15` and `$s1 = 32` - What is stored in `$t0` after executing: ```mips and $t0, $s0, $s1 ``` - What is stored in `$t0` after executing: ```mips or $t0, $s0, $s1 ``` ## Exercise 3 - Consider the following 32-bit string + `00000010010010000100000000100000` - Is it an R-type or an I-type? + Since `op = 000000`, it is R-type - What type of R instruction is it? + Since `func = 100000`, it is `add` - What registers it is adding? + `add` `$8`, `$18`, `$8` + `add` `$t0`, `$s2`, `$t0` # Conditionals ## Conditionals - What four MIPS instructions were covered in the reading that help us facilitate decision making? - `beq` `$s0`, `$s1`, `label` + "Branch when equal" + If `$s0` is equal to `$s1`, then jump to `label` - `bne` `$s0`, `$s1`, `label` + "Branch when not equal" + If `$s0` does not equal `$s1`, then jump to `label` - `slt` `$t0`, `$s0`, `$s1` (`slti` variant) + "Set on less than" + `$t0` = 1 if `$s0` < `$s1` else 0 ## Compiling an `if` Statement ```c if (i == j) // $s0 <- i, $s1 <- j k = 1; // $s2 <- k // label Exit ``` --- ```mips bne $s0, $s1, Exit addi $s2, $zero, 1 Exit: ... ``` ```c if (i == j) // $s0 <- i, $s1 <- j k = 1; // $s2 <- k else k = 2; // label Exit ``` --- ```mips bne $s0, $s1, Else addi $s2, $zero, 1 j Exit Else: addi $s2, $zero, 2 Exit: ... ``` ```c if (i == j) // $s0 <- i, $s1 <- j k = 1; // $s2 <- k else if (i != g) k = 2; else k = 3; // label Exit ``` --- ```mips bne $s0, $s1, ElseIf addi $s2, $zero, 1 j Exit ElseIf: beq $s0, $s3, Else addi $s2, $zero, 2 j Exit Else: addi $s2, $zero, 3 Exit: ... ``` ## Compiling a `while` Statement ```c while (i != 10) // $s0 <- i { i++; } // label Done ``` --- ```mips addi $t0, $zero, 10 Loop: beq $s0, $t0, Done addi $s0, $s0, 1 j Loop Done: ... ``` ```c int sum = 0; for (int i = 0; i < 10; i++) { sum = sum + i; } // label Done ``` --- ```c int sum = 0; int i = 0; while (i < 10) { sum = sum + i; i++; } // label Done ``` ```c int sum = 0; // $s0 <- sum int i = 0; // $s1 <- i while (i < 10) { sum = sum + i; i++; } // label Done ``` --- ```mips addi $s0, $zero, 0 addi $s1, $zero, 0 Loop: slti $t0, $s1, 10 beq $t0, $zero, Done add $s0, $s0, $s1 addi $s1, $s1, 1 j Loop Done: ... ```