CS 230 - Spring 2020 2-1
Computer Systems Lecture 8 Multiplication, Division, and - - PowerPoint PPT Presentation
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
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
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
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 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 … ? ? ? ? ? …
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
? ? ? …
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
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
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
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 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 … ? ? ? ? ? …
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 ? ? ? …
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 ? …
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
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
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
CS 230 - Spring 2020 2-17
Conditional Execution
Computation dependent on intermediate results
otherwise only linear number crunching
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
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
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
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 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)
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
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
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
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
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)
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
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
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
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
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
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
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
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 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
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
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
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)
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
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
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
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
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
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
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
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
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
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
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
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
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