Академический Документы
Профессиональный Документы
Культура Документы
Charles Kim
Spring 2007
1
Computer Organization and Design (3rd Ed)
-The Hardware/Software Interface
by
David A. Patterson
John L. Hennessy
2
Chapter 2
3
Chapter 2
Part C
4
Procedure Calling
• Procedure [subroutine]
– Tool for structuring programs
– Code reuse
– Pass values
– Return results
• Register Convention in Procedure Calling
– $a0 - $a3 : Parameter passing
– $v0 - $v1: Result returning
– $ra: Return address (automatically saved)
– Stack is used when more arguments and more results are involved
• Saved and unsaved registers by the callee (i.e. subroutine) in the procedure
calling
– Saved: $s0 - $s7
– Unsaved: $t0 - $t9
• Procedure calling instruction
– jal
• Ending procedure
– jr $ra
5
Exercise of Procedure Calling
6
Using Stack for save/retrieve
7
Procedure calling (p81) p1/2
8
Procedure calling (p81) p2/2
Assignment
– Revise the code so that it receives the values of g, h, i, and j from
keyboard
9
Nested Procedures
• Possible Conflicts
– Argument Register Values
– Return Address values
• One Solution?
– Caller: Push any argument registers ($a0 - $a3), temporary
registers ($t0 - $t9) that are need after call to stack. Upon return,
restore the registers from the stack.
– Callee: Push the return address ($ra) and saved registers ($s0 -
$s7) to stack
• Example (p.83) - recursive procedure that calculates factorial
Int fact (int n)
{
if (n<1) return (1);
else return (n* fact (n-1));
}
10
Factorial Calculation Details (1)
11
Factorial Calculation Details (2)
12
Factorial Calculation Details (3)
13
Factorial Calculation Details (4)
14
Factorial Calculation Details (5)
15
Nested Procedure Calling (p.83)
16
Nested Procedure Calling (p.83)-Conti.
17
Coding Exercise (p.123) - Bubble Sort
• Bubble Sort
– Simplest way of sorting array of objects
– Maybe slowest too
– Compare neighboring two objects, and swap them if the order in
in the wrong way
18
Bubble Sort Exercise (p.123) - conti
19
Bubble Sort Code (1)
20
Bubble Sort Code (2)
21
Bubble Sort Code (3)
22
Bubble Sort Code (4)
23
Insertion Sort
24
Insertion Code Illustration
25
Project 1 - Insertion Code
26
Overview of MIPS
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
27
Addresses in Branches and Jumps
• Instructions:
bne $t4,$t5,Label Next instruction is at Label if $t4 °
$t5
beq $t4,$t5,Label Next instruction is at Label if $t4 = $t5
j Label Next instruction is at Label
• Formats:
I op rs rt 16 bit address
J op 26 bit address
28
Addresses in Branches
• Instructions:
bne $t4,$t5,Label Next instruction is at Label if $t4≠$t5
beq $t4,$t5,Label Next instruction is at Label if $t4=$t5
• Formats:
I op rs rt 16 bit address
29
summary
MIPS operands
Name Example Comments
$s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform
32 registers $a0-$a3, $v0-$v1, $gp, arithmetic. MIPS register $zero always equals 0. Register $at is
$fp, $sp, $ra, $at reserved for the assembler to handle large constants.
Memory[0], Accessed only by data transfer instructions. MIPS uses byte addresses, so
30
2 memory Memory[4], ..., sequential words differ by 4. Memory holds data structures, such as arrays,
words Memory[4294967292] and spilled registers, such as those saved on procedure calls.
Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers
add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants
load word lw $s1, 100($s2) $s1 = Memory[$s2 + 100] Word from memory to register
store word sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory
Data transfer load byte lb $s1, 100($s2) $s1 = Memory[$s2 + 100] Byte from memory to register
store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory
load upper immediate lui $s1, 100 $s1 = 100 * 2
16 Loads constant in upper 16 bits
branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to Equal test; PC-relative branch
PC + 4 + 100
branch on not equal bne $s1, $s2, 25 if ($s1 != $s2) go to Not equal test; PC-relative
PC + 4 + 100
Conditional
branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; Compare less than; for beq, bne
else $s1 = 0
set less than slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than constant
immediate else $s1 = 0
2. Register addressing
op rs rt rd ... funct Registers
Register
3. Base addressing
op rs rt Address Memory
4. PC-relative addressing
op rs rt Address Memory
PC + Word
5. Pseudodirect addressing
op Address Memory
PC Word
31
Alternative Architectures
• Design alternative:
– provide more powerful operations
– goal is to reduce number of instructions executed
– danger is a slower cycle time and/or a higher CPI
32
IA - 32
33
IA-32 Overview
• Complexity:
– Instructions from 1 to 17 bytes long
– one operand must act as both a source and destination
– one operand can come from memory
– complex addressing modes
e.g., “base or scaled index with 8 or 32 bit displacement”
• Saving grace:
– the most frequently used instructions are not too difficult to
build
– compilers avoid the portions of the architecture that are slow
34
IA-32 Registers and Data Addressing
Name Use
31 0
EAX GPR 0
ECX GPR 1
EDX GPR 2
EBX GPR 3
ESP GPR 4
EBP GPR 5
ESI GPR 6
EDI GPR 7
35
IA-32 Register Restrictions
36
IA-32 Typical Instructions
37
Summary
38