Transcript Title

Branches and Procedure Calls
Lecture 14
Digital Design and Computer Architecture
Harris & Harris
Morgan Kaufmann / Elsevier, 2007
1
Review: Instructions, Operands
• Instructions
– add, sub, addi, and, lw, sw, etc.
• Operands
– Registers
– Memory
– Immediates (constants)
2
Review: MIPS Register Set
Name
Reg
Usage
$0
0
Const 0
$v0-$v1
2-3
Results & Expr. Eval.
$a0-$a3
4-7
Procedure arguments
$t0-$t7
8-15
Temporaries
$s0-$s7
16-23
Saved variables
$t8-$t9
24-25
More Temporaries
$gp
28
Global Pointer
$sp
29
Stack Pointer
$fp
30
Frame Pointer
$ra
31
Return address
3
Review: MIPS Memory
Word
Address
0000000C
F E D C
00000008
B A 9 8
00000004
7 6 5 4
00000000
3 2 1 0
MSB
LSB
4
MIPS Memory: Big- and Little-Endian
Little-Endian:
Big-Endian:
Word
Address
Word
Address
0000000C
F E D C
0000000C
C D E F
00000008
B A 9 8
00000008
8 9 A B
00000004
7 6 5 4
00000004
4 5 6 7
00000000
3 2 1 0
00000000
0 1 2 3
MSB
LSB
MSB
LSB
5
Review: Instruction Formats
R-Type
op
6 bits
rs
5 bits
rt
rd
shamt
funct
5 bits
5 bits
5 bits
6 bits
I-Type
op
rs
rt
imm
6 bits
5 bits
5 bits
16 bits
J-Type
op
addr
6 bits
26 bits
6
Shift Instructions
# shift left
sll $t0, $s1, 2
# $t0 <= $s1 << 2
# shift right logical
srl $t0, $s1, 2
# $t0 <= $s1 >> 2
# shift right arithmetic
sra $t0, $s1, 2
# $t0 <= $s1 >>> 2
7
Shift Instructions
Assembly Code
Field Values
op
rs
rt
rd
shamt
funct
sll $t0, $s1, 2
0
0
17
8
2
0
srl $s2, $s1, 2
0
0
17
18
2
2
sra $s3, $s1, 2
0
0
17
19
2
3
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
Machine Code
op
rs
rt
rd
shamt
funct
000000 00000 10001 01000 00010 000000 (0x00114080)
000000 00000 10001 10010 00010 000010 (0x00119082)
000000 00000 10001 10011 00010 000011 (0x00119883)
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
8
Review: The Stored Program
Assembly Code
Machine Code
lw
$t2, 32($0)
0x8C0A0020
add
$s0, $s1, $s2
0x02328020
addi $t0, $s3, -12
0x2268FFF4
sub
0x016D4022
$t0, $t3, $t5
Stored Program
Address
Instructions
0040000C
0 1 6 D 4 0 2 2
00400008
2 2 6 8 F F F 4
00400004
0 2 3 2 8 0 2 0
00400000
8 C0 A0 0 2 0
Main Memory
PC
9
Branching
Conditional Branching:
• branch if equal (beq)
• branch if not equal (bne)
Unconditional Branching:
• jump (j)
• jump register (jr)
• jump and link (jal)
10
Conditional Branching (beq)
# MIPS assembly
addi
addi
sll
beq
addi
sub
$s0,
$s1,
$s1,
$s0,
$s1,
$s1,
$0, 4
# $s0 = 0 + 4 = 4
$0, 1
# $s1 = 0 + 1 = 1
$s1, 2
# $s1 = 1 << 2 = 4
$s1, target
$s1, 1
$s1, $s0
target:
add $s1, $s1, $s0
# $s1 = 4 + 4 = 8
11
Conditional Branching (bne)
# MIPS assembly
addi
addi
sll
bne
addi
sub
target:
add
$s0,
$s1,
$s1,
$s0,
$s1,
$s1,
$0, 4
$0, 1
$s1, 2
$s1, target
$s1, 1
$s1, $s0
$s1, $s1, $s0
# $s0 = 0 + 4 = 4
# $s1 = 0 + 1 = 1
# $s1 = 1 << 2 = 4
# $s1 = 1 + 4 = 5
12
Unconditional Branching (j)
# MIPS assembly
addi $s0, $0, 4
addi $s1, $0, 1
j
target
sra
$s1, $s1, 2
addi
$s1, $s1, 1
sub
$s1, $s1, $s0
#
#
#
#
#
#
target:
add
# $s1 = 1 + 4 = 5
$s1, $s1, $s0
$s0 = 4
$s1 = 1
jump to target
not executed
not executed
not executed
13
Unconditional Branching (jr)
# MIPS assembly
0x00002000
0x00002004
0x00002008
0x0000200C
0x00002010
addi
jr
addi
sra
lw
$s0, $0, 0x2010
$s0
$s1, $0, 1
$s1, $s1, 2
$s3, 44($s1)
14
High-Level Code Constructs
• If/Else Statements
• While Loops
• For Loops
15
If Statement
// high-level code
if (i == j)
f = g + h;
f = f - i;
// MIPS assembly
# $s0 = f, $s1 = g, $s2 = h, $s3 = i, $s4 = j
16
If / Else Statement
// high-level code
if (i == j)
f = g + h;
else
f = f - i;
// MIPS assembly
# $s0 = f, $s1 = g, $s2 = h, $s3 = i, $s4 = j
17
While Loops
// high-level code
int result = 5;
while (result > 0) {
result = result - 1;
}
// MIPS assembly
# $s0 = result
18
For Loops
// high-level code
int sum = 0;
for (i=0; i != 10; i = i+1)
sum = sum + i;
// MIPS assembly
# $s0 = i, $s1 = sum
19
For Loops: Using slt
// high-level code
int sum = 0;
for (i = 1; i < 101; i = i * 2)
sum = sum + i;
20
For Loops: Using slt
# MIPS assembly code
# $s0 = i, $s1 = sum
addi $s1, $0, 0
addi $s0, $0, 1
addi $t0, $0, 101
loop:
slt $t1,
= 0
beq $t1,
to done
add $s1,
sll $s0,
j
loop
# sum = 0
# i = 1
# $t0 = 101
$s0, $t0
# if (i < 101) $t1 = 1, else $t1
$0, done
# if $t1 == 0 (i >= 101), branch
$s1, $s0
$s0, 1
# sum = sum + i
# i = i * 2
done:
21
Procedure Calls
Definitions
• Caller: calling procedure
• Callee: called procedure
How it works
• Caller needs to call a procedure without any unintended side
effects
• Callee returns to point of call (place it was called from) when done
• Caller passes arguments to callee
• Callee returns result to caller
22
Procedure Calls
// high-level code
int main() {
simple();
...
}
// void means the function returns no value
void simple(){
return;
}
23
Procedure Calls
# MIPS assembly code
0x00400200 main:
0x00400204
...
jal
add
simple
$s0, $s1, $s2
0x00401020 simple: jr $ra
24
Input Arguments, Return Values
// high-level code
int main()
{
int y;
...
y = diffofsums(2, 3, 4, 5);
...
}
int diffofsums(int f, int g, int h, int i)
{
int result;
result = (f + g) - (h + i);
return result;
}
25
Input Arguments, Return Values
# MIPS assembly
# $s0 = y
main:
...
addi
addi
addi
addi
jal
add
...
$a0, $0, 2
$a1, $0, 3
$a2, $0, 4
$a3, $0, 5
diffofsums
$s0, $v0, $0
# $s0 = result
diffofsums:
add $t0, $a0,
add $t1, $a2,
sub $s0, $t0,
add $v0, $s0,
jr $ra
$a1
$a3
$t1
$0
#
#
#
#
#
#
argument 0 = 2
argument 1 = 3
argument 2 = 4
argument 3 = 5
call procedure
y = returned value
#
#
#
#
#
$t0 = f + g
$t1 = h + i
result = = (f + g) - (h + i)
put return value in $v0
return to caller
26
Next Time
• More on Procedures
• Addressing Modes
• Linking and Launching Applications
27