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

computer systems
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 230 - Spring 2020 2-1

CS 230 – Introduction to Computers and Computer Systems Lecture 8 – Multiplication, Division, and Conditional Execution

slide-2
SLIDE 2

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

slide-3
SLIDE 3

CS 230 - Spring 2020 2-3

Multiplication and Division Results

mfhi $d

 copy contents of hi to register d

mflo $d

 copy contents of lo to register d

slide-4
SLIDE 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

slide-5
SLIDE 5

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

$0 $1 $2 $3 $4 $5 … ? ? ? ? ? …

slide-6
SLIDE 6

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

$0 $1 $2 $3 $4 $5 … ?

  • 3

? ? ? …

slide-7
SLIDE 7

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

$0 $1 $2 $3 $4 $5 … ?

  • 3

? ? ? … lo = 9

slide-8
SLIDE 8

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

$0 $1 $2 $3 $4 $5 … ?

  • 3

9 ? ? … lo = 9

slide-9
SLIDE 9

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?

  • $3 = 910

CS 230 - Spring 2020 2-9

$0 $1 $2 $3 $4 $5 … ?

  • 3

9 ? ? … lo = 9

slide-10
SLIDE 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

slide-11
SLIDE 11

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

$0 $1 $2 $3 $4 $5 … ? ? ? ? ? …

slide-12
SLIDE 12

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

$0 $1 $2 $3 $4 $5 … ? 11 ? ? ? …

slide-13
SLIDE 13

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

$0 $1 $2 $3 $4 $5 … ? 11 ? 5 ? …

slide-14
SLIDE 14

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

$0 $1 $2 $3 $4 $5 … ? 11 ? 5 ? … hi = 1 lo = 2

slide-15
SLIDE 15

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

$0 $1 $2 $3 $4 $5 … ? 11 2 5 ? … hi = 1 lo = 2

slide-16
SLIDE 16

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?

  • $3 = 210

CS 230 - Spring 2020 2-16

$0 $1 $2 $3 $4 $5 … ? 11 2 5 ? … hi = 1 lo = 2

slide-17
SLIDE 17

CS 230 - Spring 2020 2-17

Conditional Execution

 Computation dependent on intermediate results

 otherwise only linear number crunching

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

CS 230 - Spring 2019 2-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

slide-21
SLIDE 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

slide-22
SLIDE 22

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

$0 $1 $2 $3 $4 $5 … ? ? ? ? ? … PC = 0x54

(we don’t know the value of PC here, but lets pretend for this example that it starts at 0x54)

slide-23
SLIDE 23

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

$0 $1 $2 $3 $4 $5 … ? 10 ? ? ? … PC = 0x58

slide-24
SLIDE 24

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

$0 $1 $2 $3 $4 $5 … ? 10 0 ? ? … PC = 0x5C

slide-25
SLIDE 25

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

$0 $1 $2 $3 $4 $5 … ? 10 10 ? ? … PC = 0x60

slide-26
SLIDE 26

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

$0 $1 $2 $3 $4 $5 … ? 9 10 ? ? … PC = 0x64

slide-27
SLIDE 27

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

$0 $1 $2 $3 $4 $5 … ? 9 10 ? ? … PC = 0x5C

PC = 0x64 + 410 + (410 * -310) (-3)

slide-28
SLIDE 28

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

$0 $1 $2 $3 $4 $5 … ? 9 19 ? ? … PC = 0x60

slide-29
SLIDE 29

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

$0 $1 $2 $3 $4 $5 … ? 8 19 ? ? … PC = 0x64

slide-30
SLIDE 30

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

$0 $1 $2 $3 $4 $5 … ? 8 19 ? ? … PC = 0x5C

slide-31
SLIDE 31

Example - Skip to $2=1

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

$0 $1 $2 $3 $4 $5 … ? 1 55 ? ? … PC = 0x60

slide-32
SLIDE 32

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

$0 $1 $2 $3 $4 $5 … ? 0 55 ? ? … PC = 0x64

slide-33
SLIDE 33

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

$0 $1 $2 $3 $4 $5 … ? 0 55 ? ? … PC = 0x68

slide-34
SLIDE 34

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? $3 = 5510

CS 230 - Spring 2020 2-34

$0 $1 $2 $3 $4 $5 … ? 0 55 ? ? … PC = 0x6C

slide-35
SLIDE 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

slide-36
SLIDE 36

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

$0 $1 $2 $3 $4 $5 … ? ? ? ? ? … PC = ? + 010

slide-37
SLIDE 37

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

$0 $1 $2 $3 $4 $5 … ? ? 10 ? ? … PC = ? + 410

slide-38
SLIDE 38

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

$0 $1 $2 $3 $4 $5 … ? ? ? ? … PC = ? + 810

slide-39
SLIDE 39

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

$0 $1 $2 $3 $4 $5 … ? ? ? ? … PC = ? + 1610

(1) PC = (? + 810) + 410 + (410 * 110)

slide-40
SLIDE 40

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?

  • $3 = 010

CS 230 - Spring 2020 2-40

$0 $1 $2 $3 $4 $5 … ? ? ? ? … PC = ? + 2010

slide-41
SLIDE 41

Example

Write a MIPS assembly language program that sets $4 to $2 if $1 is odd, and otherwise sets $4 to $3.

CS 230 - Spring 2020 2-41

slide-42
SLIDE 42

Example

Write a MIPS assembly language program that sets $4 to $2 if $1 is odd, and otherwise sets $4 to $3.

addi $5, $0, 2 div $1, $5 mfhi $1

CS 230 - Spring 2020 2-42

slide-43
SLIDE 43

Example

Write a MIPS assembly language program that sets $4 to $2 if $1 is odd, and otherwise sets $4 to $3.

addi $5, $0, 2 div $1, $5 mfhi $1 addi $5, $0, 1 beq $5, $1, odd

  • dd:

CS 230 - Spring 2020 2-43

slide-44
SLIDE 44

Example

Write a MIPS assembly language program that sets $4 to $2 if $1 is odd, and otherwise sets $4 to $3.

addi $5, $0, 2 div $1, $5 mfhi $1 addi $5, $0, 1 beq $5, $1, odd add $4, $3, $0 beq $0, $0, end

  • dd:

end:

CS 230 - Spring 2020 2-44

slide-45
SLIDE 45

Example

Write a MIPS assembly language program that sets $4 to $2 if $1 is odd, and otherwise sets $4 to $3.

addi $5, $0, 2 div $1, $5 mfhi $1 addi $5, $0, 1 beq $5, $1, odd add $4, $3, $0 beq $0, $0, end

  • dd:

add $4, $2, $0 end: jr $31 ; end the program

CS 230 - Spring 2020 2-45

slide-46
SLIDE 46

Try it Yourself

Assume x represents $1. Write a MIPS assembly language program that calculates x! (x factorial) and places the result in $2. Assume x is positive.

CS 230 - Spring 2020 2-46

slide-47
SLIDE 47

Try it Yourself

Assume x represents $1. Write a MIPS assembly language program that calculates x! (x factorial) and places the result in $2. Assume x is positive.

addi $2, $0, 1 mult $2, $1 mflo $2

CS 230 - Spring 2020 2-47

slide-48
SLIDE 48

Try it Yourself

Assume x represents $1. Write a MIPS assembly language program that calculates x! (x factorial) and places the result in $2. Assume x is positive.

addi $2, $0, 1 mult $2, $1 mflo $2 addi $1, $1, -1

CS 230 - Spring 2020 2-48

slide-49
SLIDE 49

Try it Yourself

Assume x represents $1. Write a MIPS assembly language program that calculates x! (x factorial) and places the result in $2. Assume x is positive.

addi $2, $0, 1 fact: mult $2, $1 mflo $2 addi $1, $1, -1 bne $1, $0, fact

CS 230 - Spring 2020 2-49

slide-50
SLIDE 50

Try it Yourself

Assume x represents $1. Write a MIPS assembly language program that calculates x! (x factorial) and places the result in $2. Assume x is positive.

addi $2, $0, 1 fact: mult $2, $1 mflo $2 addi $1, $1, -1 bne $1, $0, fact jr $31

CS 230 - Spring 2020 2-50

slide-51
SLIDE 51

CS 230 - Spring 2020 2-51

MIPS Emulators in CS230

 Several emulators in /u/cs230/pub/

 twoints <mips-file>

enter two integer numbers – stored in $1, $2

 array <mips-file>

enter array of integer numbers – start stored in $1, length in $2

 noargs <mips-file>

run code immediately without expecting predefined user input

 Also called simulators or frontends  Argument <mips-file> is binary machine code

 output from /u/cs230/pub/binasm

slide-52
SLIDE 52

CS 230 - Spring 2020 2-52

Debugging Assembly Language

 Difficult

 use output

 covered in lecture 9

 terminate early (insert an early jr $31)

 study register settings  verify program step by step