CS 230 Chapter 2 Instructions: Language of the Computer

Download Report

Transcript CS 230 Chapter 2 Instructions: Language of the Computer

CS 230: Computer Organization
and Assembly Language
Aviral Shrivastava
Department of Computer Science and Engineering
School of Computing and Informatics
Arizona State University
Slides courtesy: Prof. Yann Hang Lee, ASU, Prof. Mary
Jane Irwin, PSU, Ande Carle, UCB
M
C L
Announcements
• Quiz 1
– Collect your answer sheet from TA
– Grades are online
– Grade distribution is online (Avg. 53 points)
• Project 1
– Grades online
• Quiz 2
– Complete Chapter 2
• MIPS Assembly Language Programming, including function calls
– Thursday, Sept 24, 2009
• Project 2
– MIPS Assembly Language Programming, including function calls
– Will be posted tonight, will be due in a week
– Will take a day or two to program
M
C L
What have we learned
• So far
– Write any program in MIPS
• Today
– More Examples of Function Calls
M
C L
Below the Program

High-level language program (in C)
swap (int v[], int k)
. . .
Assembly
swap:

language program (for MIPS)
sll
add
lw
lw
sw
sw
jr
C - Compiler
$2, $5, 2
$2, $4, $2
$15, 0($2)
$16, 4($2)
$16, 0($2)
$15, 4($2)
$31
Machine (object) code (for MIPS)
000000
000000
100011
100011
101011
101011
000000
00000
00100
00010
00010
00010
00010
11111
00101
00010
01111
10000
10000
01111
00000
0001000010000000
0001000000100000
0000000000000000
0000000000000100
0000000000000000
0000000000000100
0000000000001000
Assembler
M
C L
MIPS Instructions, so far
Category
Instr
Op Code
Example
Meaning
Arithmetic
(R format)
add
0 and 32
add $s1, $s2, $s3
$s1 = $s2 + $s3
subtract
0 and 34
sub $s1, $s2, $s3
$s1 = $s2 - $s3
Data
transfer
(I format)
load word
35
lw
$s1, 100($s2)
$s1 = Memory($s2+100)
store word
43
sw $s1, 100($s2)
Memory($s2+100) = $s1
load byte
32
lb
$s1, 101($s2)
$s1 = Memory($s2+101)
store byte
40
sb
$s1, 101($s2)
Memory($s2+101) = $s1
br on equal
4
beq $s1, $s2, L
if ($s1==$s2) go to L
br on not equal
5
bne $s1, $s2, L
set on less than
0 and 42
Cond.
Branch
Uncond.
Jump
slt
$s1, $s2, $s3
if ($s1 !=$s2) go to L
if ($s2<$s3) $s1=1 else
$s1=0
2
j
2500
go to 10000
jump register
0 and 8
jr
$t1
go to $t1
jump and link
3
jal
2500
go to 10000; $ra=PC+4
jump
M
C L
MIPS Organization
Processor
Memory
Register File
src1 addr
5
src2 addr
5
dst addr
write data
5
1…1100
src1
32 data
32
registers
($zero - $ra)
read/write
addr
src2
data
32
32
32
32 bits
br offset
32
Fetch
PC = PC+4
Exec
32 Add
PC
32 Add
4
read data
32
32
32
write data
32
Decode
230
words
32
32 ALU
32
32
4
0
5
1
6
2
32 bits
byte address
(big Endian)
7
3
0…1100
0…1000
0…0100
0…0000
word address
(binary)
M
C L
MIPS R3000 ISA
Registers
• Instruction Categories
–
–
–
–
R0 - R31
Arithmetic
Load/Store
Jump and Branch
Floating Point
PC
HI
• coprocessor
LO
– Memory Management
– Special
• 3 Instruction Formats: all 32 bits wide
6 bits
5 bits
OP
rs
rt
6 bits
5 bits
5 bits
OP
rs
rt
6 bits
OP
5 bits
5 bits
5 bits
rd
sa
6 bits
funct
R Format
16 bits
immediate
26 bits
jump target
I Format
M
C L
J Format
Programming Styles
• Procedures (subroutines) allow the programmer to
structure programs making them
– easier to understand and debug and
– allowing code to be reused
• Procedures allow the programmer to concentrate on
one portion of the code at a time
– parameters act as barriers between the procedure and the rest
of the program and data, allowing the procedure to be passed
values (arguments) and to return values (results)
M
C L
Requirements for Functions
• Pass arguments to the function
– $a0, $a1, $a2, $a3
• Get results from the function
– $v0, $v1
• Can call from anywhere
– jal
• Can always return back
– jr
• Nested and Recursive Functions
– Save $ra on stack
• Saving and Restoring Registers
• Functions with more than 4 parameters
M
C L
Steps for Making a Function Call
1) Save necessary values onto stack
2) Assign argument(s), if any
3) jal call
4) Restore values from stack
M
C L
Example Function
int sumSquare(int x, int y)
{
return mult(x,x)+ y;
}
sumSquare:
addi $sp,$sp,-8
“push” sw $ra, 4($sp)
sw $a1, 0($sp)
add $a1,$a0,$zero
jal mult
lw $a1, 0($sp)
“pop” add $v0,$v0,$a1
lw $ra, 4($sp)
addi $sp,$sp,8
jr $ra
mult: ...
# space on stack
# save ret addr
# save y
# mult(x,x)
# call mult
#
#
#
#
restore y
mult()+y
get ret addr
restore stack
M
C L
Rules for Function Calls
• Called with a jal instruction, returns with a jr $ra
• Accepts up to 4 arguments in $a0, $a1, $a2 and $a3
• Return value is always in $v0 (and if necessary in $v1)
• Must follow register conventions
– even in functions that only you will call!
M
C L
Other Registers
• $at: may be used by the assembler at any time; unsafe
to use
• $k0-$k1: may be used by the OS at any time; unsafe to
use
• $gp, $fp: don’t worry about them
– Feel free to read up on $gp and $fp in Appendix A, but you can
write perfectly good MIPS code without them.
M
C L
Basic Structure of a Function
Prologue
entry_label:
addi $sp,$sp, -framesize
sw $ra, framesize-4($sp) # save $ra
save other regs if need be
...
Body
ra
(call other functions…)
memory
Epilogue
restore other regs if need be
lw $ra, framesize-4($sp) # restore $ra
addi $sp,$sp, framesize
jr $ra
M
C L
Register Conventions
• CalleR: the calling function
• CalleE: the function being called
• When callee returns from executing, the caller needs
to know which registers may have changed and which
are guaranteed to be unchanged.
• Register Conventions: A set of generally accepted
rules as to which registers will be unchanged after a
procedure call (jal) and which may be changed.
M
C L
Register Conventions
• None guaranteed  inefficient
– Caller will be saving lots of regs that callee doesn’t use!
• All guaranteed  inefficient
– Callee will be saving lots of regs that caller doesn’t use!
• Register convention: A balance between the two.
M
C L
Register Conventions – Saved Registers
• $0: No Change. Always 0.
• $s0-$s7: Restore if you change. Very important, that’s why
they’re called saved registers. If the callee changes these in
any way, it must restore the original values before returning.
• $sp: Restore if you change. The stack pointer must point to
the same place before and after the jal call, or else the
caller won’t be able to restore values from the stack.
• HINT -- All saved registers start with S!
M
C L
Register Conventions – Volatile Registers
• $ra: Can Change. The jal call itself will change this register.
Caller needs to save on stack if nested call.
•
• $v0-$v1: Can Change. These will contain the new returned
values.
•
• $a0-$a3: Can change. These are volatile argument registers.
Caller needs to save if they’ll need them after the call.
• $t0-$t9: Can change. That’s why they’re called temporary:
any procedure may change them at any time. Caller needs to
save if they’ll need them afterwards.
M
C L
MIPS Register Convention
Name
$zero
Register
Number
0
Usage
Should preserve
on call?
the constant 0
n.a.
$v0 - $v1
2-3
returned values
no
$a0 - $a3
4-7
arguments
yes
$t0 - $t7
8-15
temporaries
no
$s0 - $s7
16-23
saved values
yes
$t8 - $t9
24-25
temporaries
no
$gp
28
global pointer
yes
$sp
29
stack pointer
yes
$fp
30
frame pointer
yes
$ra
31
return address
yes
M
C L
Register Conventions
• What do these conventions mean?
– If function R calls function E, then function R must
save any temporary registers that it may be using
onto the stack before making a jal call.
– Function E must save any S (saved) registers it
intends to use before garbling up their values
– Remember: Caller/callee need to save only
temporary/saved registers they are using, not all
registers.
M
C L
Requirements for Functions
• Pass arguments to the function
– $a0, $a1, $a2, $a3
• Get results from the function
– $v0, $v1
• Can call from anywhere
– jal
• Can always return back
– jr
• Nested and Recursive Functions
– Save $ra on stack
• Saving and Restoring Registers
– Register Conventions
• Functions with more than 4 parameters
– Pass them on the stack
M
C L
Nested Procedures
• Leaf procedures do not call other procedures.
– What happens to return addresses with nested procedures?
int rt_1 (int i)
{
if (i == 0)
return 0;
else
return rt_2(i-1);
}
caller:
jal
rt_1
next:
. . .
rt_1:
bne
add
jr
addi
jal
jr
to_2:
rt_2:
. . .
$a0, $zero, to_2
$v0, $zero, $zero
$ra
$a0, $a0, -1
rt_2
$ra
M
C L
Nested Procedures Outcome
next:
rt_1:
caller:
. . .
to_2:
bne
add
jr
addi
jal
jr
rt_2:
. . .
jal
rt_1
int rt_1 (int i)
{
if (i == 0)
return 0;
else
return rt_2(i-1);
}
$a0, $zero, to_2
$v0, $zero, $zero
$ra
$a0, $a0, -1
rt_2
$ra
• On the call to rt_1, the return address (next in the
caller routine) gets stored in $ra. What happens to
the value in $ra (when i != 0) when rt_1 makes
a call to rt_2?
M
C L
Saving the Return Address, Part 1
Nested procedures (i passed in $a0, return value in $v0)
high addr
rt_1:
 $sp
old TOS
caller rt addr
to_2:
$a0 value
low addr
bk_2:
caller
bk_2
rt addr $ra
$a0 value
$a0
bne
add
jr
addi
sw
sw
addi
jal
lw
lw
addi
jr
$a0,
$v0,
$ra
$sp,
$ra,
$a0,
$a0,
rt_2
$a0,
$ra,
$sp,
$ra
int rt_1 (int i)
{
if (i == 0)
return 0;
else
return rt_2(i-1);
}
$zero, to_2
$zero, $zero
$sp, -8
4($sp)
0($sp)
$a0, -1
0($sp)
4($sp)
$sp, 8
$a0 value - 1
• Save the return address (and arguments) on the stack
$rt_2 $a0 value
M
C L
Compiling a Recursive Procedure
• Calculating factorial:
int fact (int n)
{
if (n < 1) return 1;
else return (n * fact (n-1));
}
• Recursive procedure (one that calls itself!)
fact (0) = 1
fact (1) = 1 * 1 = 1
fact (2) = 2 * 1 * 1 = 2
fact (3) = 3 * 2 * 1 * 1 = 6
fact (4) = 4 * 3 * 2 * 1 * 1 = 24
...
• Assume n is passed in $a0; result returned in $v0
M
C L
Compiling a Recursive Procedure
fact:
addi
sw
sw
slt
beq
addi
addi
jr
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$sp,
$ra
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
$sp, 8
L1:
addi
jal
$a0, $a0, -1
fact
bk_f:
lw
lw
addi
mul
jr
$a0,
$ra,
$sp,
$v0,
$ra
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
#adjust stack pointer
#save return address
#save argument n
#test for n < 1
#if n >=1, go to L1
#else return 1 in $v0
#adjust stack pointer
#return to caller
#n >=1, so decrement n
#call fact with (n-1)
#this is where fact returns
0($sp)
#restore argument n
4($sp)
#restore return address
$sp, 8
#adjust stack pointer
$a0, $v0
#$v0 = n * fact(n-1)
#return to caller
M
C L
A Look at the Stack for $a0 = 2
old TOS
 $sp
caller rt addr
$a0 = 2
caller
bk_f
rt addr
$ra
1
2
$a0
$v0
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
fact:
addi
sw
sw
slt
beq
addi
addi
jr
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$sp,
$ra
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
$sp, 8
L1:
addi
jal
$a0, $a0, -1
fact
bk_f:
lw
lw
addi
mul
jr
$a0,
$ra,
$sp,
$v0,
$ra
0($sp)
4($sp)
$sp, 8
$a0, $v0
M
C L
A Look at the Stack for $a0 = 2
old TOS
caller rt addr
$a0 = 2
 $sp
bk_f
$ra
1
$a0
$v0
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
• Stack state after execution of
first encounter with the jal
instruction (second call to fact
routine with $a0 now
holding 1)
– saved return address to caller
routine (i.e., location in the main
routine where first call to fact is
made) on the stack
– saved original value of $a0 on
the stack
M
C L
A Look at the Stack for $a0 = 2
old TOS
caller rt addr
$a0 = 2
 $sp
fact:
addi
sw
sw
slt
beq
addi
addi
jr
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$sp,
$ra
L1:
addi
jal
$a0, $a0, -1
fact
bk_f:
lw
lw
addi
mul
jr
$a0,
$ra,
$sp,
$v0,
$ra
bk_f
$a0 = 1
bk_f
$ra
1
0
$a0
$v0
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
$sp, 8
0($sp)
4($sp)
$sp, 8
$a0, $v0
M
C L
A Look at the Stack for $a0 = 2
old TOS
caller rt addr
$a0 = 2
bk_f
$a0 = 1
 $sp
bk_f
$ra
0
$a0
$v0
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
• Stack state after execution of
second encounter with the
jal instruction (third call to
fact routine with $a0 now
holding 0)
– saved return address of
instruction in caller routine
(instruction after jal) on the
stack
– saved previous value of $a0
on the stack
M
C L
A Look at the Stack for $a0 = 2
old TOS
caller rt addr
$a0 = 2
fact:
addi
sw
sw
slt
beq
addi
addi
jr
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$sp,
$ra
L1:
addi
jal
$a0, $a0, -1
fact
bk_f:
lw
lw
addi
mul
jr
$a0,
$ra,
$sp,
$v0,
$ra
bk_f
$a0 = 1
bk_f
 $sp
$a0 = 0
bk_f
$ra
0
$a0
1
$v0
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
$sp, 8
0($sp)
4($sp)
$sp, 8
$a0, $v0
M
C L
A Look at the Stack for $a0 = 2
old TOS
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
caller rt addr
$a0 = 2
bk_f
$a0 = 1
 $sp
• Stack state after execution
of first encounter with the
first jr instruction ($v0
initialized to 1)
– stack pointer updated to
point to third call to fact
bk_f
$ra
0
$a0
1
$v0
M
C L
A Look at the Stack for $a0 = 2
old TOS
caller rt addr
$a0 = 2
fact:
addi
sw
sw
slt
beq
addi
addi
jr
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$sp,
$ra
L1:
addi
jal
$a0, $a0, -1
fact
bk_f:
lw
lw
addi
mul
jr
$a0,
$ra,
$sp,
$v0,
$ra
bk_f
$a0 = 1
 $sp
bk_f
$ra
1
0
$a0
1*1 1= 1
$v0
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
$sp, 8
0($sp)
4($sp)
$sp, 8
$a0, $v0
M
C L
A Look at the Stack for $a0 = 2
old TOS
caller rt addr
$a0 = 2
 $sp
bk_f
$ra
1
$a0
1
$v0
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
• Stack state after execution
of first encounter with the
second jr instruction
(return from fact routine
after updating $v0 to 1 * 1)
– return address to caller
routine (bk_f in fact routine)
restored to $ra from the stack
– previous value of $a0
restored from the stack
– stack pointer updated to
point to second call to fact
M
C L
A Look at the Stack for $a0 = 2
old TOS
caller rt addr
$a0 = 2
 $sp
bk_f
caller
rt addr $ra
1
2
2*1 1= 2
$a0
$v0
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
fact:
addi
sw
sw
slt
beq
addi
addi
jr
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$sp,
$ra
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
$sp, 8
L1:
addi
jal
$a0, $a0, -1
fact
bk_f:
lw
lw
addi
mul
jr
$a0,
$ra,
$sp,
$v0,
$ra
0($sp)
4($sp)
$sp, 8
$a0, $v0
M
C L
A Look at the Stack for $a0 = 2
old TOS
 $sp
int fact (int n)
{
if (n < 1)
return 1;
else
return (n * fact (n-1));
}
• Stack state after execution of
second encounter with the
second jr instruction (return
from fact routine after updating
$v0 to
1 * 1 * 2)
caller rt addr $ra
2
$a0
2
$v0
– return address to caller routine
(main routine) restored to $ra
from the stack
– original value of $a0 restored
from the stack
– stack pointer updated to point to
first call to fact
M
C L
MIPS Register Convention
Name
$zero
Register
Number
0
Usage
Should preserve
on call?
the constant 0
n.a.
$v0 - $v1
2-3
returned values
no
$a0 - $a3
4-7
arguments
yes
$t0 - $t7
8-15
temporaries
no
$s0 - $s7
16-23
saved values
yes
$t8 - $t9
24-25
temporaries
no
$gp
28
global pointer
yes
$sp
29
stack pointer
yes
$fp
30
frame pointer
yes
$ra
31
return address
yes
M
C L
MIPS Instructions
Category
Instr
Op Code
Example
Meaning
Arithmetic
(R format)
add
0 and 32
add $s1, $s2, $s3
$s1 = $s2 + $s3
subtract
0 and 34
sub $s1, $s2, $s3
$s1 = $s2 - $s3
Data
transfer
(I format)
load word
35
lw
$s1, 100($s2)
$s1 = Memory($s2+100)
store word
43
sw $s1, 100($s2)
Memory($s2+100) = $s1
load byte
32
lb
$s1, 101($s2)
$s1 = Memory($s2+101)
store byte
40
sb
$s1, 101($s2)
Memory($s2+101) = $s1
br on equal
4
beq $s1, $s2, L
if ($s1==$s2) go to L
br on not equal
5
bne $s1, $s2, L
set on less than
0 and 42
Cond.
Branch
Uncond.
Jump
slt
$s1, $s2, $s3
if ($s1 !=$s2) go to L
if ($s2<$s3) $s1=1 else
$s1=0
2
j
2500
go to 10000
jump register
0 and 8
jr
$t1
go to $t1
jump and link
3
jal
2500
go to 10000; $ra=PC+4
jump
M
C L
Yoda says…
• Do or do not... there is no try
M
C L