ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 3: Arithmetic Instructions Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy, and classes taught by Patterson at Berkeley, Ryan Kastner at UCSB and Mary Jane Irwin at Penn State
39
Embed
ECE 15B Computer Organization Spring 2010 Dmitri Strukov
ECE 15B Computer Organization Spring 2010 Dmitri Strukov. Lecture 3: Arithmetic Instructions. Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy, and classes taught by Patterson at Berkeley, Ryan Kastner at UCSB and Mary Jane Irwin at Penn State. - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
ECE 15B Computer OrganizationSpring 2010
Dmitri Strukov
Lecture 3: Arithmetic Instructions
Partially adapted from Computer Organization and Design, 4th edition, Patterson and Hennessy, and classes taught by Patterson at Berkeley, Ryan Kastner at UCSB and Mary Jane Irwin at Penn State
ECE 15B Spring 2010
Announcement
• TA office hours for Vivek were moved to Tuesday 11:00 am – 12:00 am
• Basics of logic design is in Appendix C (P+H)• SPIM and reading status
ECE 15B Spring 2010
Agenda
• Key concepts from last lecture & several new ones
• C operators and operands• Variables in Assembly: Registers• Addition and Subtraction in Assembly
– Where1) operation name2,3,4) operands5) comments0) label field is optional, will discuss later
– For arithmetic and logic instruction2) operand getting result (“destination”)
3) 1st operand for operation (“source 1”) 4) 2nd operand for operation (source 2”
• Syntax is rigid– 1 operator, 3 operands– Why? Keep hardware simple via regularity
ECE 15B Spring 2010
Addition and Subtraction of Integers• Addition in assembly
– Example:add $s0, $s1, $s2 (in MIPS)• Equivalent to: a = b + c (in C)• Where MIPS registers $s0, $s1, $s2 are associated with C
variables a, b, c
• Subtraction in Assembly– Example
Sub $s3, $s4, S5 (in MIPS)• Equivalent to: d = e - f (in C)• Where MIPS registers $s3, $s4, $s5 are associated with C
variables d, e, f
ECE 15B Spring 2010
Addition and Subtraction of Integers
• The following C statement in MIPS?a= b + c+ d - a
• Break into multiple instructions add $t0, $s1, $s2 #temp = b + c add $t0, $t0, $s3 # temp = temp + d sub $s0, $t0, $s4 # a = temp – e
– Notes: • A single line of C may break up into several lines of MIPS• Everything after the hash mark on each line is ignored (i.e. comments)
ECE 15B Spring 2010
Addition and Subtraction of Integers
• How do we do this? f = (g + h) – (i + j)Use intermediate temporary registers
add $t0, $s1, $s2 #temp = g + hadd $t1, $s3, $s4 #temp = I + j
sub $s0, $t0, $t1 #f = (g+h)-(i+j)
ECE 15B Spring 2010
Immediates
• Immediates are numerical constants• They appear often in code, so there are special
instructions for them• Add immediate:
addi $s0, $s1, 10 # f= g + 10 (in C)– Where MIPS registers $s0 and $s1 are associated
with C variables f and g– Syntax similar to add instruction, except that last
argument is a number instead of register
ECE 15B Spring 2010
Immediates• There is no Subtract Immediate in MIPS: Why?
– Remove redundant operations, i.e. if operation can be decomposed to into simpler ones exclude it from the set of instructions
addi …, -X is equivalent to subi …, X so no subi
• Exampleaddi $so, $s1, -10 # f = g – 10– where MIPS registers $s0 and $s1 are associated with C variables
f and g
ECE 15B Spring 2010
Register Zero
• One particular immediate, the number zero (o) appears very often in code– So define register zero ($0 or $zero) to always have the value
0• Example
add $s0, S1, $zero # f = g• Where MIPS registers $s0 and $s1 are associated with C variables f,
g,
• Defined in hardware, so an instruction addi $zero, $zero, 5will not do anything!
Additional Notes: CPU Time
• Performance improved by– Reducing number of clock cycles– Increasing clock rate– Hardware designer must often trade off clock rate
against cycle count
Rate ClockCycles Clock CPU
Time Cycle ClockCycles Clock CPUTime CPU
ECE 15B Spring 2010
Additional Notes: CPU Time Example• Computer A: 2GHz clock, 10s CPU time• Designing Computer B
– Aim for 6s CPU time– Can do faster clock, but causes 1.2 × clock cycles
• How fast must Computer B clock be?
4GHz6s
10246s
10201.2Rate Clock
10202GHz10s
Rate ClockTime CPUCycles Clock
6sCycles Clock1.2
Time CPUCycles ClockRate Clock
99
B
9
AAA
A
B
BB
ECE 15B Spring 2010
Additional Notes: Instruction Count and CPI
• Instruction Count for a program– Determined by program, ISA and compiler
• Average cycles per instruction– Determined by CPU hardware– If different instructions have different CPI
• Average CPI affected by instruction mix
Rate ClockCPICount nInstructio
Time Cycle ClockCPICount nInstructioTime CPU
nInstructio per CyclesCount nInstructioCycles Clock
ECE 15B Spring 2010
Additional Notes: CPI Example• Computer A: Cycle Time = 250ps, CPI = 2.0• Computer B: Cycle Time = 500ps, CPI = 1.2• Same ISA• Which is faster, and by how much?
1.2500psI600psI
ATime CPUBTime CPU
600psI500ps1.2IBTime CycleBCPICount nInstructioBTime CPU
500psI250ps2.0IATime CycleACPICount nInstructioATime CPU
A is faster…
…by this much
ECE 15B Spring 2010
Additional Notes: CPI in More Detail• If different instruction classes take different