Computer Architecture

Download Report

Transcript Computer Architecture

Computer Architecture
MIPS Instruction Set Architecture
( Supporting Procedures )
Memory/Storage Architecture Lab
MIPS Register Naming
0
$zero constant 0
16
$s0 permanent
1
$at reserved for assembler
···
2
$v0 return values
23
$s7
3
$v1
24
$t8 temporary
4
$a0 arguments
25
$t9
5
$a1
26
$k0 OS kernel (reserved)
6
$a2
27
$k1
7
$a3
28
$gp global pointer
8
$t0 temporary
29
$sp stack pointer
···
30
$fp frame pointer
15 $t7
31
$ra return address
(For variables in a high-level
language program)
Memory/Storage Architecture Lab
2
Calling Convention (Caller Save)
Callee
Caller
• save (live) registers
• set up arguments
• save return address
and jump to the callee
• allocate stack frame
• restore saved registers
• place return value
• free stack frame
• restore and jump to the
return address
Memory/Storage Architecture Lab
3
Calling Convention (Callee Save)
Callee
Caller
• allocate stack frame
• save registers(that may
be modified in the callee)
• set up arguments
• save return address
and jump to the callee
• place return value
• restore saved registers
• free stack frame
• restore and jump to the
return address
Memory/Storage Architecture Lab
4
MIPS Calling Convention (Hybrid)
Callee
Caller
• save (live) caller-saved registers:
$t0~$t9, $a0~$a3, $v0~$v1, $ra
• set up arguments : first four in
$a0~$a3, rest on stack
• execute jal instruction
• allocate stack frame:
$sp ← $sp – frame size
• save callee-saved registers
(that may be modified in the
callee) : $s0~$s7
• restore caller-saved registers
• place return value in $v0~$v1
• restore callee-saved registers
• free stack frame :
$sp ← $sp + frame size
• return by jr $ra
Memory/Storage Architecture Lab
5
MIPS Register Convention
0
$zero constant 0
16
$s0 permanent (callee saves)
1
$at reserved for assembler
···
2
$v0 function return value
23
$s7
3
$v1 (caller saves)
24
$t8 temporary (caller saves)
4
$a0 arguments (caller saves)
25
$t9
5
$a1
26
$k0 OS kernel (reserved)
6
$a2
27
$k1
7
$a3
28
$gp global pointer (caller saves)
8
$t0 temporary (caller saves)
29
$sp stack pointer (preserved)
···
30
$fp frame pointer ($s8 for cc)
15 $t7
31
$ra return address(jal) (caller saves)
(For variables in a high-level
language program)
Memory/Storage Architecture Lab
(callee saves)
6
Compiling a Leaf Procedure
Assembly
C
leaf_example:
int leaf_example ( int g,
int h, int i, int j )
{
int f ;
f = ( g + h ) – ( i + j );
return f;
}
Actually
Not needed
Memory/Storage Architecture Lab
addi
sw
sw
sw
add
add
sub
add
lw
lw
lw
addi
jr
$sp,
$t1,
$t0,
$s0,
$t0,
$t1,
$s0,
$v0,
$s0,
$t0,
$t1,
$sp,
$ra
$sp, -12
8($sp)
4($sp)
0($sp)
$a0, $a1
$a2, $a3
$t0, $t1
$s0, $zero
0($sp)
4($sp)
8($sp)
$sp, 12
7
Stack Allocation
FIGURE 3.12 Illustration of the stack allocation (a)before, (b)during, and (c)after the procedure call
Memory/Storage Architecture Lab
8
Compiling a Non-Leaf Procedure
C
Assembly
int nonleaf_example (int arg)
{
int result;
result = arg +
leaf_example(5,4,3,2);
return result;
}
nonleaf_example:
Memory/Storage Architecture Lab
addi
sw
sw
sw
addi
addi
addi
addi
jal
lw
lw
add
add
lw
addi
jr
$sp, $sp, -12
$s0, 8($sp)
$ra, 4($sp)
$a0, 0($sp)
$a0, $zero, 5
$a1, $zero, 4
$a2, $zero, 3
$a3, $zero, 2
leaf_example
$a0, 0($sp)
$ra, 4($sp)
$s0, $a0, $v0
$v0, $s0, $zero
$s0, 8($sp)
$sp, $sp, 12
$ra
9
Compiling a Recursive Procedure
C
Assembly
int fact ( int n )
{
if ( n < 1 ) return (1) ;
else
return ( n * fact ( n – 1) );
}
fact: slti
beq
addi
jr
L1:
addi
sw
sw
addi
jal
lw
lw
addi
mult
mflo
jr
Memory/Storage Architecture Lab
$t0, $a0, 1
$t0, $zero, L1
$v0, $zero, 1
$ra
$sp, $sp, -8
$ra, 4($sp)
$a0, 0($sp)
$a0, $a0, -1
fact
$ra, 4($sp)
$a0, 0($sp)
$sp, $sp, 8
$a0, $v0
$v0
$ra
10
Compiling and Starting a Program
Memory/Storage Architecture Lab
11
Object File Example
Object file header
Text segment
Data segment
Relocation information
Symbol table
Object file header
Name
Procedure A
Name
Procedure B
Text size
100hex
Text size
200hex
Data size
20hex
Data size
30hex
Address
Instruction
Address
Instruction
0
lw $a0, 0($gp)
0
sw $a1, 0($gp)
4
jal 0
4
jal 0
…
…
…
…
0
(X)
0
(Y)
…
…
…
…
Address
Instruction type
Dependency
Address
Instruction type
Dependency
0
lw
X
0
sw
Y
4
jal
B
4
jal
A
Label
Address
Label
Address
X
-
Y
-
B
-
A
-
Text segment
Data segment
Relocation information
Symbol table
Memory/Storage Architecture Lab
12
MIPS Memory Allocation
Memory/Storage Architecture Lab
13
Executable File Example
Executable file header
Text segment
Data segment
Text size
300hex
Data size
50hex
Address
Instruction
0040 0000hex
lw $a0, 8000hex($gp)
0040 0004hex
jal 40 0100hex
…
…
0040 0100hex
sw $a1, 8020hex($gp)
0000 0104hex
jal 40 0000hex
…
…
Address
1000 0000hex
(X)
…
…
1000 0020hex
(Y)
…
…
Memory/Storage Architecture Lab
14