CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional...
-
Upload
jamie-bufton -
Category
Documents
-
view
215 -
download
1
Transcript of CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional...
![Page 1: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/1.jpg)
CPS3340 COMPUTER
ARCHITECTURE Fall Semester, 2013
CPS3340 COMPUTER
ARCHITECTURE Fall Semester, 2013
10/15/2013
Lecture 11: MIPS-Conditional Instructions
Instructor: Ashraf YaseenDEPARTMENT OF MATH & COMPUTER SCIENCECENTRAL STATE UNIVERSITY, WILBERFORCE, OH
1
![Page 2: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/2.jpg)
Review
Last Class Binary Integers
Unsigned, Signed, Signed Extension
Representation of MIPS Instructions R-format I-format
This Class Conditional Instructions
slt
Branch Addressing
Next Class Procedure
2
![Page 3: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/3.jpg)
Instructions for Making Decisions
High-level programming languageC/C++:
if … else … (conditional)goto (unconditional)for, while, until (loops)
Assembly LanguagesMIPS:
beq (branch if equal)bne (branch if not equal)j (unconditional jump)
3
![Page 4: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/4.jpg)
Conditional Operations
Branch to a labeled instruction if a condition is true Otherwise, continue sequentially
beq rs, rt, L1 if (rs == rt) branch to instruction labeled
L1; bne rs, rt, L1
if (rs != rt) branch to instruction labeled L1; j L1
unconditional jump to instruction labeled L1
4
![Page 5: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/5.jpg)
Compiling If Statements
C code:
if (i==j) f = g+h;else f = g-h;
5
![Page 6: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/6.jpg)
Compiling If Statements
C code:
if (i==j) f = g+h;else f = g-h;
f ($s0), g ($s1), h($s2), i($s3), j($s4) Compiled MIPS code:
bne $s3, $s4, Else add $s0, $s1, $s2 j ExitElse: sub $s0, $s1, $s2Exit: …
Assembler calculates addresses
6
![Page 7: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/7.jpg)
Compiling Loop Statements
C code:
while (save[i] == k) i = i+1; i in $s3, k in $s5, address of save in $s6
Flowchart?
7
![Page 8: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/8.jpg)
Compiling Loop Statements
C code:
while (save[i] == k) i = i+1; i in $s3, k in $s5, address of save in $s6
Compiled MIPS code:
Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j LoopExit: …
8
![Page 9: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/9.jpg)
Basic Blocks
A basic block is a sequence of instructions with No embedded branches (except at end) No branch targets (except at beginning)
A compiler identifies basic blocks for optimization
An advanced processor can accelerate execution of basic blocks
9
![Page 10: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/10.jpg)
More Conditional Operations Less than Greater than Combination of logical operations
10
![Page 11: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/11.jpg)
More Conditional Operations Set result to 1 if a condition is true
Otherwise, set to 0 slt rd, rs, rt
if (rs < rt) rd = 1; else rd = 0; slti rt, rs, constant
if (rs < constant) rt = 1; else rt = 0; Use in combination with beq, bne
slt $t0, $s1, $s2 # if ($s1 < $s2)bne $t0, $zero, L # branch to L
11
![Page 12: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/12.jpg)
Exercise
Convert the following C++ statement into MIPS
if (i>j and i<k) {a++;
}Assume i in $s0, j in $s1, k in $s2, a in $s3
12
![Page 13: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/13.jpg)
Exercise
if (i>j and i<k) {
a++;
}
Assume i in $s0, j in $s1, k in $s2, a in $s3
slt $t0, $s1, $s0
slt $t1, $s0, $s2
and $t0, $t0, $t1
beq $t0, $zero, L
addi $s3, $s3, 1
L: …
13
![Page 14: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/14.jpg)
Better Solution
if (i>j and i<k) {
a++;
}
Assume i in $s0, j in $s1, k in $s2, a in $s3
slt $t0, $s1, $s0
beq $t0, $zero, L
slt $t0, $s0, $s2
beq $t0, $zero, L
addi $s3, $s3, 1
L: …
14
![Page 15: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/15.jpg)
Branch Instruction Design
Why not blt, bge, etc? Hardware for <, ≥, … slower than =, ≠
Combining with branch involves more work per instruction, requiring a slower clock
All instructions penalized! beq and bne are the common case This is a good design compromise
15
![Page 16: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/16.jpg)
Signed vs. Unsigned
Signed comparison: slt, slti Unsigned comparison: sltu, sltui Example
$s0 = 1111 1111 1111 1111 1111 1111 1111 1111 $s1 = 0000 0000 0000 0000 0000 0000 0000 0001 slt $t0, $s0, $s1 # signed
–1 < +1 $t0 = 1 sltu $t0, $s0, $s1 # unsigned
+4,294,967,295 > +1 $t0 = 0
16
![Page 17: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/17.jpg)
Branch Addressing
Branch instructions specify Opcode, two registers, target address
Most branch targets are near branch Forward or backward
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
PC-relative addressing Target address = PC + offset × 4 PC already incremented by 4 by this time
17
![Page 18: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/18.jpg)
Jump Addressing
Jump (j and jal) targets could be anywhere in text segment Encode full address in instruction
op address
6 bits 26 bits
(Pseudo)Direct jump addressing Target address = PC31…28 : (address × 4)
18
![Page 19: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/19.jpg)
Target Addressing Example Loop code from earlier example
Assume Loop at location 80000
Loop: sll $t1, $s3, 2 80000 0 0 19 9 2 0
add $t1, $t1, $s6 80004 0 9 22 9 0 32
lw $t0, 0($t1) 80008 35 9 8 0
bne $t0, $s5, Exit 80012 5 8 21 2
addi $s3, $s3, 1 80016 8 19 19 1
j Loop 80020 2 20000
Exit: … 80024
19
![Page 20: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/20.jpg)
Branching Far Away
If branch target is too far to encode with 16-bit offset, assembler rewrites the code
Examplebeq $s0,$s1, L1
↓bne $s0,$s1, L2j L1
L2: …
20
![Page 21: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/21.jpg)
Addressing Mode Summary21
![Page 22: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/22.jpg)
Summary
Conditional Instructions beq bne j slt, slti sltu, sltui Converting a C Program to MIPS
Branch Addressing
22
![Page 23: CPS3340 COMPUTER ARCHITECTURE Fall Semester, 2013 10/15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.](https://reader035.fdocuments.net/reader035/viewer/2022062712/56649c785503460f9492dd81/html5/thumbnails/23.jpg)
What I want you to do
Review Chapter 2 Prepare for assignment 4
23