1 CISC 2 RISC Complex Instruction Set Computer

  • Slides: 137
Download presentation

Αρχιτεκτονικές Καταχωρητών Γενικού Σκοπού 1. CISC 2. RISC • Complex Instruction Set Computer •

Αρχιτεκτονικές Καταχωρητών Γενικού Σκοπού 1. CISC 2. RISC • Complex Instruction Set Computer • Reduced Instruction Set Computer • Εντολές για πράξεις Register. Memory ή Memory-Memory • Πράξεις μόνο Register-Register (load store) (1980+) • Αφήνουν το ένα όρισμα να είναι στη μνήμη (πχ. 80386) Load R 1, B Add R 1, C Store A, R 1 A=B+C Load R 1, B Load R 2, C Add R 3, R 1, R 2 Store A, R 3 extended-accumulator Memory-memory cslab@ntua 2019 -2020 accumulator register-register-memory 7

Κανόνες Ονοματοδοσίας και Χρήση των MIPS Registers • Εκτός από το συνήθη συμβολισμό των

Κανόνες Ονοματοδοσίας και Χρήση των MIPS Registers • Εκτός από το συνήθη συμβολισμό των καταχωρητών με $ ακολουθούμενο από τον αριθμό του καταχωρητή, μπορούν επίσης να παρασταθούν και ως εξής : Αρ. Καταχωρητή Όνομα 0 1 2 -3 $zero $at $v 0 -$v 1 4 -7 8 -15 16 -23 24 -25 26 -27 28 29 30 31 $a 0 -$a 3 $t 0 -$t 7 $s 0 -$s 7 $t 8 -$t 9 $k 0 -$k 1 $gp $sp $fp $ra cslab@ntua 2019 -2020 Χρήση 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 26 n. a. όχι ναι ναι ναι

Αναπαράσταση Εντολών (2) Συμβολική αναπαράσταση: Assembly add $t 0, $s 1, $s 2 Πώς

Αναπαράσταση Εντολών (2) Συμβολική αναπαράσταση: Assembly add $t 0, $s 1, $s 2 Πώς την καταλαβαίνει ο MIPS? $s 1 0 17 $s 2 $t 0 18 8 add unused 0 32 Κώδικας μηχανής 000000 10001 10010 01000 00000 10000 6 bit 5 bit 6 bit cslab@ntua 2019 -2020 28

Μορφή Εντολής – Instruction Format Θυμηθείτε την 1η αρχή σχεδίασης: Η ομοιομορφία των λειτουργιών

Μορφή Εντολής – Instruction Format Θυμηθείτε την 1η αρχή σχεδίασης: Η ομοιομορφία των λειτουργιών συμβάλλει στην απλότητα του υλικού R-Type (register type) op rs rt rd shamt funct 6 bits 5 bits 6 bits Op: opcode rs, rt: register source operands Rd: register destination operand Shamt: shift amount Funct: op specific (function code) add $rd, $rs, $rt cslab@ntua 2019 -2020 29

MIPS R-Type (ALU) R-Type: Όλες οι εντολές της ALU που χρησιμοποιούν 3 καταχωρητές OP

MIPS R-Type (ALU) R-Type: Όλες οι εντολές της ALU που χρησιμοποιούν 3 καταχωρητές OP 6 bits rs rt 5 bits rd shamt 5 bits funct 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 cslab@ntua 2019 -2020 30

MIPS I-Type : Load/Store OP rs 6 bits 5 bits address rt 5 bits

MIPS I-Type : Load/Store OP rs 6 bits 5 bits address rt 5 bits 16 bits – address: 16 -bit memory address offset in bytes added to base register. • Παραδείγματα : source register in rt Offset – Store word: sw $3, 500($4) – Load word: lw $1, 30($2) base register in rs Destination register in rt cslab@ntua 2019 -2020 Offset 34

MIPS I-Type : ALU Οι I-Type εντολές της ALU χρησιμοποιούν 2 καταχωρητές και μία

MIPS I-Type : ALU Οι I-Type εντολές της ALU χρησιμοποιούν 2 καταχωρητές και μία σταθερή τιμή I-Type είναι και οι εντολές Loads/stores, conditional branches. OP rs 6 bits 5 bits immediate rt 5 bits 16 bits – immediate: Constant second operand for ALU instruction. • Παραδείγματα : – add immediate: addi $1, $2, 100 – and immediate andi $1, $2, 10 Result register in rt Source operand register in rs cslab@ntua 2019 -2020 35 Constant operand in immediate

MIPS data transfer instructions : Παραδείγματα (1) Instruction sw $3, 500($4) sh $3, 502($2),

MIPS data transfer instructions : Παραδείγματα (1) Instruction sw $3, 500($4) sh $3, 502($2), sb $2, 41($3) Σχόλια Store word Store half Store byte lw $1, 30($2) lh $1, 40($3) lhu $1, 40($3) lbu $1, 40($3) Load word Load halfword unsigned Load byte unsigned lui $1, 40 Load Upper Immediate (16 bits shifted left by 16) LUI R 5 cslab@ntua 2019 -2020 R 5 0000 … 0000 36

Αναπαράσταση Εντολών στον Υπολογιστή εντολή μορφή add R 0 reg reg 0 32 ten

Αναπαράσταση Εντολών στον Υπολογιστή εντολή μορφή add R 0 reg reg 0 32 ten δ. ε. sub R 0 reg reg 0 34 ten δ. ε. addi I 8 ten reg δ. ε. σταθ. lw I 35 ten reg δ. ε. διευθ. sw I 43 ten reg δ. ε. διευθ. cslab@ntua 2019 -2020 op rs rt 38 rd shamt funct address

Αναπαράσταση Εντολών στον Υπολογιστή Παράδειγμα: Μεταγλωττίστε το A[300] = h + A[300] $t 1

Αναπαράσταση Εντολών στον Υπολογιστή Παράδειγμα: Μεταγλωττίστε το A[300] = h + A[300] $t 1 δνση βάσης πίνακα Α (32 bit/στοιχείο Α[i]), $s 2 μεταβλητή h lw $t 0, 1200($t 1) add $t 0, $s 2, $t 0 sw $t 0, 1200($t 1) op rs rt 35 9 8 0 18 8 43 9 8 op rs rt 100011 01000 000000 10010 01000 101011 01000 cslab@ntua 2019 -2020 rd shamt funct 1200 8 0 32 1200 rd shamt funct 0000 0100 1011 0000 8 0 0000 0100 1011 0000 39 32

Λογικές Λειτουργίες (Πράξεις) (1) Λογικές Λειτουργίες Τελεστές C Εντολές MIPS << Sll (shift left

Λογικές Λειτουργίες (Πράξεις) (1) Λογικές Λειτουργίες Τελεστές C Εντολές MIPS << Sll (shift left logical) Shift right >> Srl (shift right logical) AND & and, andi OR | or, ori NOT ~ nor Shift left cslab@ntua 2019 -2020 40

Λογικές Λειτουργίες (Πράξεις) (3) SHIFT Kαταχωρητές (σκονάκι ) $s 0, . . . ,

Λογικές Λειτουργίες (Πράξεις) (3) SHIFT Kαταχωρητές (σκονάκι ) $s 0, . . . , $s 7 αντιστοιχίζονται στους 16 - 23 $t 0, . . . , $t 7 αντιστοιχίζονται στους 8 - 15 sll $t 2, $s 0, 4 6 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 cslab@ntua 2019 -2020 42

Λογικές Λειτουργίες (Πράξεις) (4) AND, OR $t 2: 0000 0000 1101 0000 $t 1:

Λογικές Λειτουργίες (Πράξεις) (4) AND, OR $t 2: 0000 0000 1101 0000 $t 1: 0000 0011 1100 0000 and $t 0, $t 1, $t 2 # Μάσκα $t 0: 0000 0000 1100 0000 or $t 0, $t 1, $t 2 $t 0: 0000 0011 1101 0000 cslab@ntua 2019 -2020 43

Λογικές Λειτουργίες (Πράξεις) (5) NOT, NOR $t 1: 0000 0011 1100 0000 $t 3:

Λογικές Λειτουργίες (Πράξεις) (5) NOT, NOR $t 1: 0000 0011 1100 0000 $t 3: 0000 0000 δεν χρειάζεται γιατί μπορούμε να χρησιμοποιούμε τη nor: A NOR 0 = NOT (A OR 0) = NOT A not $t 0, $t 1 nor $t 0, $t 1, $t 3 $t 0: 1111 1100 0011 1111 cslab@ntua 2019 -2020 44

MIPS Arithmetic Instructions : Παραδείγματα Instruction add subtract add immediate add unsigned subtract unsigned

MIPS Arithmetic Instructions : Παραδείγματα Instruction add subtract add immediate add unsigned subtract unsigned add imm. unsign. 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 divide unsigned divu $2, $3 Move from Hi Move from Lo mfhi $1 mflo $1 cslab@ntua 2019 -2020 Έννοια $1 = $2 + $3 $1 = $2 – $3 $1 = $2 + 100 Hi, Lo = $2 x $3 Lo = $2 ÷ $3, Hi = $2 mod $3 $1 = Hi $1 = Lo 45 Σχόλια 3 operands; exception possible + constant; exception possible 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

MIPS Logic/Shift Instructions : Παραδείγματα Instruction and or xor nor and immediate or immediate

MIPS Logic/Shift Instructions : Παραδείγματα Instruction and or xor nor and immediate or immediate xor immediate shift left logical shift right arithm. cslab@ntua 2019 -2020 Παράδειγμα 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 sra $1, $2, 10 sllv $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 << $3 $1 = $2 >> $3 46 Σχόλια 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 (sign extend) Shift left by variable Shift right arith. by variable

Εντολές Λήψης Αποφάσεων (2) Παράδειγμα: if(i == j) f = g + h; else

Εντολές Λήψης Αποφάσεων (2) Παράδειγμα: if(i == j) f = g + h; else f = g – h; με f, g, h, i, j αντιστοιχούνται σε $s 0, . . . , $s 4 version 1 Else: version 2 bne $s 3, $s 4, Else add $s 0, $s 1, $s 2 j Exit sub $s 0, $s 1, $s 2 beq $s 3, $s 4, Then sub $s 0, $s 1, $s 2 j Exit Then: add $s 0, $s 1, $s 2 Exit: cslab@ntua 2019 -2020 Exit: 48

Εντολές Λήψης Αποφάσεων (3) Βρόχοι (Loops) while (save[i] == k) i += 1; με

Εντολές Λήψης Αποφάσεων (3) Βρόχοι (Loops) while (save[i] == k) i += 1; με i = $s 3, k = $s 5, save base addr = $s 6 Loop: sll add lw bne addi j $t 1, $s 3, 2 #πολ/ζω i επί 4 $t 1, $s 6 $t 0, 0($t 1) $t 0, $s 5, Exit $s 3, 1 Loop Exit: cslab@ntua 2019 -2020 49

MIPS Branch, Compare, Jump : Παραδείγματα Instruction Παράδειγμα branch on equal beq $1, $2,

MIPS Branch, Compare, Jump : Παραδείγματα Instruction Παράδειγμα branch on equal beq $1, $2, 100 branch on not eq. bne $1, $2, 100 set on less than slt $1, $2, $3 set less than imm. slti $1, $2, 100 set less than uns. sltu $1, $2, $3 set l. t. imm. uns. sltiu $1, $2, 100 jump j 10000 jump register jr $31 jump and link jal 10000 cslab@ntua 2019 -2020 Έννοια if ($1 == $2) go to PC+4+10 Equal test; PC relative branch if ($1!= $2) go to PC+4+100 Not equal test; PC relative branch if ($2 < $3) $1=1; else $1=0 Compare less than; 2’s comp. if ($2 < 100) $1=1; else $1=0 Compare < constant; 2’s comp. if ($2 < $3) $1=1; else $1=0 Compare less than; natural numbers if ($2 < 100) $1=1; else $1=0 Compare < constant; natural numbers go to 10000 Jump to target address go to $31 For switch, procedure return $31 = PC + 4; go to 10000 For function/procedure call 52

Εντολές διακλάδωσης – branching instructions branch if equal beq $s 3, $s 4, L

Εντολές διακλάδωσης – branching instructions branch if equal beq $s 3, $s 4, L 1 # goto L 1 if $s 3 equals $s 4 branch if !equal bne $s 3, $s 4, L 1 # goto L 1 if $s 3 not equals $s 4 unconditional Jump . . . είναι I –Type εντολές jr $t 1 # goto $t 1 . . . είναι R –Type εντολή slt $t 0, $s 3, $s 4 #set $t 0 to 1 if $s 3 is less than $s 4; else set $t 0 to 0 Όμως: j L 1 # goto L 1 Πόσο μεγάλο είναι το μήκος του address L 1; Πόσο «μεγάλο» μπορεί να είναι το άλμα; cslab@ntua 2019 -2020 53

MIPS Branch I-Type OP 6 bits rs rt 5 bits address 16 bits –

MIPS Branch I-Type OP 6 bits rs rt 5 bits address 16 bits – address: 16 -bit memory address branch target offset in words added to PC to form branch address. • Παραδείγματα : cslab@ntua 2019 -2020 Register in rs 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 54

MIPS J-Type: jump j, jump and link jal OP jump target 6 bits 26

MIPS 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 • Παραδείγματα : cslab@ntua 2019 -2020 – Jump j 10000 – Jump and Link jal 10000 55

Σύνοψη – MIPS Instruction Formats • R-type (add, sub, slt, shift*, jr, jalr) op

Σύνοψη – MIPS Instruction Formats • R-type (add, sub, slt, shift*, jr, jalr) op 6 bits rs rt rd 5 bits shamt funct 5 bits 6 bits • I-type (beq, bne + addi, lui + lw, sw) op rs rt 6 bits 5 bits immediate value / address offset 16 bits • J-type (j, jal) op jump target address 6 bits 26 bits cslab@ntua 2019 -2020 57

Memory layout of programs (συνέχεια) 1. Text segment (κώδικας προγράμματος) 2. Initialized data segment

Memory layout of programs (συνέχεια) 1. Text segment (κώδικας προγράμματος) 2. Initialized data segment (or. data segment): contains global variables, static variables divided into read only area + read-write area e. g char s[]=“hello world” int debug = 1 const char * string =“hello world”; “hello world” literal stored in ro area, string stored in rw area static int i = 10 global int i = 10 3. Uninitialized data segment (or. bss segment) bss: block started by symbol all global variables and static variables that are initialized to zero or do not have explicit initialization in source code. int j ; static int i; 4. Stack stack pointer. Local variables from a function. 5. Heap pointer. Dynamic memory allocation. Malloc, realloc, free. cslab@ntua 2019 -2020 60

Τύποι Δεδομένων • Applications / HLL – – – – – • Hardware support

Τύποι Δεδομένων • Applications / HLL – – – – – • Hardware support Integer Floating point Character String Date Currency Text, Objects (ADT) Blob double precision Signed, unsigned cslab@ntua 2019 -2020 61 – Numeric data types – Integers – 8 / 16 / 32 / 64 bits – Signed or unsigned – Binary coded decimal (COBOL, Y 2 K!) • Floating point • 32 / 64 /128 bits – Nonnumeric data types • • Characters Strings Boolean (bit maps) Pointers

Τύποι Δεδομένων : MIPS (1) • Βασικός τύπος δεδομένων: 32 -bit word – 0100

Τύποι Δεδομένων : MIPS (1) • Βασικός τύπος δεδομένων: 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 χαρακτήρες (en. wikipedia. org/wiki/ASCII) • CISE – Διευθύνσεις μνήμης (pointers) • 0 x 43495345 – Εντολές (opcode = 010000, …) cslab@ntua 2019 -2020 62

Τύποι Δεδομένων : MIPS (2) • 16 -bit σταθερές (immediates) – addi $s 0,

Τύποι Δεδομένων : MIPS (2) • 16 -bit σταθερές (immediates) – addi $s 0, $s 1, 0 x 8020 – lw $t 0, 20($s 0) • Half word (16 bits) – lh (lhu): load half word – sh: save half word lh $t 0, 20($s 0) sh $t 0, 20($s 0) • Byte (8 bits) – lb (lbu): load byte – sb: save byte cslab@ntua 2019 -2020 lb $t 0, 20($s 0) sb $t 0, 20($s 0) 63

Εντολές λειτουργίας Byte lb $s 1, 4($s 0) $s 0: 0 x 10000000 $s

Εντολές λειτουργίας Byte lb $s 1, 4($s 0) $s 0: 0 x 10000000 $s 1: 0 x. FFFFFFAA Address Memory Bytes 0 x 10000000 lbu $s 1, 4($s 0) 1010 $s 0: 0 x 10000000 $s 1: 0 x 000000 AA lb => Επέκταση προσήμου, lbu => zero-fill cslab@ntua 2019 -2020 64

Παράδειγμα : Αντιγραφή String strcpy: Void strcpy (char x[], char y[]) { subi $sp,

Παράδειγμα : Αντιγραφή String strcpy: Void strcpy (char x[], char y[]) { subi $sp, 4 sw $s 0, 0($sp) int i; add $s 0, $zero # i=0 i = 0; L 1: add $t 1, $a 1, $s 0 while ((x[i]=y[i]) != 0) lb $t 2, 0($t 1) i = i + 1; add $t 3, $a 0, $s 0 sb $t 2, 0($t 3) } beq $t 2, $zero, L 2 C convention: addi $s 0, 1 # i++ Null byte (0000) j L 1 represents end of the string L 2: lw $s 0, 0($sp) addi $sp, 4 Importance of comments in MIPS! jr $ra cslab@ntua 2019 -2020 65

Γιατί MIPS? cslab@ntua 2019 -2020 71

Γιατί MIPS? cslab@ntua 2019 -2020 71

Generic Examples of Instruction Format Widths Variable: … … MIPS Fixed: Hybrid: cslab@ntua 2019

Generic Examples of Instruction Format Widths Variable: … … MIPS Fixed: Hybrid: cslab@ntua 2019 -2020 72

Top 10 80 x 86 Instructions cslab@ntua 2019 -2020 73

Top 10 80 x 86 Instructions cslab@ntua 2019 -2020 73

Operand Size Usage • Support for these data sizes and types: 8 -bit, 16

Operand Size Usage • Support for these data sizes and types: 8 -bit, 16 -bit, 32 -bit integers and 32 -bit and 64 -bit IEEE 754 floating point numbers cslab@ntua 2019 -2020 74

Conditional Branch Distance Bits of Branch Displacement • 25% of integer branches are 2

Conditional Branch Distance Bits of Branch Displacement • 25% of integer branches are 2 to 4 instructions cslab@ntua 2019 -2020 75

Conditional Branch Addressing • PC-relative since most branches are relatively close to the current

Conditional Branch Addressing • PC-relative since most branches are relatively close to the current PC • At least 8 bits suggested ( 128 instructions) • Compare Equal/Not Equal most important for integer programs (86%) cslab@ntua 2019 -2020 76

Παράδειγμα : Απομακρυσμένες Διευθύνσεις • Τι γίνεται αν ένα branch πηγαίνει «πολύ μακρυά» ;

Παράδειγμα : Απομακρυσμένες Διευθύνσεις • Τι γίνεται αν ένα branch πηγαίνει «πολύ μακρυά» ; Text Segment (252 MB) 0 x 00400000 (0 x 07 fe 0000) -217 PC (0 x 08000000) beq $s 0, $s 1, L 1 +217 (0 x 08020000) bne $s 0, $s 1, L 2 j L 1 (0 x 08200000) L 1: L 2: 0 x 10000000 cslab@ntua 2019 -2020 77

C Pointer Operators • Έστω ότι η μεταβλητή c έχει την τιμή 100 και

C Pointer Operators • Έστω ότι η μεταβλητή c έχει την τιμή 100 και βρίσκεται στη θέση μνήμης 0 x 10000000 • Unary operator & → δίνει τη διεύθυνση: p = &c; gives address of c to p; – p “points to” c (p == 0 x 10000000) • Unary operator * → δίνει την τιμή στην οποία δείχνει ο pointer – if p = &c => * p == 100 (Dereferencing a pointer) • Dereferencing → data transfer in assembler –. . . =. . . *p. . . ; → load (get value from location pointed to by p) – *p =. . . ; → store (put value into location pointed to by p) cslab@ntua 2019 -2020 79

Pointer Arithmetic int x = 1, y = 2; /* x and y are

Pointer Arithmetic int x = 1, y = 2; /* x and y are integer variables */ int z[10]; /* an array of 10 ints, z points to start */ int *p; /* p is a pointer to an int */ p: x = 21; /* assigns x the new value 21 */ z[0] = 2; z[1] = 3 /* assigns 2 to the first, 3 to the next */ p = &z[0]; /* p refers to the first element of z */ p = z; /* same thing; p[i] == z[i]*/ p = p+1; /* now it points to the next element, z[1] */ z[1] p++; /* now it points to the one after that, z[2] */ z[0] *p = 4; /* assigns 4 to there, z[2] == 4*/ p = 3; /* bad idea! Absolute address! Compiler gives a warning*/ y: p = &x; /* p points to x, *p == 21 */ z = &y; /*illegal! array name is not a variable*/ x: z++; /*illegal for the same reason*/ cslab@ntua 2019 -2020 81 4 3 2 2 21

Constants – Constant reference A reference to a variable (here int), which is constant.

Constants – Constant reference A reference to a variable (here int), which is constant. We pass the variable as a reference mainly, because references are smaller in size than the actual value, but there is a side effect and that is because it is like an alias to the actual variable. We may accidentally change the main variable through our full access to the alias, so we make it constant to prevent this side effect. int var 0 = 0; const int * ptr 1 = & var 0; *ptr 1 = 8; // Error var 0 = 6; // OK cslab@ntua 2019 -2020 82 //const int & ptr 1 //ptr 1=8; in c++ = var 0; in c++

Constants – Constant pointers Once a constant pointer points to a variable then it

Constants – Constant pointers Once a constant pointer points to a variable then it cannot point to any other variable. int var 1 = 1; int var 2 = 0; int *const ptr 2 = &var 1; ptr 2 = &var 2; // Error cslab@ntua 2019 -2020 83

Constants – Pointer to constant A pointer through which one cannot change the value

Constants – Pointer to constant A pointer through which one cannot change the value of a variable it points is known as a pointer to constant. int const * ptr 3 = &var 2; *ptr 3 = 4; // Error cslab@ntua 2019 -2020 84

Constants – Constant pointer to a constant A constant pointer to a constant is

Constants – Constant pointer to a constant A constant pointer to a constant is a pointer that can neither change the address it's pointing to and nor can it change the value kept at that address. int var 3 = 0; int var 4 = 0; const int * const ptr 4 = &var 3; *ptr 4 = 1; // Error ptr 4 = &var 4; // Error cslab@ntua 2019 -2020 85

What's the difference between const int* p, int * const p and const int

What's the difference between const int* p, int * const p and const int * const p? You have to read pointer declarations right-to-left. const int * p means "p is a pointer to a constant integer" — that is, you can change the pointer, you cannot change the object where it points to. int * const p means "p is a constant pointer to an integer" — that is, you can change the integer via p, but you can't change the pointer p itself. const int* const p means "p is a const pointer to a const int" — that is, you can't change the pointer p itself, nor can you change the integer via p. cslab@ntua 2019 -2020 86

Let’s. . play! int* - pointer to int const * - pointer to const

Let’s. . play! int* - pointer to int const * - pointer to const int * const - const pointer to int const * const - const pointer to const int Now the first const can be on either side of the type so: const int * == int const * const int * const == int const * const int ** - pointer to int ** const - a const pointer to an int * const * - a pointer to a const pointer to an int const ** - a pointer to a const int * const - a const pointer to an int cslab@ntua 2019 -2020 87

int const or const int ? Η σειρά του τύπου και των qualifiers/specifiers στις

int const or const int ? Η σειρά του τύπου και των qualifiers/specifiers στις C/C++ δεν έχει σημασία. Δηλαδή, όλα τα παρακάτω είναι ισοδύναμα: • const volatile unsigned long int • volatile unsigned const int long • unsigned int volatile long const cslab@ntua 2019 -2020 88

Assembly Code : Παράδειγμα (1) Έστω ακέραιος c με τιμή 100 που βρίσκεται στη

Assembly Code : Παράδειγμα (1) Έστω ακέραιος c με τιμή 100 που βρίσκεται στη θέση μνήμης 0 x 10000000, p στον $a 0 και x στον $s 0 1. p = &c; /* p gets 0 x 10000000*/ lui $a 0, 0 x 1000 # p = 0 x 10000000 2. x = *p; /* x gets 100 */ lw $s 0, 0($a 0) # dereferencing p 3. *p = 200; /* c gets 200 */ addi $t 0, $0, 200 sw $t 0, 0($a 0) # dereferencing p cslab@ntua 2019 -2020 89

Assembly Code : Παράδειγμα (2) int strlen(char *s) { char *p = s; /*

Assembly Code : Παράδειγμα (2) int strlen(char *s) { char *p = s; /* p points to chars */ while (*p != ’’) p++; /* points to next char */ return p - s; /* end - start */ } mov $t 0, $a 0 lbu $t 1, 0($t 0) /* derefence p */ beq $t 1, $zero, Exit Loop: addi $t 0, 1 /* p++ */ lbu $t 1, 0($t 0) /* derefence p */ bne $t 1, $zero, Loop Exit: sub $v 0, $t 0, $a 0 jr $ra cslab@ntua 2019 -2020 90

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : Version 1 int x[100], y[100], z[100]; sumarray(x, y, z);

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : Version 1 int x[100], y[100], z[100]; sumarray(x, y, z); • C calling convention : sumarray(&x[0], &y[0], &z[0]); • Στην πραγματικότητα περνάμε pointers στους πίνακες addi $a 0, $gp, 0 # x[0] starts at $gp addi $a 1, $gp, 400 # y[0] above x[100] addi $a 2, $gp, 800 # z[0] above y[100] jal sumarray cslab@ntua 2019 -2020 97

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : Version 1 void sumarray(int a[], int b[], int c[])

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : Version 1 void sumarray(int a[], int b[], int c[]) { int i; for(i = 0; i < 100; i = i + 1) c[i] = a[i] + b[i]; } addi $t 0, $a 0, 400 # beyond end of a[] Loop: beq $a 0, $t 0, Exit lw $t 1, 0($a 0) # $t 1=a[i] lw $t 2, 0($a 1) # $t 2=b[i] add $t 1, $t 2 # $t 1=a[i] + b[i] sw $t 1, 0($a 2) # c[i]=a[i] + b[i] addi $a 0, 4 # $a 0++ addi $a 1, 4 # $a 1++ addi $a 2, 4 # $a 2++ j Loop Exit: jr $ra cslab@ntua 2019 -2020 98

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : Version 2 addi $t 0, $a 0, 400 #

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : Version 2 addi $t 0, $a 0, 400 # beyond end of a[] int *sumarray(int a[], int b[]) { addi $sp, -400 # space for c int i, c[100]; addi $t 3, $sp, 0 # ptr for c for(i=0; i<100; i=i+1) addi $v 0, $t 3, 0 # $v 0 = &c[0] c[i] = a[i] + b[i]; Loop: beq $a 0, $t 0, Exit return c; lw $t 1, 0($a 0) # $t 1=a[i] } lw $t 2, 0($a 1) # $t 2=b[i] add $t 1, $t 2 # $t 1=a[i] + b[i] sw $t 1, 0($t 3) # c[i]=a[i] + b[i] addi $a 0, 4 # $a 0++ $sp c[100] addi $a 1, 4 # $a 1++ addi $t 3, 4 # $t 3++ a[100] j Loop B[100] Exit: addi $sp, 400 # pop stack jr $ra cslab@ntua 2019 -2020 99

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : Version 3 addi $t 0, $a 0, 400 addi

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : Version 3 addi $t 0, $a 0, 400 addi $sp, -12 sw $ra, 0($sp) sw $a 0, 4($sp) sw $a 1, 8($sp) addi $a 0, $zero, 400 jal malloc addi $t 3, $v 0, 0 lw $a 0, 4($sp) lw $a 1, 8($sp) Loop: beq $a 0, $t 0, Exit. . . (loop as before on prior slide ) j Loop Exit: lw $ra, 0($sp) addi $sp, 12 jr $ra cslab@ntua 2019 -2020 102 # beyond end of a[] # space for regs # save $ra # save 1 st arg. # save 2 nd arg. # ptr for c # restore 1 st arg. # restore 2 nd arg. # restore $ra # pop stack

Επανάληψη MIPS (1) • Οι λέξεις (words) έχουν πλάτος 32 bits • Υπάρχει ένα

Επανάληψη MIPS (1) • Οι λέξεις (words) έχουν πλάτος 32 bits • Υπάρχει ένα αρχείο καταχωρητών με 32 καταχωρητές των 32 bits • Ονοματολογίες καταχωρητών: $0 - $31 ή με συμβολικά ονόματα: $zero, $at, $t 0, … $sp, $ra • Ο καταχωρητής $0 έχει πάντα την τιμή 0 (μηδέν) • Ειδικός καταχωρητής: Program Counter (PC) με πλάτος 32 bits $zero $0 zero $at $1 assembler temporary register $v 0, $v 1 $2, $3 expression evaluation and function result $a 0. . $a 3 $4. . $7 procedure arguments $t 0. . $t 7 $8. . $15 temporary $s 0. . $s 7 $16. . $23 saved temporaries $t 8, $t 9 $24, $25 temporary $k 0, $k 1 $26, $27 used by OS Kernel $gp $28 global pointer $sp $29 stack pointer $fp $30 frame pointer $ra $31 return address (from subroutine call) cslab@ntua 2019 -2020 104

Επανάληψη (3) cslab@ntua 2019 -2020 106

Επανάληψη (3) cslab@ntua 2019 -2020 106

Επανάληψη : Τρόποι Διευθυνσιοδότησης Addr. mode Παράδειγμα Έννοια χρήση Register add r 4, r

Επανάληψη : Τρόποι Διευθυνσιοδότησης Addr. mode Παράδειγμα Έννοια χρήση Register add r 4, r 3 Regs[r 4]← Regs[r 4]+ Regs[r 3] a value is in register Immediate add r 4, #3 Regs[r 4]← Regs[r 4]+3 for constants Displacement add r 4, 100(r 1) Regs[r 4]← Regs[r 4]+Mem[100+ Regs[r 1]] local variables Reg. indirect add r 4, (r 1) Regs[r 4]← Regs[r 4]+Mem[Regs[r 1]] accessing using a pointer or comp. address Indexed add r 4, (r 1+r 2) Regs[r 4]← Regs[r 4]+Mem[Regs[r 1]+ Regs[r 2]] array addressing (base +offset) Direct add r 4, (1001) Regs[r 4]← Regs[r 4]+Mem[1001] addr. static data Mem. Indirect add r 4, @(r 3) Regs[r 4]← Regs[r 4]+Mem[Regs[r 3]]] if R 3 keeps the address of a pointer p, this yields *p Autoincrement add r 4, (r 3)+ Regs[r 4]← Regs[r 4]+Mem[Regs[r 3]] Regs[r 3]← Regs[r 3]+d stepping through arrays within a loop; d defines size of an element Autodecrement add r 4, -(r 3) Regs[r 3]← Regs[r 3]-d Regs[r 4]← Regs[r 4]+Mem[Regs[r 3]] similar as previous Scaled add r 4, 100(r 2)[r 3] Regs[r 4]← Regs[r 4]+ Mem[100+Regs[r 2]+Regs[r 3]*d] to index arrays cslab@ntua 2019 -2020 107

Αναδρομική συνάρτηση factorial() long factorial(int n) { if (n == 0) return 1; else

Αναδρομική συνάρτηση factorial() long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1)); } cslab@ntua 2019 -2020 113

Αναδρομική συνάρτηση factorial() #1 factorial: # Prologos: apothikeysh dieythynshs epistrofhs, kai tou orismatos #

Αναδρομική συνάρτηση factorial() #1 factorial: # Prologos: apothikeysh dieythynshs epistrofhs, kai tou orismatos # to opoio xreiazetai kai meta thn anadromikh klhsh ths factorial addu $sp, -8 sw $ra, 4($sp) # save return address sw $a 0, 0($sp) # save argument (n) # Telos prologou bgtz $a 0, greater_than_zero # If we reach here, the argument is zero, and fact(0) = 1. li $v 0, 1 j epilogue cslab@ntua 2019 -2020 114

Αναδρομική συνάρτηση factorial() #2 greater_than_zero: sub $a 0, 1 # call factorial(n-1) jal factorial

Αναδρομική συνάρτηση factorial() #2 greater_than_zero: sub $a 0, 1 # call factorial(n-1) jal factorial # factorial(n-1) is in $v 0 lw $v 1, 0($sp) # restore n from stack mul $v 0, $v 1 # multiply n * factorial(n-1) # we are done, $v 0 has the correct value epilogue: lw $ra, 4($sp) # Epanafora ths dieythynshs # epistrofhs addiu $sp, 8 # Epanafora tou $sp sthn timh # prin thn klhsh jr $ra cslab@ntua 2019 -2020 115

Η Aloop με Caller-save καταχωρητές Αloop: addui sw $sp, -16 $ra, 0($sp) //push Loop:

Η Aloop με Caller-save καταχωρητές Αloop: addui sw $sp, -16 $ra, 0($sp) //push Loop: sw sw sw jal lw lw addi bnez move $a 0, 4($sp) //push $a 1, 8($sp) //push $t 0, 12($sp) //push Β $a 0, 4($sp) //pop $t 0, 12($sp) //pop $t 0, $v 0, $t 0 $a 1, 8($sp) //pop $a 1, -1 $a 1, Loop $v 0, $t 0 lw addui jr $ra, 0($sp) //pop $sp, 16 $ra cslab@ntua 2019 -2020 Εδώ η Aloop υλοποιείται με χρήση temporary καταχωρητών ($t 0, $a 1) Τρεις τιμές πρέπει να «επιβιώσουν» μετά την κλήση της Β, το Χ (στον $a 0), το i (στον $a 1) και το y (στον $t 0) ΣΥΝΟΛΟ SAVE/RESTORE = 3*N+1 / 3*N+1 132