CS 230 – Introduction to Computers and Computer Systems Lecture 8 – Multiplication, Division, and Conditional Execution CS 230 - Spring 2020 2-1
Multiplication and Division special locations hi and lo mult $s, $t multiply registers s and t place result in hi : lo in CS230 we can ignore hi for multiplication only div $s, $t divide register s by t place integer quotient in lo , remainder in hi hi is very useful here CS 230 - Spring 2020 2-2
Multiplication and Division Results mfhi $d copy contents of hi to register d mflo $d copy contents of lo to register d CS 230 - Spring 2020 2-3
Example addi $2, $0, -3 mult $2, $2 mflo $3 jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-4
Example addi $2, $0, -3 mult $2, $2 $0 $1 $2 $3 $4 $5 … ? … mflo $3 0 ? ? ? ? jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-5
Example addi $2, $0, -3 mult $2, $2 $0 $1 $2 $3 $4 $5 … ? … mflo $3 0 ? -3 ? ? jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-6
Example addi $2, $0, -3 lo = 9 mult $2, $2 $0 $1 $2 $3 $4 $5 … ? … mflo $3 0 ? -3 ? ? jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-7
Example addi $2, $0, -3 lo = 9 mult $2, $2 $0 $1 $2 $3 $4 $5 … ? … mflo $3 0 ? -3 9 ? jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-8
Example addi $2, $0, -3 lo = 9 mult $2, $2 $0 $1 $2 $3 $4 $5 … ? … mflo $3 0 ? -3 9 ? jr $31 What is the value in register $3 at the end of this program? • $3 = 9 10 CS 230 - Spring 2020 2-9
Try it Yourself addi $2, $0, 11 addi $4, $0, 5 div $2, $4 mflo $3 jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-10
Try it Yourself addi $2, $0, 11 addi $4, $0, 5 $0 $1 $2 $3 $4 $5 … ? … div $2, $4 0 ? ? ? ? mflo $3 jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-11
Try it Yourself addi $2, $0, 11 addi $4, $0, 5 $0 $1 $2 $3 $4 $5 … ? … div $2, $4 0 ? 11 ? ? mflo $3 jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-12
Try it Yourself addi $2, $0, 11 addi $4, $0, 5 $0 $1 $2 $3 $4 $5 … ? … div $2, $4 0 ? 11 ? 5 mflo $3 jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-13
Try it Yourself addi $2, $0, 11 hi = 1 lo = 2 addi $4, $0, 5 $0 $1 $2 $3 $4 $5 … ? … div $2, $4 0 ? 11 ? 5 mflo $3 jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-14
Try it Yourself addi $2, $0, 11 hi = 1 lo = 2 addi $4, $0, 5 $0 $1 $2 $3 $4 $5 … ? … div $2, $4 0 ? 11 2 5 mflo $3 jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-15
Try it Yourself addi $2, $0, 11 hi = 1 lo = 2 addi $4, $0, 5 $0 $1 $2 $3 $4 $5 … ? … div $2, $4 0 ? 11 2 5 mflo $3 jr $31 What is the value in register $3 at the end of this program? • $3 = 2 10 CS 230 - Spring 2020 2-16
Conditional Execution Computation dependent on intermediate results otherwise only linear number crunching CS 230 - Spring 2020 2-17
Conditional Branch beq $s, $t, i compare registers s and t if equal , skip i instructions i can be negative bne $s, $t, i compare registers s and t if not equal , skip i instructions i can be negative CS 230 - Spring 2020 2-18
Branch Labels Use a label instead of i leave computation of i to the assembler (binasm) example label here called loop addi $1, $0, 10 loop: addi $1, $1, -1 bne $1, $0, loop jr $31 in branch instructions we can write loop instead of -2 binasm will turn bne $1, $0, loop into bne $1, $0, -2 CS 230 - Spring 2020 2-19
Program Counter (PC) MIPS word size is 32-bits therefore each instruction is 32-bits = 4 bytes long Program counter = address of current instruction the dashed line from examples we’ll talk more about addresses later always incremented by 4 at each instruction since instructions are 4 bytes, this moves to next instruction beq and bne do PC=PC+( i *4) when condition met CS 230 - Spring 2019 2-20
Example addi $2, $0, 10 addi $3, $0, 0 x: add $3, $3, $2 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-21
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? ? ? ? PC = 0x54 addi $2, $2, -1 (we don’t know the value of PC here, but lets pretend for this bne $2, $0, x example that it starts at 0x54) jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-22
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 10 ? ? PC = 0x58 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-23
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 10 0 ? PC = 0x5C addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-24
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 10 10 ? PC = 0x60 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-25
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 9 10 ? PC = 0x64 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-26
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 9 10 ? PC = 0x5C addi $2, $2, -1 PC = 0x64 + 4 10 + (4 10 * -3 10 ) bne $2, $0, x (-3) jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-27
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 9 19 ? PC = 0x60 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-28
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 8 19 ? PC = 0x64 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-29
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 8 19 ? PC = 0x5C addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-30
Example - Skip to $2=1 addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 1 55 ? PC = 0x60 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-31
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 0 55 ? PC = 0x64 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-32
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 0 55 ? PC = 0x68 addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-33
Example addi $2, $0, 10 addi $3, $0, 0 $0 $1 $2 $3 $4 $5 … ? … x: add $3, $3, $2 0 ? 0 55 ? PC = 0x6C addi $2, $2, -1 bne $2, $0, x jr $31 What is the value in register $3 at the end of this program? $3 = 55 10 CS 230 - Spring 2020 2-34
Try it Yourself addi $3, $0, 10 sub $3, $3, $3 beq $3, $0, tt addi $3, $0, 37 tt: jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-35
Try it Yourself addi $3, $0, 10 sub $3, $3, $3 $0 $1 $2 $3 $4 $5 … ? … beq $3, $0, tt 0 ? ? ? ? PC = ? + 0 10 addi $3, $0, 37 tt: jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-36
Try it Yourself addi $3, $0, 10 sub $3, $3, $3 $0 $1 $2 $3 $4 $5 … ? … beq $3, $0, tt 0 ? ? 10 ? PC = ? + 4 10 addi $3, $0, 37 tt: jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-37
Try it Yourself addi $3, $0, 10 sub $3, $3, $3 $0 $1 $2 $3 $4 $5 … ? … beq $3, $0, tt 0 ? ? 0 ? PC = ? + 8 10 addi $3, $0, 37 tt: jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-38
Try it Yourself addi $3, $0, 10 sub $3, $3, $3 $0 $1 $2 $3 $4 $5 … ? … beq $3, $0, tt 0 ? ? 0 ? (1) PC = ? + 16 10 addi $3, $0, 37 PC = (? + 8 10 ) + 4 10 + (4 10 * 1 10 ) tt: jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-39
Try it Yourself addi $3, $0, 10 sub $3, $3, $3 $0 $1 $2 $3 $4 $5 … ? … beq $3, $0, tt 0 ? ? 0 ? PC = ? + 20 10 addi $3, $0, 37 tt: jr $31 What is the value in register $3 at the end of this program? • $3 = 0 10 CS 230 - Spring 2020 2-40
Recommend
More recommend