Document 7663338

Download Report

Transcript Document 7663338

FAMU-FSU College of Engineering
Computer
Architecture
EEL 4713/5764, Spring 2006
Dr. Michael Frank
Module #7 – MIPS ISA, part 2:
Procedures and Data
1
Part II
Instruction-Set Architecture
June 2005
Computer Architecture, Instruction-Set Architecture
Slide 2
II Instruction Set Architecture
Introduce machine “words” and its “vocabulary,” learning:
• A simple, yet realistic and useful instruction set
• Machine language programs; how they are executed
• RISC vs CISC instruction-set design philosophy
Topics in This Part
Chapter 5 Instructions and Addressing
Chapter 6 Procedures and Data
Chapter 7 Assembly Language Programs
Chapter 8 Instruction Set Variations
June 2005
Computer Architecture, Instruction-Set Architecture
Slide 3
6 Procedures and Data
Finish our study of MiniMIPS instructions and its data types:
• Instructions for procedure call/return, misc. instructions
• Procedure parameters and results, utility of stack
Topics in This Chapter
6.1 Simple Procedure Calls
6.2 Using the Stack for Data Storage
6.3 Parameters and Results
6.4 Data Types
6.5 Arrays and Pointers
6.6 Additional Instructions
June 2005
Computer Architecture, Instruction-Set Architecture
Slide 4
6.1 Simple Procedure Calls
Using a procedure involves the following sequence of actions:
1.
2.
3.
4.
5.
6.
Put arguments in places known to procedure (reg’s $a0-$a3)
Transfer control to procedure, saving the return address (jal)
Acquire storage space, if required, for use by the procedure
Perform the desired task
Put results in places known to calling program (reg’s $v0-$v1)
Return control to calling point (jr)
MiniMIPS instructions for procedure call and return from procedure:
June 2005
jal
proc
# jump to loc “proc” and link;
# “link” means “save the return
# address” (PC)+4 in $ra ($31)
jr
rs
# go to loc addressed by rs
Computer Architecture, Instruction-Set Architecture
Slide 5
Illustrating a Procedure Call
main
PC
jal
proc
Prepare
to call
Prepare
to continue
proc
Save, etc.
Restore
jr
Figure 6.1
June 2005
$ra
Relationship between the main program and a procedure.
Computer Architecture, Instruction-Set Architecture
Slide 6
A Simple MiniMIPS Procedure
Example 6.1
Procedure to find the absolute value of an integer.
$v0  |($a0)|
Solution
int abs(int a0) {
int v0 = -a0;
if (a0 >= 0)
v0 = a0;
return v0;
} /* C equiv. */
The absolute value of x is –x if x < 0 and x otherwise.
abs: sub
$v0,$zero,$a0
bltz $a0,done
add $v0,$a0,$zero
done: jr
$ra
#
#
#
#
#
put -($a0) in $v0;
in case ($a0) < 0
if ($a0)<0 then done
else put ($a0) in $v0
return to calling program
In practice, we seldom use such short procedures because of the
overhead that they entail. In this example, we have 3-4
instructions of overhead for 3 instructions of useful computation.
June 2005
Computer Architecture, Instruction-Set Architecture
Slide 7
Nested Procedure Calls
main
PC
jal
abc
Prepare
to call
Prepare
to continue
abc
Procedure
abc
Save
xyz
jal
xyz
Procedure
xyz
Prep.
to call
Prep.
to continue
Save
Restore
jr
Figure 6.2
June 2005
$ra
Restore
jr
$ra
Example of nested procedure calls.
Computer Architecture, Instruction-Set Architecture
Slide 8
6.2 Using the Stack for Data Storage
sp
Push c
sp
c
b
a
Figure 6.4
push: addi
sw
June 2005
b
a
Pop x
sp
sp = sp – 4
mem[sp] = c
b
a
x = mem[sp]
sp = sp + 4
Effects of push and pop operations on a stack.
$sp,$sp,-4
$t4,0($sp)
pop: lw
addi
Computer Architecture, Instruction-Set Architecture
$t5,0($sp)
$sp,$sp,4
Slide 9
Memory
Map in
MiniMIPS
Hex address
00000000
Reserved
1 M words
Program
Text segment
63 M words
00400000
10000000
Addressable
with 16-bit
signed offset
Static data
10008000
1000ffff
Data segment
Dynamic data
$gp
$28
$29
$30
448 M words
$sp
$fp
Stack
Stack segment
7ffffffc
Second half of address
space reserved for
memory-mapped I/O
Figure 6.3
June 2005
Overview of the memory address space in MiniMIPS.
Computer Architecture, Instruction-Set Architecture
Slide 10
6.3 Parameters and Results
Stack allows us to pass/return an arbitrary number of values
$sp
Local
variables
z
y
..
.
Saved
registers
Frame for
current
procedure
Old ($fp)
$sp
$fp
c
b
a
..
.
Frame for
current
procedure
c
b
a
..
.
Frame for
previous
procedure
$fp
Before calling
Figure 6.5
June 2005
After calling
Use of the stack by a procedure.
Computer Architecture, Instruction-Set Architecture
Slide 11
Example of Using the Stack
Saving $fp, $ra, and $s0 onto the stack and restoring
them at the end of the procedure
proc:
sw
addi
addi
sw
sw
.
.
.
lw
lw
addi
lw
jr
June 2005
$fp,-4($sp)
$fp,$sp,0
$sp,$sp,–12
$ra,-8($fp)
$s0,-12($fp)
#
#
#
#
#
save the old frame pointer
save ($sp) into $fp
create 3 spaces on top of stack
save ($ra) in 2nd stack element
save ($s0) in top stack element
$s0,-12($fp)
$ra,-8($fp)
$sp,$fp, 0
$fp,-4($sp)
$ra
#
#
#
#
#
put top stack element in $s0
put 2nd stack element in $ra
restore $sp to original state
restore $fp to original state
return from procedure
Computer Architecture, Instruction-Set Architecture
Slide 12
6.4 Data Types
Data size (number of bits), data type (meaning assigned to bits)
Signed integer:
Unsigned integer:
Floating-point number:
Bit string:
byte
byte
byte
word
word
word
word
doubleword
doubleword
Converting from one size to another
Type
8-bit number Value
32-bit version of the number
Unsigned 0010 1011
Unsigned 1010 1011
43
171
0000 0000 0000 0000 0000 0000 0010 1011
0000 0000 0000 0000 0000 0000 1010 1011
Signed
Signed
+43
–85
0000 0000 0000 0000 0000 0000 0010 1011
1111 1111 1111 1111 1111 1111 1010 1011
June 2005
0010 1011
1010 1011
Computer Architecture, Instruction-Set Architecture
Slide 13
ASCII Characters
Table 6.1
ASCII (American standard code for information interchange)
0
0
NUL
1
DLE
2
SP
3
0
4
@
5
P
6
`
7
p
1
SOH
DC1
!
1
A
Q
a
q
2
STX
DC2
“
2
B
R
b
r
3
ETX
DC3
#
3
C
S
c
s
4
EOT
DC4
$
4
D
T
d
t
5
ENQ
NAK
%
5
E
U
e
u
6
ACK
SYN
&
6
F
V
f
v
7
BEL
ETB
‘
7
G
W
g
w
8
BS
CAN
(
8
H
X
h
x
9
HT
EM
)
9
I
Y
i
y
a
LF
SUB
*
:
J
Z
j
z
b
VT
ESC
+
;
K
[
k
{
c
FF
FS
,
<
L
\
l
|
d
CR
GS
-
=
M
]
m
}
e
SO
RS
.
>
N
^
n
~
f
SI
US
/
?
O
_
o
DEL
June 2005
Computer Architecture, Instruction-Set Architecture
8-9
a-f
More
More
controls
symbols
8-bit ASCII code
(col #, row #)hex
e.g., code for +
is (2b) hex or
(0010 1011)two
Slide 14
Loading and Storing Bytes
Bytes can be used to store ASCII characters or small integers.
MiniMIPS addresses refer to bytes, but registers hold words.
31
I
lb
$t0,8($s3)
lbu
$t0,8($s3)
sb
$t0,A($s3)
op
25
rs
#
#
#
#
#
20
rt
load rt with mem[8+($s3)]
sign-extend to fill reg
load rt with mem[8+($s3)]
zero-extend to fill reg
LSB of rt to mem[A+($s3)]
15
immediate / offset
0
1 0 x x 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
lb = 32
lbu = 36
sb = 40
Figure 6.6
June 2005
Base
register
Data
register
Address offset
Load and store instructions for byte-size data elements.
Computer Architecture, Instruction-Set Architecture
Slide 15
Meaning of a Word in Memory
Bit pattern
(02114020) hex
0000 0010 0001 0001 0100 0000 0010 0000
00000010000100010100000000100000
Add instruction
00000010000100010100000000100000
Positive integer
00000010000100010100000000100000
Four-character string
Figure 6.7
A 32-bit word has no inherent meaning and can be
interpreted in a number of equally valid ways in the absence of
other cues (e.g., context) for the intended meaning.
June 2005
Computer Architecture, Instruction-Set Architecture
Slide 16
6.5 Arrays and Pointers
Index: Use a register that holds the index i and increment the register in
each step to effect moving from element i of the list to element i + 1
Pointer: Use a register that points to (holds the address of) the list element
being examined and update it in each step to point to the next element
Array index i
Add 1 to i;
Compute 4i;
Add 4i to base
Base
Array A
A[i]
A[i + 1]
Pointer to A[i]
Add 4 to get
the address
of A[i + 1]
Array A
A[i]
A[i + 1]
Figure 6.8 Stepping through the elements of an array using the
indexing method and the pointer updating method.
June 2005
Computer Architecture, Instruction-Set Architecture
Slide 17
Selection Sort
Example 6.4
To sort a list of numbers, repeatedly perform the following:
Find the max element, swap it with the last item, move up the “last” pointer
A
A
first
first
max
A
first
x
swap
x&y
y
last
last
last
Start of iteration
Figure 6.9
June 2005
y
x
Maximum identified
End of iteration
One iteration of selection sort.
Computer Architecture, Instruction-Set Architecture
Slide 18
Selection Sort Using the Procedure max
Example 6.4 (continued)
A
A
first
Inputs to
proc max
first
in $a0
max
in $v1
in $a1
y
Outputs from
proc max
last
last
Start of iteration
June 2005
x
in $v0
last
sort: beq
jal
lw
sw
sw
addi
j
done: ...
A
first
$a0,$a1,done
max
$t0,0($a1)
$t0,0($v0)
$v1,0($a1)
$a1,$a1,-4
sort
#
#
#
#
#
#
#
#
y
x
Maximum identified
End of iteration
single-element list is sorted
call the max procedure
load last element into $t0
copy the last element to max loc
copy max value to last element
decrement pointer to last element
repeat sort for smaller list
continue with rest of program
Computer Architecture, Instruction-Set Architecture
Slide 19
6.6 Additional Instructions
MiniMIPS instructions for multiplication and division:
mult
div
$s0, $s1
$s0, $s1
mfhi
mflo
$t0
$t0
31
R
op
25
rs
20
rt
set
set
and
set
set
15
Hi,Lo to ($s0)($s1)
Hi to ($s0)mod($s1)
Lo to ($s0)/($s1)
$t0 to (Hi)
$t0 to (Lo)
rd
10
sh
5
fn
0
0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 x 0
ALU
instruction
Figure 6.10
#
#
#
#
#
Source
register 1
Source
register 2
Unused
Unused
mult = 24
div = 26
The multiply (mult) and divide (div) instructions of MiniMIPS.
31
R
op
25
rs
20
rt
15
rd
10
sh
5
fn
0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 x 0
ALU
instruction
Unused
Unused
Destination
register
Unused
mfhi = 16
mflo = 18
Figure 6.11 MiniMIPS instructions for copying the contents of Hi and Lo
registers into general registers .
June 2005
Computer Architecture, Instruction-Set Architecture
Slide 20
Logical Shifts
MiniMIPS instructions for left and right shifting:
sll
srl
sllv
srlv
$t0,$s1,2
$t0,$s1,2
$t0,$s1,$s0
$t0,$s1,$s0
31
R
op
25
20
rt
15
left-shifted by 2
right-shifted by 2
left-shifted by ($s0)
right-shifted by ($s0)
rd
10
sh
fn
5
0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 x 0
ALU
instruction
31
R
rs
# $t0=($s1)
# $t0=($s1)
# $t0=($s1)
# $t0=($s1)
op
Unused
25
rs
Source
register
20
rt
Destination
register
15
rd
Shift
amount
10
sh
sll = 0
srl = 2
fn
5
0
0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 x 0
ALU
instruction
Figure 6.12
June 2005
Amount
register
Source
register
Destination
register
Unused
sllv = 4
srlv = 6
The four logical shift instructions of MiniMIPS.
Computer Architecture, Instruction-Set Architecture
Slide 21
Unsigned Arithmetic and Miscellaneous Instructions
MiniMIPS instructions for unsigned arithmetic (no overflow exception):
addu
subu
multu
divu
$t0,$s0,$s1
$t0,$s0,$s1
$s0,$s1
$s0,$s1
addiu $t0,$s0,61
#
#
#
#
#
#
#
#
set $t0 to ($s0)+($s1)
set $t0 to ($s0)–($s1)
set Hi,Lo to ($s0)($s1)
set Hi to ($s0)mod($s1)
and Lo to ($s0)/($s1)
set $t0 to ($s0)+61;
the immediate operand is
sign extended
To make MiniMIPS more powerful and complete, we introduce later:
sra
$t0,$s1,2
srav $t0,$s1,$s0
syscall
June 2005
# sh. right arith (Sec. 10.5)
# shift right arith variable
# system call (Sec. 7.6)
Computer Architecture, Instruction-Set Architecture
Slide 22
The 20 MiniMIPS
Instructions
Copy
from Chapter 6
(40 in all so far)
Arithmetic
Table 6.2 (partial)
Shift
Memory access
Control transfer
June 2005
Instruction
Usage
Move from Hi
Move from Lo
Add unsigned
Subtract unsigned
Multiply
Multiply unsigned
Divide
Divide unsigned
Add immediate unsigned
Shift left logical
Shift right logical
Shift right arithmetic
Shift left logical variable
Shift right logical variable
Shift right arith variable
Load byte
Load byte unsigned
Store byte
Jump and link
System call
mfhi rd
mflo rd
addu rd,rs,rt
subu rd,rs,rt
mult rs,rt
multu rs,rt
div
rs,rt
divu rs,rt
addiu rs,rt,imm
sll
rd,rt,sh
srl
rd,rt,sh
sra
rd,rt,sh
sllv rd,rt,rs
srlv rt,rd,rs
srav rd,rt,rd
lb
rt,imm(rs)
lbu
rt,imm(rs)
sb
rt,imm(rs)
jal
L
syscall
Computer Architecture, Instruction-Set Architecture
op fn
0
0
0
0
0
0
0
0
9
0
0
0
0
0
0
32
36
40
3
0
Slide 23
16
18
33
35
24
25
26
27
0
2
3
4
6
7
12
Table 6.2 The 37 + 3 MiniMIPS Instructions Covered So Far
Instruction
Usage
Instruction
Usage
Load upper immediate
Add
Subtract
Set less than
Add immediate
Set less than immediate
AND
OR
XOR
NOR
AND immediate
OR immediate
XOR immediate
Load word
Store word
Jump
Jump register
Branch less than 0
Branch equal
Branch not equal
lui
add
sub
slt
addi
slti
and
or
xor
nor
andi
ori
xori
lw
sw
j
jr
bltz
beq
bne
Move from Hi
Move from Lo
Add unsigned
Subtract unsigned
Multiply
Multiply unsigned
Divide
Divide unsigned
Add immediate unsigned
Shift left logical
Shift right logical
Shift right arithmetic
Shift left logical variable
Shift right logical variable
Shift right arith variable
Load byte
Load byte unsigned
Store byte
Jump and link
mfhi
mflo
addu
subu
mult
multu
div
divu
addiu
sll
srl
sra
sllv
srlv
srav
lb
lbu
sb
jal
System call
syscall
June 2005
rt,imm
rd,rs,rt
rd,rs,rt
rd,rs,rt
rt,rs,imm
rd,rs,imm
rd,rs,rt
rd,rs,rt
rd,rs,rt
rd,rs,rt
rt,rs,imm
rt,rs,imm
rt,rs,imm
rt,imm(rs)
rt,imm(rs)
L
rs
rs,L
rs,rt,L
rs,rt,L
Computer Architecture, Instruction-Set Architecture
rd
rd
rd,rs,rt
rd,rs,rt
rs,rt
rs,rt
rs,rt
rs,rt
rs,rt,imm
rd,rt,sh
rd,rt,sh
rd,rt,sh
rd,rt,rs
rd,rt,rs
rd,rt,rs
rt,imm(rs)
rt,imm(rs)
rt,imm(rs)
L
Slide 24