Τελεστέοι Υλικού Υπολογιστών

Download Report

Transcript Τελεστέοι Υλικού Υπολογιστών

Οργάνωση Υπολογιστών
5 “συστατικά” στοιχεία
-Επεξεργαστής:
datapath (δίοδος δεδομένων) (1) και control (2)
-Μνήμη (3)
-Συσκευές Εισόδου (4), Εξόδου (5) (Μεγάλη ‘ποικιλία’ !!)
Συσκευές γρήγορες π.χ. κάρτες γραφικών, αργές π.χ. πληκτρολόγιο.
Για το Ι/Ο έχει γίνει η λιγότερη έρευνα …..(I/O busses , I/O switched fabrics …)
Ιεραρχία Μνήμης: καταχωρητές, κρυφή μνήμη (L1), κρυφή
μνήμη (L2), κύρια Μνήμη- ΠΟΛΥ ΣΗΜΑΝΤΙΚΟ ΣΤΟΙΧΕΙΟ!
30/10/2015
cslab@ntua 2010-2011
1
Αρχιτεκτονικές Συνόλου Εντολών
Instruction Set Architectures
Instruction
Fetch (IF)
Instruction
Decode
(ID)
Operand
Fetch (OF)
Execute
(Ex)
Result
Store (WB)
Next
Instruction
30/10/2015
Αριθμός εντολών
Μορφή Εντολών:
μεταβλητό ή σταθερό μέγεθος bytes για κάθε
εντολή; (8086 1-17 bytes, MIPS 4 bytes)
Πώς γίνεται η αποκωδικοποίηση (ID);
Που βρίσκονται τα ορίσματα (operands) και το
αποτέλεσμα:
Μνήμη-καταχωρητές, πόσα ορίσματα, τι
μεγέθους;
Ποια είναι στη μνήμη και ποια όχι;
Πόσοι κύκλοι για κάθε εντολή;
cslab@ntua 2010-2011
2
Κατηγορίες Αρχιτεκτονικών Συνόλου Εντολών
(ISA Classes)
1. Αρχιτεκτονικές Συσσωρευτή (accumulator architectures)
(μας θυμίζει κάτι?)
2. Αρχιτεκτονικές επεκταμένου συσσωρευτή ή καταχωρητών
ειδικού σκοπού (extended accumulator ή special purpose
register)
3. Αρχιτεκτονικές Καταχωρητών Γενικού Σκοπού
3α. register-memory
3b. register-register (RISC)
30/10/2015
cslab@ntua 2010-2011
3
Αρχιτεκτονικές Συσσωρευτή (1)
1η γενιά υπολογιστών: h/w ακριβό, μεγάλο μέγεθος καταχωρητή.
Ένας καταχωρητής για όλες τις αριθμητικές εντολές (συσσώρευε
όλες τις λειτουργίες  Συσσωρευτής (Accum)
Σύνηθες: 1ο όρισμα είναι ο Αccum, 2o η μνήμη, αποτέλεσμα στον
Accum π.χ. add 200
Παράδειγμα:
A=B+C
Accum = Memory(AddressB);
Load AddressB
Accum = Accum + Memory(AddressC);
Add AddressC
Memory(AddressA) = Accum;
Store AddressA
Όλες οι μεταβλητές αποθηκεύονται στη μνήμη. Δεν υπάρχουν
βοηθητικοί καταχωρητές
30/10/2015
cslab@ntua 2010-2011
4
Αρχιτεκτονικές Συσσωρευτή (2)
Κατά:
Χρειάζονται πολλές εντολές για ένα πρόγραμμα
Κάθε φορά πήγαινε-φέρε από τη μνήμη
(? Κακό είναι αυτό)
Bottleneck o Accum!
Υπέρ:
Εύκολοι compilers, κατανοητός προγραμματισμός,
εύκολη σχεδίαση h/w
Λύση; Πρόσθεση καταχωρητών για συγκεκριμένες λειτουργίες
(ISAs καταχωρητών ειδικού σκοπού)
30/10/2015
cslab@ntua 2010-2011
5
Αρχιτεκτονικές Επεκταμένου Συσσωρευτή
Καταχωρητές ειδικού σκοπού π.χ. δεικτοδότηση, αριθμητικές
πράξεις
Υπάρχουν εντολές που τα ορίσματα είναι όλα σε καταχωρητές
Κατά βάση (π.χ. σε αριθμητικές εντολές) το ένα όρισμα στη
μνήμη.
30/10/2015
cslab@ntua 2010-2011
6
Αρχιτεκτονικές Καταχωρητών γενικού σκοπού
Register-memory
Register-register (load
store) (1980+)
Αφήνουν το ένα όρισμα
να είναι στη μνήμη (πχ.
80386)
A=B+C
Load R1, B
Add R1, C
Store A,
R1
Load R1, B
Load R2, C
Add R3, R1, R2
Store A, R3
extended-accumulator
Memory-memory
30/10/2015
accumulator
register-register
register-memory
cslab@ntua 2010-2011
7
Αρχιτεκτονική Στοίβας
Καθόλου registers! Stack model ~ 1960!!!
Στοίβα που μεταφέρονται τα ορίσματα που αρχικά βρίσκονται
στη μνήμη. Καθώς βγαίνουν γίνονται οι πράξεις και το
αποτέλεσμα ξαναμπαίνει στη στοίβα.
Θυμάστε τα HP calculators με reverse polish notation
A=B+C
push Address C
push AddressB
add
pop AddressA
30/10/2015
cslab@ntua 2010-2011
8
Εντολές μεταβλητού μήκους:
1-17 bytes 80x86
1-54 bytes VAX, IBM
Γιατί??
Ιnstruction Memory ακριβή, οικονομία χώρου!!!!
Εμείς στο μάθημα: register-register ISA! (load- store)
1.
2.
3.
4.
Οι καταχωρητές είναι γρηγορότεροι από τη μνήμη
Μειώνεται η κίνηση με μνήμη
Δυνατότητα να υποστηριχθεί σταθερό μήκος εντολών
(τα ορίσματα είναι καταχωρητές, άρα ό αριθμός τους (πχ.
1-32 καταχωρητές) όχι δ/νσεις μνήμης
Compilers πιο δύσκολοι!!!
30/10/2015
cslab@ntua 2010-2011
9
Βασικές Αρχές Σχεδίασης (patterson-hennessy COD2e)
1. Η ομοιομορφία των λειτουργιών συμβάλλει στην απλότητα
του υλικού (Simplicity favors Regularity)
2. Όσο μικρότερο τόσο ταχύτερο! (smaller is faster)
3. H καλή σχεδίαση απαιτεί σημαντικούς συμβιβασμούς (Good
design demands good compromises)
Γενικότητες? Θα τα δούμε στη συνέχεια......
30/10/2015
cslab@ntua 2010-2011
10
MIPS σύνολο εντολών:
Λέξεις των 32 bit (μνήμη οργανωμένη σε bytes, ακολουθεί το
μοντέλο big Εndian)
32 καταχωρητές γενικού σκοπού - REGISTER FILE
Θα μιλήσουμε για: εντολές αποθήκευσης στη μνήμη (lw, sw)
Αριθμητικές εντολές (add, sub κλπ)
Εντολές διακλάδωσης (branch instructions)
Δεν αφήνουμε τις εντολές να έχουν μεταβλητό πλήθος
ορισμάτων- π.χ. add a,b,c πάντα: a=b+c
Θυμηθείτε την 1η αρχή: H ομοιομορφία των λειτουργιών
συμβάλλει στην απλότητα του h/w
30/10/2015
cslab@ntua 2010-2011
11
Σύνολο Εντολών
Instruction Set
• Λέξεις της γλώσσας του υπολογιστή – εντολές
• Γλώσσες των υπολογιστών – όμοιες
• Στο μάθημα, σύνολο εντολών MIPS
30/10/2015
cslab@ntua 2010-2011
12
Αφού οι καταχωρητές είναι τόσο....«γρήγοροι» γιατί να μην
μεγαλώσουμε το μέγεθος του register file?
2η αρχή: Όσο μικρότερο τόσο ταχύτερο!
Αν το register file πολύ μεγάλο, πιο πολύπλοκη η
αποκωδικοποίηση, πιο μεγάλος ο κύκλος ρολογιού (φάση ID)
άρα.....υπάρχει tradeoff
Μνήμη οργανωμένη σε bytes:
(Κάθε byte και ξεχωριστή
δνση)
230 λέξεις μνήμης των 32 bit (4
bytes) κάθε μια
Memory [0]
32 bits
Memory [4]
32 bits
Memory [8]
32 bits
Memory [12]
32 bits
$s0, $s1,… καταχωρητές (μεταβλητές συνήθως)
$t0, $t1,… καταχωρητές (προσωρινές τιμές)
$zero ειδικός καταχωρητής περιέχει το 0
30/10/2015
cslab@ntua 2010-2011
13
Big Endian vs Little Endian
Big Endian: H δνση του πιο σημαντικού byte (MSB) είναι και δνση
της λέξης
Little Endian: H δνση του λιγότερο σημαντικού byte (LSB) είναι και
δνση της λέξης
H λέξη αποθηκεύεται πάντα σε συνεχόμενες θέσεις: δνση, δνση+1, ..,
δνση+3
LITTLE_ENDIAN
BIG_ENDIAN
A[0]
A[1]
A[2]
30/10/2015
0
1
2
3
4
5
6
7
8
9
10
11
MSB
8 bits
LSB
MSB
A[0]
A[1]
LSB
MSB
A[2]
LSB
cslab@ntua 2010-2011
0
1
2
3
4
5
6
7
8
9
10
11
LSB
MSB
LSB
MSB
LSB
MSB
14
MIPS ISA (βασικές εντολές)
Αριθμητικές εντολές add, sub: πάντα τρία ορίσματα - ποτέ
δνση μνήμης!
add $s1, $s2, $s3
#
$s1 = $s2+$s3
sub $s1, $s2, $s3
#
$s1 = $s2-$s3
Εντολές μεταφοράς δεδομένων (load-store):
Εδώ έχουμε αναφορά στη μνήμη (πόσοι τρόποι? Θα το
δούμε στη συνέχεια)
lw $s1, 100($s2) # $s1 = Memory(100+$s2) (load word)
sw $s1, 100($s2) # Memory(100+$s2) = $s1(store word)
30/10/2015
cslab@ntua 2010-2011
15
Λειτουργίες υλικού υπολογιστών
Αριθμητικές Πράξεις
• add a, b, c #
a← b + c
• a = b + c + d + e;
– add a, b, c
– add a, a, d
– add a, a, e
3 εντολές για το άθροισμα 4 μεταβλητών
30/10/2015
cslab@ntua 2010-2011
16
Λειτουργίες υλικού υπολογιστών
Παράδειγμα:
Κώδικας σε C
a = b + c;
d = a – e;
Μετάφραση σε κώδικα MIPS
add a, b, c
sub d, a, e
30/10/2015
cslab@ntua 2010-2011
17
Λειτουργίες υλικού υπολογιστών
Παράδειγμα:
f = (g + h) – (i + j);
Τι παράγει ο compiler?
add t0, g, h # προσωρινή μεταβλητή t0
add t1, i, j # προσωρινή μεταβλητή t1
sub f, t0, t1 # το f περιέχει το t0 – t1
30/10/2015
cslab@ntua 2010-2011
18
Λειτουργίες υλικού υπολογιστών
Κατηγορία
Εντολή
Παράδειγμα
add
add a, b, c
subtract
sub a, b, c
Σημασία
a=b+c
Πάντα
3 τελεστέοι
a=b-c
Πάντα
3 τελεστέοι
Αριθμητικές
Πράξεις
30/10/2015
cslab@ntua 2010-2011
Σχόλια
19
Τελεστέοι Υλικού Υπολογιστών
f = (g + h) – (i + j);
(f,g,h,i,j) ανατίθενται σε ($s0,$s1,$s2,$s3,$s4)
$t0, $t1 προσωρινοί καταχωρητές
add $t0, $s1, $s2
add $t1, $s3, $s4
sub $s0, $t0, $t1
30/10/2015
cslab@ntua 2010-2011
20
Τελεστέοι Υλικού Υπολογιστών
Γλώσσες προγραμματισμού έχουν:
• απλές μεταβλητές
• σύνθετες δομές (π.χ. arrays, structs)
Πώς τις αναπαριστά ένας υπολογιστής;
Πάντα στη μνήμη
Εντολές μεταφοράς δεδομένων
30/10/2015
cslab@ntua 2010-2011
21
Τελεστέοι Υλικού Υπολογιστών
Εντολές μεταφοράς
δεδομένων
Π.χ. Διεύθυνση του 3
στοιχείου είναι 2
Τιμή στοιχείου Memory[2]
είναι 10
30/10/2015
cslab@ntua 2010-2011
22
Τελεστέοι Υλικού Υπολογιστών
Εντολή μεταφοράς δεδομένων από τη μνήμη
load καταχωρητής, σταθερά(καταχωρητής)
π.χ.
lw $t1, 4($s2)
φορτώνουμε στον $t1 την τιμή M[$s2+4]
30/10/2015
cslab@ntua 2010-2011
23
Τελεστέοι Υλικού Υπολογιστών
Παράδειγμα:
Α πίνακας 100 λέξεων (4 bytes η λέξη)
g, h ανατίθενται σε $s1, $s2
αρχική διεύθυνση του A στον $s3
Μεταγλωττίστε g = h + A[8];
offset
base register
lw $t0, 32($s3)
add $s1, $s2, $t0
30/10/2015
cslab@ntua 2010-2011
24
Τελεστέοι Υλικού Υπολογιστών
• Μνήμη είναι byte
addressable
• Δύο διαδοχικές λέξεις
διαφέρουν κατά 4
• alignment restriction
(ευθυγράμμιση)
– λέξεις ξεκινάνε πάντα σε
διεύθυνση πολ/σιο του 4
30/10/2015
cslab@ntua 2010-2011
25
Τελεστέοι Υλικού Υπολογιστών
Σταθερές:
Πρόσθεση της τιμής 4 στον $s3
lw $t0, AddrConstant4($s1)
add $s3, $s3, $t0
ή
addi $s3, $s3, 4
Make common case FAST
30/10/2015
cslab@ntua 2010-2011
26
Τελεστέοι Υλικού Υπολογιστών
Τελεστέοι MIPS
32 καταχωρητές $s0, $s1, ...
230 θέσεις λέξεων στη μνήμη
232 θέσεις byte στη μνήμη
30/10/2015
cslab@ntua 2010-2011
27
Τελεστέοι Υλικού Υπολογιστών
Συμβολική γλώσσα MIPS
Παραδείγματα
add $s1, $s2, $s3
sub $s1, $s2, $s3
addi $s1, $s2, 100
lw $s1, 100($s2)
sw $s1,100($s2)
30/10/2015
cslab@ntua 2010-2011
28
Τελεστέοι Υλικού Υπολογιστών
Πόσο γρήγορα πληθαίνουν οι καταχωρητές
σε έναν επεξεργαστή;
1. Πολύ γρήγορα (Νόμος Moore, 2ος αριθμός
τρανζίστορ/18 μήνες)
2. Πολύ αργά (portable binary code)
Π.χ. Θέλουμε τα προγράμματά μας να τρέχουν
και στον Pentium III και στον Pentium IV
30/10/2015
cslab@ntua 2010-2011
29
Αναπαράσταση Εντολών στον Υπολογιστή
Δυαδικά ψηφία, Δυαδικό σύστημα (binary)
Υλικό υπολογιστών, υψηλή-χαμηλή τάση, κλπ.
Kαταχωρητές
$s0, ..., $s7 αντιστοιχίζονται στους 16 ως 23
$t0, ..., $t7 αντιστοιχίζονται στους 8 ως 15
30/10/2015
cslab@ntua 2010-2011
30
Αναπαράσταση Εντολών στον Υπολογιστή
Συμβολική αναπαράσταση:
Assembly
add $t0, $s1, $s2
Πώς την καταλαβαίνει ο MIPS?
$s1
0
17
$s2
18
$t0
8
add
unused
0
32
Κώδικας μηχανής
000000
10001
10010
01000
00000
10000
6 bit
5 bit
5 bit
5 bit
5 bit
6 bit
30/10/2015
cslab@ntua 2010-2011
31
Μορφή Εντολής - Instruction Format
Θυμηθείτε το 1ο κανόνα: Η ομοιομορφία των λειτουργιών
συμβάλλει στην απλότητα του υλικού
R-Type
(register type)
op
rs
rt
rd
shamt
funct
6 bits
5bits
5bits
5bits
5bits
6bits
Op: opcode
rs,rt: register source operands
Rd: register destination operand
Shamt: shift amount
Funct: op specific (function code)
add $rd, $rs, $rt
30/10/2015
cslab@ntua 2010-2011
32
MIPS R-Type (ALU)
R-Type: Όλες οι εντολές της ALU που χρησιμοποιούν 3 καταχωρητές
OP
6 bits
rs
5 bits
rt
rd
shamt
funct
5 bits
5 bits
5 bits
6 bits
• Παραδείγματα :
– add $1,$2,$3
and $1,$2,$3
– sub $1,$2,$3
or $1,$2,$3
Destination register in rd
Operand register in rt
Operand register in rs
30/10/2015
cslab@ntua 2010-2011
33
Αναπαράσταση Εντολών στον Υπολογιστή
op
rs
rt
rd
shamt
funct
6 bit
5 bit
5 bit
5 bit
5 bit
6 bit
Τι γίνεται με τη load?
Πώς χωράνε οι τελεστές της στα παραπάνω
πεδία? Π.χ. η σταθερά της lw.
lw $t1, 8000($s3)
σε ποιο πεδίο χωράει;
30/10/2015
cslab@ntua 2010-2011
34
Ερώτηση: Μας αρκεί το R-Type?
Τι γίνεται με εντολές που θέλουν ορίσματα διευθύνσεις
ή σταθερές? Θυμηθείτε, θέλουμε σταθερό μέγεθος κάθε
εντολής (32 bit)
Απάντηση: Μάλλον όχι
Άρα: H καλή σχεδίαση απαιτεί σημαντικούς συμβιβασμούς (3η αρχή)
Ι-Type:
op
rs
rt
address_offset
6 bits
5 bits
5 bits
16 bits
lw $rt, address_offset($rs)
Τα 3 πρώτα πεδία (op,rs, rt) έχουν το ίδιο όνομα και μέγεθος
όπως και πριν
30/10/2015
cslab@ntua 2010-2011
35
Αναπαράσταση Εντολών στον Υπολογιστή
Παράδειγμα:
Kαταχωρητές (σκονάκι )
$s0, ..., $s7 αντιστοιχίζονται στους 16
ως 23
$t0, ..., $t7 αντιστοιχίζονται στους 8 ως
15
lw $t0, 32($s3)
I-format
op
rs
rt
σταθερά ή διεύθυνση
6 bit
5 bit
5 bit
16 bit
xxxxxx
19
8
32
30/10/2015
cslab@ntua 2010-2011
36
Επεξεργαστής
Αριθμός καταχωρητών
γενικού σκοπού
Αρχιτεκτονική
Έτος
EDSAC
1
accumulator
1949
IBM 701
1
accumulator
1953
CDC 6600
8
load-store
1963
IBM 360
16
register-memory
1964
DEC PDP-8
1
accumulator
1965
DEC PDP-11
8
Register-memory
1970
Intel 8008
1
accumulator
1972
Motorola 6800
2
accumulator
1974
DEC VAX
16
register-memory, memory-memory
1977
Intel 8086
8
extended accumulator
1978
Motorola 68000
16
register-memory
1980
Intel 80386
8
register-memory
1985
MIPS
32
load-store
1985
HP PA-RISC
32
load-store
1986
SPARC
32
load-store
1987
PowerPC
32
load-store
1992
DEC Alpha
32
load-store
1992
30/10/2015
cslab@ntua 2010-2011
37
Κανόνες Ονοματοδοσίας και Χρήση των MIPS Registers
• Εκτός από το συνήθη συμβολισμό των καταχωρητών με $ ακολουθούμενο
από τον αριθμό του καταχωρητή, μπορούν επίσης να παρασταθούν και ως
εξής :
Αρ. Καταχωρητή
30/10/2015
Όνομα
0
1
2-3
$zero
$at
$v0-$v1
4-7
8-15
16-23
24-25
26-27
28
29
30
31
$a0-$a3
$t0-$t7
$s0-$s7
$t8-$t9
$k0-$k1
$gp
$sp
$fp
$ra
Χρήση
Preserved on call?
Constant value 0
Reserved for assembler
Values for result and
expression evaluation
Arguments
Temporaries
Saved
More temporaries
Reserved for operating system
Global pointer
Stack pointer
Frame pointer
Return address
cslab@ntua 2010-2011
n.a.
όχι
όχι
ναι
όχι
ναι
όχι
ναι
ναι
ναι
ναι
ναι
38
MIPS I-Type : Load/Store
OP
rs
rt
6 bits
5 bits
5 bits
address
16 bits
– address: 16-bit memory address offset in bytes added to base
register.
• Παραδείγματα :
base register in rs
Offset
– Store word:
sw
– Load word:
lw $1, 30($2)
Destination register in rt
30/10/2015
source register in rt
500($4), $3
base register in rs
Offset
cslab@ntua 2010-2011
39
MIPS ALU I-Type
Οι I-Type εντολές της ALU χρησιμοποιούν 2 καταχωρητές και μία σταθερή τιμή
I-Type είναι και οι εντολές Loads/stores, conditional branches.
OP
rs
rt
6 bits
5 bits
5 bits
immediate
16 bits
– immediate: Constant second operand for ALU instruction.
• Παραδείγματα :
– add immediate:
addi $1,$2,100
– and immediate
andi $1,$2,10
Constant operand
Result register in rt
in immediate
Source operand register in rs
30/10/2015
cslab@ntua 2010-2011
40
MIPS data transfer instructions
Παραδείγματα
Instruction
sw 500($4), $3
sh 502($2), $3
sb 41($3), $2
Σχόλια
Store word
Store half
Store byte
lw $1, 30($2)
lh $1, 40($3)
lhu $1, 40($3)
lb $1, 40($3)
lbu $1, 40($3)
Load word
Load halfword
Load halfword unsigned
Load byte
Load byte unsigned
lui $1, 40
Load Upper Immediate (16 bits shifted left by 16)
LUI
R5
30/10/2015
cslab@ntua 2010-2011
R5
0000 … 0000
41
How about larger constants?
• We'd like to be able to load a 32 bit constant into a register
• Must use two instructions; new "load upper immediate" instruction
lui $t0, 1010101010101010
filled with zeros
1010101010101010

0000000000000000
Then must get the lower order bits right, i.e.,
ori $t0, $t0, 1010101010101010
1010101010101010
0000000000000000
0000000000000000
1010101010101010
1010101010101010
1010101010101010
ori
30/10/2015
cslab@ntua 2010-2011
42
Topics to Review
• Memory layout
• Text, data (static and heap), and the stack
• Procedure conventions
• Procedure call bookkeeping
• Caller Saved Registers:
•
•
•
•
Return address
Arguments
Return value
$t Registers
$ra
$a0, $a1, $a2, $a3
$v0, $v1
$t0 - $t9
• Callee Saved Registers:
• $s Registers
• Procedure structure
• Prologue:
• Body:
• Epilogue:
30/10/2015
$s0 - $s7
allocate frame, save registers, assign locals
procedure code
restore registers, free frame
cslab@ntua 2010-2011
43
Overview
• Data types
– Application / HLL requirements
– Hardware support (data and instructions)
• MIPS data types
• Support for bytes and strings
• Addressing Modes
– Data
– Instructions
• Large constants and far target addresses
• SPIM code
30/10/2015
cslab@ntua 2010-2011
44
Data Types
• Applications / HLL
• Hardware support
– Integer
– Floating point
Character
– String
– Date
– Currency
– Text,
– Objects (ADT)
– Blob
– double precision
– Signed, unsigned
30/10/2015
– Numeric data types
– Integers
– 8 / 16 / 32 / 64 bits
– Signed or unsigned
– Binary coded decimal
(COBOL, Y2K!)
• Floating point
• 32 / 64 /128 bits
– Nonnumeric data types
•
•
•
•
cslab@ntua 2010-2011
Characters
Strings
Boolean (bit maps)
Pointers
45
MIPS Data Types (1/2)
• Basic machine data type: 32-bit word
– 0100 0011 0100 1001 0101 0011 0100 0101
– Integers (signed or unsigned)
• 1,128,878,917
– Floating point numbers
• 201.32421875
– 4 ASCII characters
• CISE
– Memory addresses (pointers)
• 0x43495345
– Instructions
30/10/2015
cslab@ntua 2010-2011
46
MIPS Data Types (2/2)
• 16-bit constants (immediates)
– addi $s0, $s1, 0x8020
– lw $t0, 20($s0)
• Half word (16 bits)
– lh (lhu): load half word
– sh: save half word
lh $t0, 20($s0)
sh $t0, 20($s0)
• Byte (8 bits)
– lb (lbu): load byte
– sb: save byte
30/10/2015
cslab@ntua 2010-2011
sh $t0, 20($s0)
sh $t0, 20($s0)
47
Byte Instructions
lb $s1, 4($s0)
Address
Memory Bytes
0x10000000
$s0: 0x10000000
$s1: 0xFFFFFFAA
lbu $s1, 2($s0)
10101010
$s0: 0x10000000
$s1: 0x000000 AA
30/10/2015
cslab@ntua 2010-2011
48
String Manipulation
Void strcpy (char[], char y[]) {
int i;
i = 0;
while ((x[i]=y[i]) != 0)
i = i + 1;
}
C convention:
Null byte (00000000)
represents end of the string
Importance of comments in MIPS!
30/10/2015
strcpy:
subi $sp, $sp, 4
sw $s0, 0($sp)
add $s0, $zero, $zero
L1: add $t1, $a1, $s0
lb $t2, 0($t1)
add $t3, $a0, $s0
sb $t2, 0($t3)
beq $t2, $zero, L2
addi $s0, $s0, 1
j
L1
L2: lw $s0, 0($sp)
addi $sp, $sp, 4
jr $ra
cslab@ntua 2010-2011
49
Constants
• Small constants are used frequently (50% of
operands)
– e.g.,
A = A + 5;
• Solutions
– Put 'typical constants' in memory and load them.
– Create hard-wired registers (like $zero) for constants like 1.
• MIPS Instructions:
slti $8, $18, 10
andi $29, $29, 6
ori $29, $29, 0x4a
addi $29, $29, 4
30/10/2015
8
29
29
4
101011
10011
01000
0000 0000 0011 0100
cslab@ntua 2010-2011
50
Large Constants
•
To load a 32 bit constant into a register:
1. Load (16) higher order bits
lui $t0, 1010101010101010
1010 1010 1010 1010 0000 0000 0000 0000
2. Then must get the lower order bits right, i.e.,
ori $t0, $t0, 1010101010101010
$t0:
ori
1010 1010 1010 1010 0000 0000 0000 0000
0000 0000 0000 0000 1010 1010 1010 1010
1010 1010 1010 1010 1010 1010 1010 1010
30/10/2015
cslab@ntua 2010-2011
51
Addressing Modes
• Addresses for data and instructions
• Data (operands and results)
– Registers
– Memory locations
– Constants
• Efficient encoding of addresses (space: 32 bits)
–
–
–
–
Registers (32) => 5 bits to encode address
Destructive instructions: reg2 = reg2 + reg1
Accumulator
Stack
• Orthogonality of opcodes and addressing modes
30/10/2015
cslab@ntua 2010-2011
52
Data Addressing Modes
• Register addressing
– The most common (fastest and shortest)
– add $3, $2, $1
• Base addressing
– Operand is at a memory location with offset
– lw $t0, 20 ($t1)
• Immediate addressing
– Operand is a small constant within the instruction
– addi $t0, $t1, 4
(signed 16-bit integer)
30/10/2015
cslab@ntua 2010-2011
53
Addressing
1. Immediate addressing
op
rs
rt
Immediate
2. Register addressing
op
rs
rt
rd
...
funct
Registers
Register
3. Base addressing
op
rs
rt
Memory
Address
+
Register
4. PC-relative addressing
op
rs
rt
Byte
Halfword
Word
Address
Memory
*4
PC
+
Word
5. Pseudodirect addressing
op
Address
*4
Memory
Word
PC
30/10/2015
cslab@ntua 2010-2011
54
Instruction Addressing Modes
• Addresses are 32 bits long
• Special purpose register PC (program counter)
stores the address of the current instruction
• PC-relative addressing (branches)
– Address: PC + (constant in the instruction) * 4
– beq $t0, $t1, 20 (0x15090005)
• Pseudodirect addressing (jumps)
– Address: PC[31:28] : (constant in the instruction) * 4
30/10/2015
cslab@ntua 2010-2011
55
SPIM Code
MIPS
PC
machine code
Pseudo MIPS
main
[0x00400020]
add $9, $10, $11
(0x014b4820)
[0x00400024]
j
(0x08100012)
[0x00400028]
addi $9, $10, -50
[0x0040002c]
lw
[0x00400030]
lw
[0x00400034]
bne $8, $9, 20 [exit-PC]
(0x15090005)
bne $t0, $t1, exit
[0x00400038]
addi $9, $10, 50
(0x21490032)
addi $t1, $t2, 50
[0x0040003c]
bne $8, $9, -28 [main-PC]
(0x1509fff9)
bne $t0, $t1, main
[0x00400040]
lb
$8, -5($9)
(0x8128fffb)
lb
$t0, -5($t1)
[0x00400044]
j
0x00400020 [main]
j
main
[0x00400048]
exit
add $9, $10, $11
30/10/2015
0x00400048 [exit]
main: add $t1, $t2, $t3
j
exit
(0x2149ffce)
addi $t1, $t2, -50
$8, 5($9)
(0x8d280005)
lw $t0, 5($t1)
$8, -5($9)
(0x8d28fffb)
lw $t0, -5($t1)
(0x08100008)
(0x014b4820)
cslab@ntua 2010-2011
exit: add $t1, $t2, $t3
56
Far Target Address
Text Segment (252MB)
0x00400000
(0x07fe0000)
-217
PC (0x08000000)
beq $s0, $s1, L1
+217
(0x08020000)
bne $s0, $s1, L2
j
L1
(0x08200000) L1:
L2:
0x10000000
30/10/2015
cslab@ntua 2010-2011
57
Overview
•
•
•
•
•
•
30/10/2015
Pointers (addresses) and values
Argument passing
Storage lifetime and scope
Pointer arithmetic
Pointers and arrays
Pointers in MIPS
cslab@ntua 2010-2011
58
Pointers
• Pointer: a variable that contains the address of
another variable
– HLL version of machine language memory address
• Why use Pointers?
– Sometimes only way to express computation
– Often more compact and efficient code
• Why not?
– Huge source of bugs in real software, perhaps the
largest single source
1) Dangling reference (premature free)
2) Memory leaks (tardy free): can't have long-running
jobs without periodic restart of them
30/10/2015
cslab@ntua 2010-2011
59
C Pointer Operators
• Suppose c has value 100, it is located in memory at
address 0x10000000
• Unary operator & gives address:
p = &c; gives address of c to p;
– p “points to” c
(p == 0x10000000)
• Unary operator * gives value that pointer points to
– if p = &c => * p == 100 (Dereferencing a pointer)
• Deferencing  data transfer in assembler
– ... = ... *p ...;
 load
(get value from location pointed to by p)
– *p = ...;
 store
(put value into location pointed to by p)
30/10/2015
cslab@ntua 2010-2011
60
Pointer Arithmetic
int x = 1, y = 2;
int z[10];
int *p;
/* x and y are integer variables */
/* an array of 10 ints, z points to start */
/* p is a pointer to an int */
x = 21;
z[0] = 2; z[1] = 3
p = &z[0];
p = z;
p = p+1;
p++;
*p = 4;
p = 3;
p = &x;
z = &y
/* assigns x the new value 21 */
/* assigns 2 to the first, 3 to the next */
/* p refers to the first element of z */
/* same thing; p[ i ] == z[ i ]*/
/* now it points to the next element, z[1] */
/* now it points to the one after that, z[2] */
/* assigns 4 to there, z[2] == 4*/
/* bad idea! Absolute address!!! */
/* p points to x, *p == 21 */
illegal!!!!! array name is not a variable
30/10/2015
cslab@ntua 2010-2011
p:
z[1]
z[0]
4
3
2
y:
2
x:
21
61
Assembly Code
c is int, has value 100, in memory at address
0x10000000, p in $a0, x in $s0
1. p = &c;
/* p gets 0x10000000*/
lui $a0,0x1000 # p = 0x10000000
2. x = *p;
lw
/* x gets 100 */
$s0, 0($a0) # dereferencing p
3. *p = 200; /* c gets 200 */
addi $t0,$0,200
sw
$t0, 0($a0) # dereferencing p
30/10/2015
cslab@ntua 2010-2011
62
Example
int strlen(char *s) {
char *p = s;
/* p points to chars */
while (*p != ’\0’)
p++;
/* points to next char */
return p - s;
/* end - start */
}
mov $t0,$a0
lbu $t1,0($t0) /* derefence p */
beq $t1,$zero, Exit
Loop:addi $t0,$t0,1 /* p++ */
lbu $t1,0($t0) /* derefence p */
bne $t1,$zero, Loop
Exit:sub $v0,$t0,$a0
jr
$ra
30/10/2015
cslab@ntua 2010-2011
63
Argument Passing Options
• 2 choices
– “Call by Value”: pass a copy of the item to the
function/procedure
– “Call by Reference”: pass a pointer to the item
to the function/procedure
• Single word variables passed by value
• Passing an array? e.g., a[100]
– Pascal (call by value) copies 100 words of a[]
onto the stack
– C (call by reference) passes a pointer
(1 word) to the array a[] in a register
30/10/2015
cslab@ntua 2010-2011
64
Lifetime of Storage and Scope
• Automatic (stack allocated)
– Typical local variables of a function
– Created upon call, released upon return
– Scope is the function
• Heap allocated
Code
Static
Heap
– Created upon malloc, released upon free
– Referenced via pointers
• External / static
– Exist for entire program
30/10/2015
cslab@ntua 2010-2011
Stack
65
Arrays, Pointers, and Functions
•
4 versions of array function that adds two
arrays and puts sum in a third array (sumarray)
1. Third array is passed to function
2. Using a local array (on stack) for result and passing
a pointer to it
3. Third array is allocated on heap
4. Third array is declared static
•
30/10/2015
Purpose of example is to show interaction of C
statements, pointers, and memory allocation
cslab@ntua 2010-2011
66
Version 1
int x[100], y[100], z[100];
sumarray(x, y, z);
• C calling convention means:
sumarray(&x[0], &y[0], &z[0]);
• Really passing pointers to arrays
addi
addi
addi
jal
30/10/2015
$a0,$gp,0
# x[0] starts at $gp
$a1,$gp,400 # y[0] above x[100]
$a2,$gp,800 # z[0] above y[100]
sumarray
cslab@ntua 2010-2011
67
Version1: Compiled Code
void sumarray(int a[], int b[], int c[]) {
int i;
for(i = 0; i < 100; i = i + 1)
c[i] = a[i] + b[i];
}
Loop:
Exit:
30/10/2015
addi
beq
lw
lw
add
sw
addi
addi
addi
j
jr
$t0,$a0,400
$a0,$t0,Exit
$t1, 0($a0)
$t2, 0($a1)
$t1,$t1,$t2
$t1, 0($a2)
$a0,$a0,4
$a1,$a1,4
$a2,$a2,4
Loop
$ra
# beyond end of a[]
# $t1=a[i]
# $t2=b[i]
# $t1=a[i] + b[i]
# c[i]=a[i] + b[i]
# $a0++
# $a1++
# $a2++
cslab@ntua 2010-2011
68
Version 2
int *sumarray(int a[],int b[]) {
int i, c[100];
for(i=0;i<100;i=i+1)
c[i] = a[i] + b[i];
return c;
}
$sp
c[100]
a[100]
B[100]
30/10/2015
addi
addi
addi
addi
Loop: beq
lw
lw
add
sw
addi
addi
addi
j
Exit: addi
jr
cslab@ntua 2010-2011
$t0,$a0,400 # beyond end of a[]
$sp,$sp,-400 # space for c
$t3,$sp,0
# ptr for c
$v0,$t3,0
# $v0 = &c[0]
$a0,$t0,Exit
$t1, 0($a0) # $t1=a[i]
$t2, 0($a1) # $t2=b[i]
$t1,$t1,$t2 # $t1=a[i] + b[i]
$t1, 0($t3) # c[i]=a[i] + b[i]
$a0,$a0,4 # $a0++
$a1,$a1,4 # $a1++
$t3,$t3,4
# $t3++
Loop
$sp,$sp, 400 # pop stack
$ra
69
Version 3
int * sumarray(int a[],int b[]) {
int i;
int *c;
c = (int *) malloc(100);
for(i=0;i<100;i=i+1)
c[i] = a[i] + b[i];
return c;
}
Code
Static
c[100]
Heap
• Not reused unless freed
– Can lead to memory leaks
– Java, Scheme have garbage
collectors to reclaim free space
30/10/2015
cslab@ntua 2010-2011
Stack
70
Version 3: Compiled Code
addi $t0,$a0,400 # beyond end of a[]
addi $sp,$sp,-12 # space for regs
sw
$ra, 0($sp) # save $ra
sw
$a0, 4($sp) # save 1st arg.
sw
$a1, 8($sp) # save 2nd arg.
addi $a0,$zero,400
jal
malloc
addi $t3,$v0,0
# ptr for c
lw
$a0, 4($sp) # restore 1st arg.
lw
$a1, 8($sp) # restore 2nd arg.
Loop:
beq $a0,$t0,Exit
... (loop as before on prior slide )
j
Loop
Exit:lw
$ra, 0($sp) # restore $ra
addi $sp, $sp, 12 # pop stack
jr
$ra
30/10/2015
cslab@ntua 2010-2011
71
Version 4
int * sumarray(int a[],int b[]) {
int i;
static int c[100];
for(i=0;i<100;i=i+1)
c[i] = a[i] + b[i];
return c;
}
Code
Static
c[100]
Heap
• Compiler allocates once for
function, space is reused
– Will be changed next time
sumarray invoked
– Why describe? used in C libraries
30/10/2015
cslab@ntua 2010-2011
Stack
72
Review
MIPS operands
Name
Example
Comments
$s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform
32 registers $a0-$a3, $v0-$v1, $gp, arithmetic. MIPS register $zero alw ays equals 0. Register $at is
$fp, $sp, $ra, $at
reserved for the assembler to handle large constants.
Memory[0],
Accessed only by data transfer instructions. MIPS uses byte addresses, so
230 memory Memory[4], ...,
words
30/10/2015
Memory[4294967292]
sequential w ords differ by 4. Memory holds data structures, such as arrays,
and spilled registers, such as those saved on procedure calls.
cslab@ntua 2010-2011
73
Review
add
MIPS assembly language
Example
Meaning
add $s1, $s2, $s3
$s1 = $s2 + $s3
Three operands; data in registers
subtract
sub $s1, $s2, $s3
$s1 = $s2 - $s3
Three operands; data in registers
$s1 = $s2 + 100
$s1 = Memory[$s2 + 100]
Memory[$s2 + 100] = $s1
$s1 = Memory[$s2 + 100]
Memory[$s2 + 100] = $s1
Used to add constants
Category
Arithmetic
Instruction
addi $s1, $s2, 100
lw $s1, 100($s2)
load word
sw $s1, 100($s2)
store word
lb $s1, 100($s2)
load byte
sb $s1, 100($s2)
store byte
load upper immediate lui $s1, 100
add immediate
Data transfer
Conditional
branch
Unconditional jump
30/10/2015
$s1 = 100 * 2
16
Comments
Word from memory to register
Word from register to memory
Byte from memory to register
Byte from register to memory
Loads constant in upper 16 bits
branch on equal
beq
$s1, $s2, 25
if ($s1 == $s2) go to
PC + 4 + 100
Equal test; PC-relative branch
branch on not equal
bne
$s1, $s2, 25
if ($s1 != $s2) go to
PC + 4 + 100
Not equal test; PC-relative
set on less than
slt
$s1, $s2, $s3
if ($s2 < $s3) $s1 = 1;
else $s1 = 0
Compare less than; for beq, bne
set less than
immediate
slti
jump
j
jr
jal
jump register
jump and link
$s1, $s2, 100 if ($s2 < 100) $s1 = 1;
Compare less than constant
else $s1 = 0
2500
$ra
2500
Jump to target address
go to 10000
$ra
For switch, procedure return
go to
$ra = PC + 4; go to 10000 For procedure call
cslab@ntua 2010-2011
74
Conclusions
• Data can be anything
– Datatyping restricts data representations
– Applications restrict datatyping
• MIPS Datatypes: Number, String, Boolean
• Addressing: Pointers, Values
– Many addressing modes (direct, indirect,…)
– Memory-based address storage (jr
instruction)
• Arrays: big chunks of memory
– Pointers versus stack storage
– Be careful of memory leaks!
30/10/2015
cslab@ntua 2010-2011
75
Αναπαράσταση Εντολών στον Υπολογιστή
εντολή
μορφή
add
R
0
reg
reg
reg
0
32ten
δ.ε.
sub
R
0
reg
reg
reg
0
34ten
δ.ε.
addi I
8ten
reg
reg
δ.ε.
δ.ε.
δ.ε.
σταθ.
lw
I
35ten
reg
reg
δ.ε.
δ.ε.
δ.ε.
διευθ.
sw
I
43ten
reg
reg
δ.ε.
δ.ε.
δ.ε.
διευθ.
30/10/2015
op
rs
rt
rd
cslab@ntua 2010-2011
shamt
funct
address
76
Αναπαράσταση Εντολών στον Υπολογιστή
Παράδειγμα:
$t1 περιέχει base address πίνακα Α (32 bit/στοιχείο Α[i])
$s2 αντιστοιχίζεται στη μεταβλητή h
Μεταγλωττίστε το A[300] = h + A[300];
lw $t0, 1200($t1)
add $t0, $s2, $t0
sw $t0, 1200($t1)
30/10/2015
cslab@ntua 2010-2011
77
Αναπαράσταση Εντολών στον Υπολογιστή
Παράδειγμα (συνέχεια):
lw $t0, 1200($t1)
add $t0, $s2, $t0
sw $t0, 1200($t1)
Κώδικας Μηχανής?
op
35
0
43
30/10/2015
rs
9
18
9
rt
8
8
8
rd
8
cslab@ntua 2010-2011
shamt
1200
0
1200
funct
32
78
Αναπαράσταση Εντολών στον Υπολογιστή
Παράδειγμα (συνέχεια):
op
35
0
43
rs
9
18
9
rt
8
8
8
op
100011
000000
101011
rs
01001
10010
01001
rt
01000
01000
01000
30/10/2015
rd
8
shamt
1200
0
1200
funct
32
rd
shamt
funct
0000 0100 1011 0000
8
0
32
0000 0100 1011 0000
cslab@ntua 2010-2011
79
Αναπαράσταση Εντολών στον Υπολογιστή
Έννοια αποθηκευμένου
προγράμματος
Ο υπολογιστής κάνει πολλές
εργασίες φορτώνοντας
δεδομένα στο μνήμη.
Δεδομένα και εντολές είναι
στοιχεία στη μνήμη.
Π.χ. compilers μεταφράζουν
στοιχεία σε κάποια άλλα
στοιχεία.
30/10/2015
cslab@ntua 2010-2011
80
Λογικές Λειτουργίες (Πράξεις)
Λογικές Λειτουργίες
Τελεστές C
Εντολές MIPS
<<
Sll (shift left
logical)
Shift right
>>
Srl (shift right
logical)
AND
&
and, andi
OR
|
or, ori
NOT
~
nor
Shift left
30/10/2015
cslab@ntua 2010-2011
81
Λογικές Λειτουργίες (Πράξεις)
SHIFT
$s0: 0000 0000 0000 0000 0000 0000 0000 1001 = 9ten 
sll $t2, $s0, 4
Κάνουμε shift αριστερά το περιεχόμενο του $s0 κατά 4
θέσεις
0000 0000 0000 0000 0000 0000 1001 0000 = 144ten
και τοποθετούμε το αποτέλεσμα στον $t2.
!!Το περιεχόμενο του $s0 μένει αμετάβλητο!!
30/10/2015
cslab@ntua 2010-2011
82
Λογικές Λειτουργίες (Πράξεις)
Kαταχωρητές (σκονάκι )
$s0, ..., $s7 αντιστοιχίζονται στους 16
ως 23
$t0, ..., $t7 αντιστοιχίζονται στους 8 ως
15
SHIFT
sll $t2, $s0, 4
6 bit
5 bit
5 bit
5 bit
5 bit
6 bit
op
0
000000
rs
0
00000
rt
16
10000
rd
10
01010
shamt
4
00100
funct
0
000000
sll: opcode=0, funct=0
30/10/2015
cslab@ntua 2010-2011
83
Λογικές Λειτουργίες (Πράξεις)
AND, OR
$t2: 0000 0000 0000 0000 0000 1101 0000 0000
$t1: 0000 0000 0000 0000 0011 1100 0000 0000
and $t0, $t1, $t2
# Μάσκα
$t0: 0000 0000 0000 0000 0000 1100 0000 0000
or $t0, $t1, $t2
$t0: 0000 0000 0000 0000 0011 1101 0000 0000
30/10/2015
cslab@ntua 2010-2011
84
Λογικές Λειτουργίες (Πράξεις)
NOT, NOR
$t1: 0000 0000 0000 0000 0011 1100 0000 0000
$t3: 0000 0000 0000 0000 0000 0000 0000 0000
δεν υπάρχει γιατί θέλουμε πάντα 2
καταχωρητές source. Άρα χρησιμοποιούμε τη
nor:
A NOR 0 = NOT (A OR 0) = NOT A
not $t0, $t1
nor $t0, $t1, $t3
$t0: 1111 1111 1111 1111 1100 0011 1111 1111
30/10/2015
cslab@ntua 2010-2011
85
MIPS Arithmetic Instructions
Παραδείγματα
Instruction
add
subtract
add immediate
add unsigned
subtract unsigned
add imm. unsign.
multiply
multiply unsigned
divide
Παράδειγμα
add $1,$2,$3
sub $1,$2,$3
addi $1,$2,100
addu $1,$2,$3
subu $1,$2,$3
addiu $1,$2,100
mult $2,$3
multu$2,$3
div $2,$3
divide unsigned
divu $2,$3
Move from Hi
Move from Lo
mfhi $1
mflo $1
30/10/2015
Έννοια
$1 = $2 + $3
$1 = $2 – $3
$1 = $2 + 100
$1 = $2 + $3
$1 = $2 – $3
$1 = $2 + 100
Hi, Lo = $2 x $3
Hi, Lo = $2 x $3
Lo = $2 ÷ $3,
Hi = $2 mod $3
Lo = $2 ÷ $3,
Hi = $2 mod $3
$1 = Hi
$1 = Lo
cslab@ntua 2010-2011
Σχόλια
3 operands; exception possible
3 operands; exception possible
+ constant; exception possible
3 operands; no exceptions
3 operands; no exceptions
+ constant; no exceptions
64-bit signed product
64-bit unsigned product
Lo = quotient, Hi = remainder
Unsigned quotient & remainder
Used to get copy of Hi
Used to get copy of Lo
86
MIPS Logic/Shift Instructions
Παραδείγματα
Instruction
and
or
xor
nor
and immediate
or immediate
xor immediate
shift left logical
shift right logical
shift right arithm.
shift left logical
shift right logical
shift right arithm.
30/10/2015
Παράδειγμα
and $1,$2,$3
or $1,$2,$3
xor $1,$2,$3
nor $1,$2,$3
andi $1,$2,10
ori $1,$2,10
xori $1, $2,10
sll $1,$2,10
srl $1,$2,10
sra $1,$2,10
sllv $1,$2,$3
srlv $1,$2, $3
srav $1,$2, $3
Έννοια
$1 = $2 & $3
$1 = $2 | $3
$1 = $2 $3
$1 = ~($2 |$3)
$1 = $2 & 10
$1 = $2 | 10
$1 = ~$2 &~10
$1 = $2 << 10
$1 = $2 >> 10
$1 = $2 >> 10
$1 = $2 << $3
$1 = $2 >> $3
$1 = $2 >> $3
cslab@ntua 2010-2011
Σχόλια
3 reg. operands; Logical AND
3 reg. operands; Logical OR
3 reg. operands; Logical XOR
3 reg. operands; Logical NOR
Logical AND reg, constant
Logical OR reg, constant
Logical XOR reg, constant
Shift left by constant
Shift right by constant
Shift right (sign extend)
Shift left by variable
Shift right by variable
Shift right arith. by variable
87
Εντολές Λήψης Αποφάσεων
beq, bne
beq reg1, reg2, L1
#branch if equal
Αν οι καταχωρητές reg1 και reg2 είναι ίσοι,
πήγαινε στην ετικέτα L1
bne reg1, reg2, L1
#branch if not equal
Αν οι καταχωρητές reg1 και reg2 δεν είναι ίσοι,
πήγαινε στην ετικέτα L1
30/10/2015
cslab@ntua 2010-2011
88
Εντολές Λήψης Αποφάσεων
Παράδειγμα:
if(i == j) f = g + h; else f = g – h;
με f, g, h, i, j αντιστοιχούνται σε $s0, ..., $s4
version 1
version 2
bne $s3, $s4, Else
beq $s3, $s4, Then
add $s0, $s1, $s2
sub $s0, $s1, $s2
j Exit
j Exit
Else:sub $s0, $s1, $s2 Then: add $s0, $s1, $s2
Exit:
30/10/2015
Exit:
cslab@ntua 2010-2011
89
Εντολές Λήψης Αποφάσεων
Βρόχοι (Loops)
while (save[i] == k) i += 1;
με i = $s3, k = $s5, save base addr = $s6
Loop:
sll
add
lw
bne
addi
j
$t1,
$t1,
$t0,
$t0,
$s3,
Loop
$s3, 2 #πολ/ζω i επί 4
$t1, $s6
0($t1)
$s5, Exit
$s3, 1
Exit:
30/10/2015
cslab@ntua 2010-2011
90
Εντολές Λήψης Αποφάσεων
Συγκρίσεις
slt $t0, $s3, $s4
# set on less than
Ο καταχωρητής $t0 τίθεται με 1 αν η τιμή στον $s3
είναι μικρότερη από την τιμή στο $s4.
Σταθερές ως τελεστέοι είναι δημοφιλείς στις συγκρίσεις
slti $t0, $s2, 10
# set on less than
# immediate
Ο καταχωρητής $t0 τίθεται με 1 αν η τιμή στον $s2
είναι μικρότερη από την τιμή 10.
30/10/2015
cslab@ntua 2010-2011
91
MIPS Branch, Compare, Jump
Παραδείγματα
Instruction
branch on equal
Παράδειγμα
Έννοια
beq $1,$2,100
if ($1 == $2) go to PC+4+100
Equal test; PC relative branch
branch on not eq. bne $1,$2,100
if ($1!= $2) go to PC+4+100
Not equal test; PC relative branch
set on less than
slt $1,$2,$3
if ($2 < $3) $1=1; else $1=0
Compare less than; 2’s comp.
set less than imm. slti $1,$2,100
if ($2 < 100) $1=1; else $1=0
Compare < constant; 2’s comp.
set less than uns. sltu $1,$2,$3
if ($2 < $3) $1=1; else $1=0
Compare less than; natural numbers
set l. t. imm. uns. sltiu $1,$2,100
if ($2 < 100) $1=1; else $1=0
Compare < constant; natural numbers
jump
j 10000
go to 10000
Jump to target address
jump register
jr $31
go to $31
For switch, procedure return
jump and link
jal 10000
$31 = PC + 4; go to 10000
For procedure call
30/10/2015
cslab@ntua 2010-2011
92
Εντολές διακλάδωσης-branching instructions
branch if
equal
branch if
!equal
beq $s3, 4s4, L1 # goto L1 if $s3 equals $s4
bne $s3, 4s4, L1 # goto L1 if $s3 not equals $s4
unconditional
Jump
jr $t1 # goto $t1
..... είναι I –Type εντολές
slt $t0, $s3, $s4 #set $t0 to 1 if $s3 is less
than $s4;else set $t0 to 0
Όμως:
j L1 # goto L1
Πόσο μεγάλο είναι το μήκος του address L1;
Πόσο «μεγάλο» μπορεί να είναι το άλμα;
30/10/2015
cslab@ntua 2010-2011
93
MIPS Branch I-Type
OP
rs
rt
6 bits
5 bits
5 bits
address
16 bits
– address: 16-bit memory address branch target offset in words
added to PC to form branch address.
• Παραδείγματα :
Register in rs
30/10/2015
Final offset is calculated in bytes,
equals to
Register in rt {instruction field address} x 4,
e.g. new PC = PC + 400
• Branch on equal
beq $1,$2,100
• Branch on not equal
bne $1,$2,100
cslab@ntua 2010-2011
94
MIPS J-Type
J-Type: jump j, jump and link jal
OP
jump target
6 bits
26 bits
– jump target: jump memory address in words.
final jump memory address in bytes is calculated
from {jump target} x 4
• Παραδείγματα :
– Branch on equal
j
10000
– Branch on not equal jal
30/10/2015
cslab@ntua 2010-2011
10000
95
Other Jump Instructions
• Jump (J-type):
– j 10000
# jump to address 10000
• Jump Register (NB: R-type!):
– jr rs
# jump to 32 bit address in register rs
• Jump and Link (J-type):
– jal 10000
# jump to 10000 and save PC in R31
– Use jal for procedure calls, it saves the return address (PC+4) in
register 31 ($ra)
– Use “jr $ra” to return from subroutine
– Nested procedures must save $ra on a stack, and should use registers
$sp (stack pointer) and $fp (frame pointer) manage the stack.
30/10/2015
cslab@ntua 2010-2011
96
Summary of MIPS instruction formats
• R-type (add, sub, slt, jr)
op
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
• I-type (beq, bne + addi, lui + lw, sw)
op
rs
rt
immediate value / address offset
6 bits
5 bits
5 bits
16 bits
• J-type (j, jal)
op
6 bits
30/10/2015
jump target address
26 bits
cslab@ntua 2010-2011
97
Απ’ ευθείας διευθυνσιοδότηση- Σταθερές
Οι πιο πολλές αριθμητικές εκφράσεις σε προγράμματα,
περιέχουν σταθερές: π.χ. index++
Στον κώδικα του gcc: 52% εκφράσεων έχουν constants
Στον κώδικα του spice: 69% των εκφράσεων!
Τι κάνουμε με τις σταθερές (και αν είναι > 16bit;)
Θέλουμε: $s3=$s3+2
lw $t0, addr_of_constant_2($zero)
add $s3,$s3,$t0
Αλλιώς: addi $s3,$s3,2 (add immediate)
Όμοια: slti $t0,$s2, 10 # $t0=1 if $s2<10
30/10/2015
cslab@ntua 2010-2011
98
Τρόποι Διευθυνσιοδότησης στον MIPS:
1. Register Addressing
2. Base or Displacement Addressing
3. Immediate Addressing
4. PC-relative addressing (address is the sum of the PC
and a constant in the instruction)
5. Pseudodirect addressing (the jump address is the 26
bits of the instruction, concatenated with the upper bits
of the PC)
30/10/2015
cslab@ntua 2010-2011
99
addi $rt,$rs,immediate
π.χ. lui $t0, 255
slti $t0, $s1, 10
add $rd,$rs,$rt
π.χ. add $t0,$s1,$s2
I-Type
R-Type
I-Type
π.χ.
lw $rt,address($rs)
lw $t1,100($s2)
I-Type
bne $rs,$rt,address # goto {4 x address (0:15)}
π.χ. bne $s0,$s1,L2
J-Type
j address # goto {4 x address(0:25)}(0:27)-(28:31)(PC)
30/10/2015
cslab@ntua 2010-2011
100
Addressing Modes : Παραδείγματα
Addr. mode
Παράδειγμα
Έννοια
χρήση
Register
add r4,r3
Regs[r4]← Regs[r4]+ Regs[r3]
a value is in register
Immediate
add r4,#3
Regs[r4]← Regs[r4]+3
for constants
Displacement
add r4,100(r1)
Regs[r4]← Regs[r4]+Mem[100+ Regs[r1]]
local variables
Reg. indirect
add r4,(r1)
Regs[r4]← Regs[r4]+Mem[Regs[r1]]
accessing using a pointer
or comp. address
Indexed
add r4,(r1+r2)
Regs[r4]← Regs[r4]+Mem[Regs[r1]+
Regs[r2]]
array addressing (base +offset)
Direct
add r4,(1001)
Regs[r4]← Regs[r4]+Mem[1001]
addr. static data
Mem. Indirect
add r4,@(r3)
Regs[r4]←
Regs[r4]+Mem[Mem[Regs[r3]]]
if R3 keeps the address of
a pointer p, this yields *p
Autoincrement
add r4,(r3)+
Regs[r4]← Regs[r4]+Mem[Regs[r3]]
Regs[r3]← Regs[r3]+d
stepping through arrays
within a loop; d defines
size of an element
Autodecrement
add r4,-(r3)
Regs[r3]← Regs[r3]-d
Regs[r4]← Regs[r4]+Mem[Regs[r3]]
similar as previous
Scaled
add r4,100(r2)[r3]
Regs[r4]← Regs[r4]+
Mem[100+Regs[r2]+Regs[r3]*d]
to index arrays
30/10/2015
cslab@ntua 2010-2011
101