computer systems
play

Computer Systems Lecture 8 Multiplication, Division, and - PowerPoint PPT Presentation

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


  1. CS 230 – Introduction to Computers and Computer Systems Lecture 8 – Multiplication, Division, and Conditional Execution CS 230 - Spring 2020 2-1

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  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? CS 230 - Spring 2020 2-8

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  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 ? 5 mflo $3 jr $31 What is the value in register $3 at the end of this program? CS 230 - Spring 2020 2-14

  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? CS 230 - Spring 2020 2-15

  16. 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

  17. Conditional Execution  Computation dependent on intermediate results  otherwise only linear number crunching CS 230 - Spring 2020 2-17

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  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 = 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

  27. 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

  28. 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

  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 = 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

  30. 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

  31. 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

  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 = 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

  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 = 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

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