Beruflich Dokumente
Kultur Dokumente
hardware
Coordination of many levels of abstraction Under a rapidly changing set of forces Design, measurement, and evaluation
3
Instruction Categories
G G G G
G
G G
P HI +O
immediate
,um! target
$ant an IS% that ma&es it easy to 'uild the hardware and the compiler while ma(imi)ing performance and minimi)ing cost Instructions are stored in memory *as is the data, similar to other IS%s developed since the /0123s
Design goals: maximize performance, minimize cost, reduce design time (time-to-market), minimize memory space (embedded systems), minimize power 5 consumption (mobile systems)
4ISC philosophy
G G G G
fi(ed instruction lengths load5store instruction sets limited num'er of addressing modes limited num'er of operations
#I"S, %4#, Sun S"%4C, I # "ower"C 6 Instruction sets are measured 'y how well compilers use them as opposed to how well assem'ly language programmers use them
CISC *C 6
/7 97 :7 ;7
Simplicity favors regularity7 Smaller is faster7 #a&e the common case fast7 <ood design demands good compromises7
Instruction Categories
G G G G
G
G G
P HI +O
#IPS &egisters
$hy not> ?eep =ardware Simple limited num'er of special locations 'uilt directly into the hardware operations can only 'e performed on these@
enefit. Since registers are implemented directly in hardware, they are very fast *faster than / 'illionth of a second,
#IPS &egisters
Draw'ac&. Since registers are in hardware, there are a predetermined num'er of them
G
Solution. #I"S code must 'e very carefully put together to efficiently use registers $hy :9> Large for the time, easier compiling Small now *'ut we are stuc& with it, <roups of :9 'its called a word in #I"S
:9 registers in #I"S
G G G
10
11
#I"S assem'ly language arithmetic statement add $t0, $s1, $s2 sub $t0, $s1, $s2 Aach arithmetic instruction performs only one operation Aach arithmetic instruction specifies e(actly three operands
destination source/
G
op
source9
The operands are contained in the datapathBs register file *$t0, $s1, $s2,
12
#I"S assem'ly language arithmetic statement add $t0, $s1, $s2 sub $t0, $s1, $s2 Aach arithmetic instruction performs only one operation Aach arithmetic instruction specifies e(actly three operands
destination source/
G
op
source9
The operands are contained in the datapathBs register file *$t0, $s1, $s2,
13
%ssuming varia'le ' is stored in register $s1, c is stored in $s2, and d is stored in $s3 and the result is to 'e left in $s0, what is the assem'ler eCuivalent to the C statement h = (b - c) + d
14
%ssuming varia'le ' is stored in register $s1, c is stored in $s2, and d is stored in $s3 and the result is to 'e left in $s0, what is the assem'ler eCuivalent to the C statement h = (b - c) + d
sub add
15
-ne particular immediate, the num'er )ero *2,, appears very often in code7 So we define register )ero ($0 or $zero, to always have the value 2D eg
add $s0,$s1,$zero *in #I"S, f = g *in C, where #I"S registers $s0,$s1 are associated with C varia'les f, g
Instructions, li&e registers and words of data, are also :9 'its long
G
Instruction !ormat.
op
rs
rt
rd
shamt
funct
222222 /222/
/22/2
2/222
22222
/22222
Instructions, li&e registers and words of data, are also :9 'its long
G
Instruction !ormat.
op
rs
rt
rd
shamt
funct
222222 /222/
/22/2
2/222
22222
/22222
&(format =3am!"e
op
6 bits
rs
5 bits
rt
5 bits
rd
5 bits
shamt
5 bits
funct
6 bits
000000100011001001000000001000002 = 0232402016
19
rs
E 'its
rt
E 'its
rd
E 'its
shamt
E 'its
funct
8 'its F :9 'its
op rs rt rd shamt funct
20
rs
E 'its
rt
E 'its
rd
E 'its
shamt
E 'its
funct
8 'its F :9 'its
op rs rt rd shamt funct
o!code indicating operation to 'e performed address of the first register source operand address of the second register source operand the register destination address shift amount *for shift instructions, function code that selects the specific variant of the operation specified in the opcode field
21
Small constants are used Cuite freCuently *E2G of operands in many common programs,
e7g7, % F % H ED F H /D C F C 5 /1D
"ut Itypical constantsJ in memory and load them Create hard5wired registers *li&e K)ero, for constants li&e /, 9, ;, /2, 6
#uch faster than if they have to 'e loaded from memory *they come in from memory with the instruction itself,
23
$hat instruction format is used for the addi > addi $s3, $s3, 4 #$s3 = $s3 + 4 #achine format.
24
$hat instruction format is used for the addi > addi $s3, $s3, 4 #$s3 = $s3 + 4 #achine format.
op 1 rs /0 rt /0 /8 'it immediate ;
I format
So must use the I format L Immediate format Limits immediate values to the range H9/EL/ to 59/E
25
26
Cut point777
27
#emory is a large, single5dimensional array %n address acts as the inde( into the memory array
#emory read addr/ :9 write addr Processor read data :9
:9write data /2 /2/ / 1 ; 2
28
:9 'its
F ; ytes F / $ord
Aach cell of the array has an address associated with it7 Aach cell also stores some value7 Do you thin& they use signed or unsigned num'ers> +egative address>@
DonBt confuse the address referring to a memory location with the value stored in that location7
...
23
42
...
29
%n address refers to a particular memory location7 In other words, it points to a memory location7 "ointer. % varia'le that contains the address of a varia'le7
#o$ation %address&
...
name
23
42
104
...
"
30
p p p
? ?
x x x
? 3 3
31
*p = 5;
32
Cut "oint777
33
%ccessing #emory
MIPS has two basic data transfer instructions for accessing memory (assume $s3 holds 2410 lw $t0! 4($s3 "load word from memory sw $t0! #($s3 "store word to memory $he data transfer instruction must s%ecify G where in memory to read from (load or write to (store & memory address G where in the register file to write to (load or read from (store & register destination (source $he memory address is formed by summing the constant %ortion of the instruction and the contents of the second register
34
%ccessing #emory
#I"S has two 'asic data transfer instructions for accessing memory *assume Ks: holds 9;/2, lw Kt2, ;*Ks:, Oload word from memory sw Kt2, 1*Ks:, Ostore word to memory
91/2 :9/2
The data transfer instruction must specify G where in memory to read from *load, or write to *store, L memory address G where in the register file to write to *load, or read from *store, L register destination *source, The memory address is formed 'y summing the constant portion of the instruction and the contents of the second register
35
The memory address is formed 'y summing the constant portion of the instruction and the contents of the second *'ase, register $s3 holds 1
#emory 7772//2 7772/2/ 777//22 777222/ 77722/2 777/222 7772/22 Data 9; 92 /8 /9 1 ; 2 $ord %ddress
%& $t0, 4($s3) #&hat is %oaded i'to $t0 s& $t0, 8($s3) #$t0 is stored &here(
36
The memory address is formed 'y summing the constant portion of the instruction and the contents of the second *'ase, register
#emory 7772//2 7772/2/ 777//22 777222/ 77722/2 777/222 7772/22 :9 'it Data 9; 92 /8 /9 1 ; 2 $ord %ddress
$s3 holds 1
7 7 7 222/
%& s&
$t0, 4($s3) #&hat( is %oaded i'to $t0 $t0, 8($s3) #$t0 is stored &here( 37
7 7 7 222/
in location /8
%ssuming varia'le b is stored in $s2 and that the 'ase address of array % is in $s3, what is the #I"S assem'ly code for the C statement )*8+ = )*2+ - b
777 %P:Q %P9Q %P/Q %P2Q 777 $s3H/9 $s3H1 $s3H; $s3
38
%ssuming varia'le b is stored in $s2 and that the 'ase address of array % is in $s3, what is the #I"S assem'ly code for the C statement )*8+ = )*2+ - b
777 %P:Q %P9Q %P/Q %P2Q 777 $s3H/9 $s3H1 $s3H; $s3
39
%ssuming that the 'ase address of array % is in register $s4, and varia'les ', c, and i are in $s1, $s2, and $s3, respectively, what is the #I"S assem'ly code for the C statement c = )*i+ - b
add add
40
%ssuming that the 'ase address of array % is in register $s4, and varia'les ', c, and i are in $s1, $s2, and $s3, respectively, what is the #I"S assem'ly code for the C statement c = )*i+ - b
$t1, $s3, $s3 $t1, $t1, $t1 $t1, $t1, $s4 $s2, $t0, $s1
#arra, i'de! i is i' $s3 #te-# reg $t1 ho%ds 4$i #addr of )*i+ 'o& i' $t1
41
%rithmetic add su'tract %rithmetic add immediate Data transfer load word store word
2R :9 2R :; 1 :E ;:
add Ks/, Ks9, Ks: Ks/ F Ks9 H Ks: su' Ks/, Ks9, Ks: Ks/ F Ks9 5 Ks: addi Ks/, Ks9, ; lw Ks/, /22*Ks9, sw Ks/, /22*Ks9, Ks/ F Ks9 H ; Ks/ F #emory*Ks9H/22, #emory*Ks9H/22, F Ks/
42
Cut point7777
43
* * *
999
* * * *
/it
999
.). ( *
44
5*9: 5 /, F
9Bsc 'inary /222 /22/ /2/2 /2// //22 //2/ ///2 //// 2222 222/ 22/2 22// 2/22 2/2/ 2//2 2///
decimal 51 5S 58 5E 5; 5: 59 5/ 2 / 9 : ; E 8 S
complement all the 'its 2/2/ and add a / 2//2 /2// and add a / /2/2 complement all the 'its
45
9: 5 / F
I5type for data transfer instructions *previous format was 45type for register,
46
I5type for data transfer instructions *previous format was 45type for register,
47
A(ample.
2410 + $s2 =
2(22222229 2(/922;20;
$s2
data
A(ample.
2410 + $s2 =
$t0
2(22222229 2(/922;2ac 2(/922;20;
$s2
data
;:
/1
9;
/2/2//
/22/2
2/222
22222222222//222
% /85'it offset means access is limited to memory locations within a range of 9/: F 1,/09 words *9/E F :9,S81 'ytes, of the address in the 'ase register $s2
G
50
;:
/1
9;
/2/2//
/22/2
2/222
22222222222//222
% /85'it offset means access is limited to memory locations within a range of H9/:5/ to 59/: *T1,/09, words *H9/E5/ to 59/E *T:9,S81, 'ytes, of the address in the 'ase register $s2
G
51
Can reduce the comple(ity with multiple formats 'y &eeping them as similar as possi'le
G
4 4 I I I
reg reg +% +% +%
2 2 +% +% +%
:9ten :;ten +% +% +%
OP OP
rs rs
rt rt
rd
sa
funct
52
immediate
Cut "oint
53
54
Beyond :um/ers
#ost computers use 15'it 'ytes to represent characters with the %merican Std Code for Info Interchange *%SCII,
har
+ull AS II :9 :: :; :E :8 :S :1 :0 ;2 ;/ ;9 ;: !! ;;
har
space @ I O K G R 3 * , N H ,
AS II ;1 ;0 E2 E/ E9 E: E; EE E8 ES E1 E0 82
har
2 / 9 : ; E 8 S 1 0 . D Y
AS II 8; 8E 88 8S 81 80 S2 S/ S9 S: S; SE S8
har
V %
AS II 08 0S 01
har
W a ' c d e f g h i M & l
AS II //9 //: //; //E //8 //S //1 //0 /92 /9/ /99 /9: /9;
har
p C r s t u v w ( y ) X Z
C D A ! < = I J ? L
/9
/E
/9S
DAL
Byte Addresses
Since 15'it 'ytes are so useful, most architectures address individual 'ytes in memory Therefore, the memory address of a word must 'e a multiple of ; *alignment restriction, ig Andian. address
56
=ndianess
ig Andian. leftmost 'yte is word address Little Andian. rightmost 'yte is word address
"itt"e endian
ms/ "s/
/ig endian
57
=ndianess
A(ample. consider the num'er 2(;2/ stored in a ;5'yte integer In 'inary 22222222 22222222 22222/22 2222222/
00
'i( )ndian #ittle )ndian
01
02
03
58
#I"S provides special instructions to move 'ytes %b $t0, 1($s3) #%oad b,te fro- -e-or, sb $t0, .($s3) #store b,te to -e-or,
op rs rt /8 'it num'er
load 'yte places the 'yte from memory in the rightmost 1 'its of the destination register store 'yte ta&es the 'yte from the rightmost 1 'its of a register and writes it to the 'yte in memory leaving the other 'ytes in the memory word unchanged
59
<iven following code seCuence and memory state what is the state of the memory after e(ecuting the code> add $s3, $zero, $zero %b $t0, 1($s3) sb $t0, .($s3)
#emory 2( 2 2 2 2 2 2 2 2 2( 2 2 2 2 2 2 2 2 2( 2 2 2 2 2 2 2 2 2( / 2 2 2 2 2 / 2 2( 2 / 2 2 2 ; 2 9 2( ! ! ! ! ! ! ! ! 2( 2 2 0 2 / 9 % 2
Data
9; 92 /8 /9 1 ; 2
$hat value is left in Kt2> $hat word is changed in #emory and to what> $hat if the machine was little Andian>
60
<iven following code seCuence and memory state what is the state of the memory after e(ecuting the code> add $s3, $zero, $zero %b $t0, 1($s3) sb $t0, .($s3)
#emory 2( 2 2 2 2 2 2 2 2 2( 2 2 2 2 2 2 2 2 2( 2 2 2 2 2 2 2 2 2( / 2 2 2 2 2 / 2 2( 2 / 2 2 2 ; 2 9 2( ! ! ! ! ! ! ! ! 2( 2 2 0 2 / 9 % 2
Data
9; 92 /8 /9 1
61
$hat if the machine was little ; Andian> Kt2 F 2(222222/9 2 $ord mem*;, F 2(!!/9!!!! %ddress *Decimal,
A"ignment
2 ligned / 9 :
!ot ligned
Ali(nment restri$tion* re+,ires that o-.e$ts fall on address that is a m,ltiple of their si/e.
62
:95'it word has to start at 'yte address that is multiple of ;D Thus, :95'it word at address ;n includes four 'ytes with addresses. ;n, ;nH/, ;nH9, and ;nH:7 /85'it half word has to start at 'yte address that is multiple of 9D Thus, /85'it word at address 9n includes two 'ytes with addresses. 9n and 9nH/7 %n address is given as :95'it unsigned integerD
63
Cut point777
64
alter the control flow i7e7, change the \ne(t\ instruction to 'e e(ecuted
#I"S conditional 'ranch instructions. b'e $s0, $s1, /b% be0 $s0, $s1, /b% #go to /b% if $s0$s1 #go to /b% if $s0=$s1
b'e $s0, $s1, /b%1 add $s3, $s0, $s1 /b%12 333
65
66
#I"S also has an unconditional 'ranch instruction or Mump instruction. 1 /b% #go to /b% if (i6=1) h=i+1" e%se h=i-1" be0 $s0, $s1, 7%se add $s3, $s0, $s1 1 7!it sub $s3, $s0, $s1 333
A(ample.
7%se2 7!it2
67
Compile the assem'ly code for the C &hi%e loop where i is in $s0, 1 is in $s1, and 8 is in $s2 &hi%e (i6=8) i=i+1" /oo#2 be0 $s0, $s2, 7!it add $s0, $s0, $s1 1 /oo# 7!it2 3 3 3
asic 'loc& L % seCuence of instructions without 'ranches *e(cept at the end, and without 'ranch targets *e(cept at the 'eginning,
68
$hat if the 'ranch destination is further away than can 'e captured in /8 'its>
The
assem'ler comes to the rescue L it inserts an unconditional Mump to the 'ranch target and inverts the condition
be0 $s0, $s1, /1
'ecomes
b'e $s0, $s1, /2 1 /1 /22
69
Cut "oint
70
%rithmetic add su'tract add immediate Data transfer Cond7 'ranch load word store word 'r on eCual 'r on not eCual
2 R 92 add Ks/, Ks9, Ks: Ks/ F Ks9 H Ks: 2 R 99 su' Ks/, Ks9, Ks: Ks/ F Ks9 5 Ks: 1 9: 9' ; E addi Ks/, Ks9, ; lw Ks/ F Ks9 H ; Ks/, /22*Ks9, Ks/ F #emory*Ks9H/22, if *Ks/FFKs9, go to L if *Ks/ @FKs9, go to L
sw Ks/, /22*Ks9, #emory*Ks9H/22, F Ks/ 'eC Ks/, Ks9, L 'ne Ks/, Ks9, L
71
4 4 I I I
reg reg +% +% +%
2 2 +% +% +%
:9ten :;ten +% +% +%
72
Assem/"ing Branches
Instructions.
b'e $s0, $s1, /b% be0 $s0, $s1, /b% #go to /b% if $s0$s1 #go to /b% if $s0=$s1
#achine !ormats.
op rs rt /8 'it num'er
I format
E ;
/8 /8
/S /S
>>>> >>>>
73
S!ecifying Branch Destinations Could specify the memory address 'ut that would reCuire a :9 'it field
Could use a I'aseJ register and add to it the /85'it offset G which register> G Instruction %ddress 4egister *"C F program counter, 5 its use is automatically implied 'y 'ranch
G
"C gets updated *"CH;, during the fetch cycle so that it holds the address of the ne(t instruction limits the 'ranch distance to 59/E to H9/E5/ instrBs from the *instruction after the, 'ranch 'ut most 'ranches are local anyway
74
The contents of the updated "C *"CH;, is added to the /8 'it 'ranch offset which is converted into a :9 'it value 'y
G
concatenating two low5order )eros to ma&e it a word address and then sign5e(tending those /1 'its
The result is written into the "C if the 'ranch condition is true 5 'efore the ne(t !etch cycle
from the low order /8 'its of the 'ranch instruction
/8
sign5e(tend
offset
22 :9 :9 %dd :9 ; :9 %dd :9
"C
:9
:9
75
Offset Tradeoffs
$hy not Must store the 'yte offset in the low order /8 'its> Then the two low order )eros wouldnBt have to 'e concatenated, it would 'e less confusing, 6
ut that would limit the 'ranch distance to 59/: to H9/:5/ instrBs from the *instruction after the, 'ranch %nd concatenating the two )ero 'its costs us very little in additional hardware and has no impact on the cloc& cycle time
76
I format
/8
/S
4emem'er
%fter
the b'e instruction is fetched, the "C is updated so that it is addressing the add instruction *"C F "C H ;,7 The offset *plus 9 low5order )eros, is sign5e(tended and added to the *updated, "C 77
I format
/8
/S
2(222/
4emem'er
%fter
the b'e instruction is fetched, the "C is updated so that it is addressing the add instruction *"C F "C H ;,7 The offset *plus 9 low5order )eros, is sign5e(tended and added to the *updated, "C 78
Assem/"ing Dum!s
#go to /b%
J format
79
The low order 98 'its of the Mump instr converted into a :9 'it Mump destination address 'y
G
concatenating two low5order )eros to create an 91 'it *word, address and then concatenating the upper ; 'its of the current "C *now "CH;, to create a :9 'it *word, address
that is put into the "C prior to the ne(t !etch cycle
22 :9 ;
"C
:9
80
81
4 0 2
1. 17
0!0040002c 0!00400030
0 333
1. 17 19 0
0!22
82
Cut "oint
83
$e have be0, b'e, 'ut what a'out 'ranch5if5less5than> +ew instruction. s%t $t0, $s0, $s1 # if $s0 : $s1 # the' # $t0 = 1 # e%se # $t0 = 0 #achine format.
op
rs
rt
rd
funct
4 format
/8
/S
;9 F 2(9a 9
84
Since constant operands are popular in comparisons, also have s%ti +ew instruction. s%ti $t0, $s0, 10 # if $s0 : 10 # the' # $t0 = 1 # e%se # $t0 = 0 #achine format.
op rs rt /8 'it num'er
I format
/8
2(222a 9
85
Can use s%t, be0, b'e, and the fi(ed value of 2 in $zero to create all relative conditions
G
less than
G G G
%s pseudo instructions they are recogni)ed *and e(panded, 'y the assem'ler The assem'ler needs a reserved register * $at,
86
Can use s%t, be0, b'e, and the fi(ed value of 2 in $zero to create all relative conditions
G
b%t $s1, $s2, /b% $at, $s1, $s2 #$at set to 1 if $at, $zero, /b% # $s1 : $s2 b%e $s1, $s2, /b% bge $s1, $s2, /b% bgt $s1, $s2, /b%
G G G
%s pseudo instructions they are recogni)ed *and e(panded, 'y the assem'ler The assem'ler needs a reserved register * $at,
87
-ther "seudo5instructions
88
Cut "oint
89
#IPS Organi1ation
%rithmetic instructions L to/from the register file Load/store instructions 5 to/from memory
Processor
4egister !ile src/ addr src9 addr dst addr write data E E E :9 :9 'its :9 %LU :9 :9 registers *K)ero 5 Kra, src/ data :9
#emory
/6//22
read/write addr
:9
9:2 words
:9
src9 :9 data
:9
:9 'its
90
-perands of arithmetic instructions must 'e from a limited num'er of special locations contained in the datapathBs register file
G
G G
data
data
91
4egisters are
G
!ast
G
G
Since registers are named with fewer 'its than a memory location
92
Instructions are represented as num'ers "rograms are stored in memory to 'e read or written, Must li&e num'ers
#emory
Stored5program
G "rograms
concept
can 'e shipped as files of 'inary num'ers L 'inary compati'ility G Computers can inherit ready5made software provided they are compati'le with an e(isting IS% L leads industry to align around a small num'er of IS%s
93
%ccounting prg *machine code, C compiler *machine code, "ayroll data Source code in C for %cct prg
Programming Sty"es
"rocedures
G
"rocedures
G
94
routine *caller, places parameters in a place where the procedure *callee, can access them
$a0 5 $a3. four argument registers
Caller
transfers control to the callee Callee acCuires the storage resources needed Callee performs the desired tas& Callee places the result value in a place where the caller can access it
G
Callee
G
Instruction for a""ing a Procedure #I"S procedure call instruction. 1a% 4roc)ddress #1u-# a'd %i'8 Saves "CH; in register $ra as the lin& to the following instruction to set up the procedure return #achine format.
op : 98 'it address >>>>
J format
Then
a procedure that computes the <CD of two values i *in $t0, and 1 *in $t1, gcd(i,1)" caller puts the i and 1 *the parameters values, in $a0 and $a1 and issues a 1a% gcd #1u-# to routi'e gcd
The
The
callee computes the <CD, puts the result in $;0, and returns control to the caller using gcd2 3 3 3 #code to co-#ute gcd 1r $ra #retur'
97
Cut "oint
98
S!i""ing &egisters
$hat
G it
high addr
if the callee needs to use more registers than allocated to argument and return values>
uses a stac& L a last5in5first5out Cueue
to# of stac8
$s#
-ne of the general registers, $s# *$29,, is used to address the stac& *which IgrowsJ from high address to low address,
G
%o& addr
99
om!i"ing a +eaf Procedure Leaf procedures are ones that do not call other procedures7 <ive the #I"S assem'ler code for
i't %eaf<e! (i't g, i't h, i't i, i't 1)= i't f" f = (g+h) > (i+1)" retur' f" ? (assu-e the $s registers are u'a;ai%ab%e)
100
om!i"ing a +eaf Procedure Leaf procedures are ones that do not call other procedures7 <ive the #I"S assem'ler code for
i't %eaf<e! (i't g, i't h, i't i, i't 1)= i't f" f = (g+h) > (i+1)" retur' f" ?
101
=3am!"e
<iven the following C code i't -ai'() (i't g, i't h, i't i, i't 1) = i't ," 333 ,=diffofsu-s(2,3,4,@)" 333 ? i't diffofsu-s(i't f, i't g, i't h, i't i) = i't resu%t" resu%t = (f+g)-(h+i)" retur' resu%t" ? !ill in the 'lan&s in the following translation of this C code into #I"S (registers $s0, $s1, and $s2 must be sa ed and restored). # $s0=, -ai'2
diffofsu-s2
!!! addi $a0,$0,2 addi $a1,$0,3 addi $a2,$0,4 addi $a3,$0,< 1a% <<<<<<<< !!! # $s0=resu%t
#ddi $sp, $sp, $$$$% s& $s0, <<<<< # s& $s1, <<<<< # s& $s2, <<<<< # #dd $$$, $$$, $$$ % #dd $$$, $$$, $$$ % sub $$$, $$$, $$$ % #dd $$$, $$$, $$$ % l& $s2, $$$$$ # l& $s1, <<<<< # l& $s0, <<<<< # #ddi $sp,$sp, $$ % 0r $$ %
&#'( sp#)( on st#)' to stor( t*r(( r(+ist(rs sa;e $s0 o' stac8 sa;e $s1 o' stac8 sa;e $s2 o' stac8 $t0=f,+ $t-=*,i r(sult = (f,+).(*,i) put r(turn /#lu( in $/0 restore $s2 fro- stac8 restore $s1 fro- stac8 restore $s0 fro- stac8 d(.#llo)#t( st#)' sp#)( r(turn to )#ll(r
102
=3am!"e
<iven the following C code i't -ai'() (i't g, i't h, i't i, i't 1) = i't ," 333 ,=diffofsu-s(2,3,4,@)" 333 ? i't diffofsu-s(i't f, i't g, i't h, i't i) = i't resu%t" resu%t = (f+g)-(h+i)" retur' resu%t" ? !ill in the 'lan&s in the following translation of this C code into #I"S *registers $s0, $s1, and $s2 must be sa ed and restored).
# $s0=, -ai'2
diffofsu-s2
!!! addi $a0,$0,2 addi $a1,$0,3 addi $a2,$0,4 addi $a3,$0,@ 1a% diffofsu-s !!! # $s0=resu%t #ddi $sp, $sp, .s& $s0, 8(s#) s& $s1, 4(s#) s& $s2, 0(s#) #dd $s-, $#0, $##dd $s , $# , $#3 sub $s0, $s-, $s #dd $/0, $s0, $0 l& $s2, 0(s#) l& $s1, 4(s#) l& $s0, 8(s#) #ddi $sp,$sp, 0r $r#
103
% # # # % % % % # # # % %
&#'( sp#)( on st#)' to stor( t*r(( r(+ist(rs sa;e $s0 o' stac8 sa;e $s1 o' stac8 sa;e $s2 o' stac8 $s-=f,+ $s =*,i r(sult = (f,+).(*,i) put r(turn /#lu( in $/0 restore $s2 fro- stac8 restore $s1 fro- stac8 restore $s0 fro- stac8 d(.#llo)#t( st#)' sp#)( r(turn to )#ll(r
Cut "oint
104
:ested Procedures
$hat
happens to return addresses with nested procedures> i't rt<1 (i't i) = if (i == 0) retur' 0" e%se retur' rt<2(i-1)" ?
ca%%er2 1a% 'e!t2 3 3 3 rt<1
rt<12 b'e $a0, $zero, to<2 add $;0, $zero, $zero 1r $ra to<22 addi $a0, $a0, -1 1a% rt<2 1r $ra rt<22 3 3 3 105
-n the call to rt<1, the return address *'e!t in the caller routine, gets stored in $ra7 $hat happens to the value in $ra *when i 6= 0, when rt<1 ma&es a call to rt<2>
106
high addr
o%d ABC
$s#
%o& addr
$ra
107
high addr
o%d ABC
$s#
108
high addr
$s# $s#
109
high addr
$s# $s#
110
high addr
$s#
%o& addr
111
high addr
$s#
$s#
%o& addr
112
114
$f#
segment of the stac& containing a procedureBs saved registers and local varia'les is its procedure frame *a&a activation record,
G The
$s#
frame pointer *$f#, points to the first word of the frame of a procedure L providing a sta'le I'aseJ register for the procedure
$f# is initiali)ed using $s# on a call and $s# is restored using $f# on a return
115
data segment for constants and other static varia'les *e7g7, arrays, Dynamic data segment *a&a heap, for structures that grow and shrin& *e7g7, lin&ed lists,
G
Ksp
#emory Stac%
2( Sf f f f f f c
Kgp
%llocate space on the heap with -a%%oc() and free it with free() 116
"C
&eser2ed
fact *2, F / fact */, F / N / F / fact *9, F 9 N / N / F 9 fact *:, F : N 9 N / N / F 8 fact *;, F ; N : N 9 N / N / F 9; 777
$s#
Stac& state after e(ecution of first encounter with the 1a% instruction *second call to fact routine with $a0 now holding /,
G
$ra $a0 G
save return address to caller routine *i7e7, location in the main routine where first call to fact is made, on the stac& save original value of $a0 on the stac&
119
$;0
Stac& state after e(ecution of first encounter with the 1a% instruction *second call to fact routine with $a0 now holding /,
G
ca%%er rt addr 2
saved return address to caller routine *i7e7, location in the main routine where first call to fact is made, on the stac& saved original value of $a0 on the stac&
120
Stac& state after e(ecution of first encounter with the 1a% instruction *second call to fact routine with $a0 now holding /,
G
ca%%er rt addr 2
saved return address to caller routine *i7e7, location in the main routine where first call to fact is made, on the stac& saved original value of $a0 on the stac&
121
Stac& state after e(ecution of first encounter with the 1a% instruction *second call to fact routine with $a0 now holding /,
G
saved return address to caller routine *i7e7, location in the main routine where first call to fact is made, on the stac& saved original value of $a0 on the stac&
122
Stac& state after e(ecution of second encounter with the 1a% instruction *third call to fact routine with $a0 now holding 2,
G
b8<f 1
saved return address of instruction in caller routine *instruction after 1a%, on the stac& saved previous value of $a0 on the stac&
123
Stac& state after e(ecution of second encounter with the 1a% instruction *third call to fact routine with $a0 now holding 2,
G
b8<f 0 1
saved return address of instruction in caller routine *instruction after 1a%, on the stac& saved previous value of $a0 on the stac&
124
Stac& state after e(ecution of first encounter with the first 1r instruction *$;0 initiali)ed to /,
G
b8<f 0
125
Stac& state after e(ecution of first encounter with the first 1r instruction *$;0 initiali)ed to /,
G
b8<f 0 1
126
Stac& state after e(ecution of first encounter with the first 1r instruction *$;0 initiali)ed to /,
G
b8<f 0 1
127
Stac& state after e(ecution of first encounter with the first 1r instruction *$;0 initiali)ed to /,
G
b8<f 0 1
128
Stac& state after e(ecution of first encounter with the second 1r instruction *return from fact routine after updating $;0 to / N /,
G
b8<f 0 1 1
G G
return address to caller routine *b8<f in fact routine, restored to $ra from the stac& previous value of $a0 restored from the stac& stac& pointer updated to point to second call to fact
129
Stac& state after e(ecution of first encounter with the second 1r instruction *return from fact routine after updating $;0 to / N /,
G
b8<f 0 1 1
G G
return address to caller routine *b8<f in fact routine, restored to $ra from the stac& previous value of $a0 restored from the stac& stac& pointer updated to point to second call to fact
130
Stac& state after e(ecution of first encounter with the second 1r instruction *return from fact routine after updating $;0 to / N /,
G
b8<f 0 1 1 $ 11
G G
return address to caller routine *b8<f in fact routine, restored to $ra from the stac& previous value of $a0 restored from the stac& stac& pointer updated to point to second call to fact
131
Stac& state after e(ecution of second encounter with the second 1r instruction *return from fact routine after updating $;0 to 9 N / N /,
G
$ra $a0
G G
132
$;0
return address to caller routine *main routine, restored to $ra from the stac& original value of $a0 restored from the stac& stac& pointer updated to point to first call to fact
Stac& state after e(ecution of second encounter with the second 1r instruction *return from fact routine after updating $;0 to 9 N / N /,
G
$ra $a0
G G
133
$;0
return address to caller routine *main routine, restored to $ra from the stac& original value of $a0 restored from the stac& stac& pointer updated to point to first call to fact
Stac& state after e(ecution of second encounter with the second 1r instruction *return from fact routine after updating $;0 to 9 N / N /,
G
$ra $a0
G G
134
$;0
return address to caller routine *main routine, restored to $ra from the stac& original value of $a0 restored from the stac& stac& pointer updated to point to first call to fact
Cut "oint
135
also li&e to 'e a'le to load a :95'it constant into a register #ust use two instructions, new \load upper immediate\ instruction %ui $t0, 0!aaaa
/8 2 1 /2/2/2/2/2/2/2/2
Then
must get the lower order 'its right, i7e7, ori $t0, $t0, 0!aaaa
/2/2/2/2/2/2/2/2 2222222222222222 2222222222222222 /2/2/2/2/2/2/2/2
136
also li&e to 'e a'le to load a :95'it constant into a register #ust use two instructions, new \load upper immediate\ instruction %ui $t0, 0!aaaa
f 2 1 /2/2/2/2/2/2/2/2
Then
must get the lower order 'its right, i7e7, ori $t0, $t0, 0!aaaa
/2/2/2/2/2/2/2/2 2222222222222222 /2/2/2/2/2/2/2/2 137 2222222222222222 /2/2/2/2/2/2/2/2 /2/2/2/2/2/2/2/2
Shift O!erations
+eed
operations to pac& and unpac& 15'it characters into :95'it words Shifts move all the 'its in a word left or right
s%% $t2, $s0, 8 sr% $t2, $s0, 8
op rs rt
4 format
/8
/2
2(22
Such
that a E5'it shamt field is enough to shift a :95'it value 9E L / or :/ 'it positions 138
arithmetic shift *sra, maintain the arithmetic correctness of the shifted value *i7e7, a num'er shifted right one 'it should 'e ^ of its original valueD a num'er shifted left should 'e 9 times its original value,
uses the most significant 'it *sign 'it, as the 'it shifted in G s%% wor&s for arithmetic left shifts for 9Bs compl7 *so there is no need for a s%a, sra $t2, $s0, 8 #$t2 = $s0 DD 8 bits
G sra
op rs rt rd shamt funct
4 format
139
/8
/2
2(2:
the assem'ly code for the C &hi%e loop where i is in $s3, 8 is in $s@, and the 'ase address of the array sa;e is in $s. &hi%e (sa;e*i+ == 8) i += 1"
140
the assem'ly code for the C &hi%e loop where i is in $s3, 8 is in $s@, and the 'ase address of the array sa;e is in $s. &hi%e (sa;e*i+ == 8) i += 1"
/oo#2 s%% $t1, $s3, 2 add $t1, $t1, $s. %& $t0, 0($t1) b'e $t0, $s@, 7!it addi $s3, $s3, 1 1 /oo# 7!it2 3 3 3
141
Cut "oint
142
%rithmetic *4 R I format,
add su'tract add immediate shift left logical shift right logical shift right arithmetic and or nor and immediate or immediate load upper immediate
2 R 92 2 R 99 1 2 R 22 2 R 29 2 R 2: 2 R 9; 2 R 9E 2 R 9S c d f
add Ks/, Ks9, Ks: su' Ks/, Ks9, Ks: addi Ks/, Ks9, ; sll srl Ks/, Ks9, ; Ks/, Ks9, ;
Ks/ F Ks9 H Ks: Ks/ F Ks9 5 Ks: Ks/ F Ks9 H ; Ks/ F Ks9 YY ; Ks/ F Ks9 __ ; *fill with )eros, Ks/ F Ks9 __ ; *fill with sign 'it, Ks/ F Ks9 R Ks: Ks/ F Ks9 Z Ks: Ks/ F not *Ks9 Z Ks:, Ks/ F Ks9 R 2(ff22 Ks/ F Ks9 Z 2(ff22
Ks/ F 2(ffff2222
sra Ks/, Ks9, ; and Ks/, Ks9, Ks: or Ks/, Ks9, Ks: nor Ks/, Ks9, Ks: and Ks/, Ks9, ff22 or Ks/, Ks9, ff22
lui Ks/, 2(ffff
143
Instr
O!
=3am!"e
#eaning
load word store word load 'yte store 'yte load half store half
9: 9' 92 91 9/ 90 ; E a 2 R 9a 9 2 R 21
lw l' lh
Ks/ F #emory*Ks9H/22, #emory*Ks9H/22, F Ks/ Ks/ F #emory*Ks9H/2/, #emory*Ks9H/2/, F Ks/ Ks/ F #emory*Ks9H/29, #emory*Ks9H/29, F Ks/ if *Ks/FFKs9, go to L if *Ks/ @FKs9, go to L if *Ks9Y/22, Ks/F/D Ks/F2 else
sw Ks/, /22*Ks9, s' Ks/, /2/*Ks9, sh Ks/, /2/*Ks9, 'eC Ks/, Ks9, L 'ne Ks/, Ks9, L slti Ks/, Ks9, /22 slt Ks/, Ks9, Ks: M Mr 9E22 Kt/
'r on eCual 'r on not eCual set on less than immediate set on less than
Uncond7 Mump
144
Categories
&egisters
&' ( &)*
G G
P HI +O
145
#IPS Organi1ation
Processor
4egister !ile src/ addr src9 addr dst addr write data E E E :9 :9 'its 'r offset :9 "C ; :9 %dd :9 :9 %LU :9 :9 %dd :9 :9 registers *K)ero 5 Kra, src/ :9 data
#emory
/6//22
src9 data :9
read/write addr
:9
9:2 words
read data
:9 :9
write data
:9 :9 ; 2 E / 8 9 S :
Decode
146
addressing L operand is in a register ase *displacement, addressing L operand is at the memory location whose address is the sum of a register and a /85'it constant contained within the instruction Immediate addressing L operand is a /85'it constant contained within the instruction "C5relative addressing Linstruction address is the sum of the "C and a /85'it constant contained within the instruction "seudo5direct addressing L instruction address is the 985'it constant contained within the instruction concatenated with the upper ; 'its of the "C
147
/7 4egister addressing
op rs rt rd
4egister
word operand
97 ase addressing
op rs rt offset
#emory
word or 'yte operand
'ase register
:7 Immediate addressing
op op rs rs rt rt operand offset
E7 "seudo5direct addressing
op Mump address ZZ "rogram Counter *"C, 148
#emory
Mump destination instruction