1 CISC 2 RISC Complex Instruction Set Computer

  • Slides: 95
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 2016 -2017 accumulator register-register-memory 6

Κανόνες Ονοματοδοσίας και Χρήση των 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 2016 -2017 Χρήση 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 25 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 2016 -2017 27

Μορφή Εντολής – 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 2016 -2017 28

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 2016 -2017 29

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) base register in rs – Load word: lw $1, 30($2) base register in rs Destination register in rt cslab@ntua 2016 -2017 Offset 33

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 2016 -2017 34 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 2016 -2017 R 5 0000 … 0000 35

Αναπαράσταση Εντολών στον Υπολογιστή εντολή μορφή 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 2016 -2017 op rs rt 37 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 2016 -2017 rd shamt funct 1200 8 0 32 1200 rd shamt funct 0000 0100 1011 0000 8 0 0000 0100 1011 0000 38 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 2016 -2017 39

Λογικές Λειτουργίες (Πράξεις) (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 2016 -2017 41

Λογικές Λειτουργίες (Πράξεις) (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 2016 -2017 42

MIPS Arithmetic Instructions : Παραδείγματα Instruction Παράδειγμα add $1, $2, $3 subtract sub $1,

MIPS Arithmetic Instructions : Παραδείγματα Instruction Παράδειγμα add $1, $2, $3 subtract sub $1, $2, $3 add immediate addi $1, $2, 100 add unsigned addu $1, $2, $3 subtract unsigned subu $1, $2, $3 add imm. unsign. addiu $1, $2, 100 multiply mult $2, $3 multiply unsigned multu$2, $3 divide div $2, $3 divide unsigned divu $2, $3 Move from Hi mfhi $1 Move from Lo mflo $1 cslab@ntua 2016 -2017 Έννοια $1 = $2 + $3 $1 = $2 – $3 $1 = $2 + 100 $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 44 Σχόλια 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 2016 -2017 Παράδειγμα 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 45 Σχόλια 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 2016 -2017 Exit: 47

Εντολές Λήψης Αποφάσεων (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 2016 -2017 48

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 2016 -2017 Έννοια 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 procedure call 51

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

Εντολές διακλάδωσης – branching instructions branch if equal branch if !equal beq $s 3, 4 s 4, L 1 # goto L 1 if $s 3 equals $s 4 bne $s 3, 4 s 4, L 1 # goto L 1 if $s 3 not equals $s 4 unconditional jr $t 1 # goto $t 1 Jump . . . είναι I –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 2016 -2017 52

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

MIPS Branch I-Type OP 6 bits rs address rt 5 bits 16 bits – address: 16 -bit memory address branch target offset in words added to PC to form branch address. • Παραδείγματα : Register in rt Register in rs • Branch on equal Final offset is calculated in bytes, equals to {instruction field address} x 4, e. g. new PC = PC + 400 beq $1, $2, 100 • Branch on not equal bne $1, $2, 100 cslab@ntua 2016 -2017 53

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

MIPS J-Type: jump j, jump and link jal OP 6 bits jump target 26 bits – jump target: jump memory address in words. final jump memory address in bytes is calculated from {jump target} x 4 • Παραδείγματα : cslab@ntua 2016 -2017 – Jump j 10000 – Jump and Link jal 10000 54

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

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

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.

Τύποι Δεδομένων • 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 2016 -2017 60 – 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 χαρακτήρες • C I S E – Διευθύνσεις μνήμης (pointers) • 0 x 43495345 – Εντολές cslab@ntua 2016 -2017 61

Τύποι Δεδομένων : 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 lh $t 0, 20($s 0) – sh: save half word sh $t 0, 20($s 0) • Byte (8 bits) – lb (lbu): load byte – sb: save byte cslab@ntua 2016 -2017 lb $t 0, 20($s 0) sb $t 0, 20($s 0) 62

Εντολές λειτουργίας 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 0 x 10000000 lbu $s 1, 4($s 0) 1010 $s 0: 0 x 10000000 $s 1: 0 x 000000 AA cslab@ntua 2016 -2017 Memory Bytes 63

Παράδειγμα : Αντιγραφή String Void strcpy (char[], char y[]) { int i; i =

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

Παράδειγμα : Απομακρυσμένες Διευθύνσεις Text Segment (252 MB) 0 x 00400000 (0 x 07

Παράδειγμα : Απομακρυσμένες Διευθύνσεις 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 2016 -2017 70

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 2016 -2017 72

Pointer Arithmetic int x = 1, int z[10]; int *p; y = 2; /*

Pointer Arithmetic int x = 1, int z[10]; int *p; y = 2; /* x and y are integer variables */ /* an array of 10 ints, z points to start */ /* p is a pointer to an int */ 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] */ p++; /* now it points to the one after that, z[2] */ *p = 4; /* assigns 4 to there, z[2] == 4*/ p = 3; /* bad idea! Absolute address! Compiler gives a warning*/ p = &x; /* p points to x, *p == 21 */ z = &y; /*illegal! array name is not a variable*/ z++; /*illegal for the same reason*/ cslab@ntua 2016 -2017 73 p: z[1] z[0] 4 3 2 y: 2 x: 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 2016 -2017 74 //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 2016 -2017 75

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 2016 -2017 76

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 2016 -2017 77

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 2016 -2017 78

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 2016 -2017 79

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

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 2016 -2017 81

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 2016 -2017 82

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : 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 2016 -2017 86

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : 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]; } Loop: Exit: cslab@ntua 2016 -2017 addi beq lw lw add sw addi j jr $t 0, $a 0, 400 # beyond end of a[] $a 0, $t 0, Exit $t 1, 0($a 0) # $t 1=a[i] $t 2, 0($a 1) # $t 2=b[i] $t 1, $t 2 # $t 1=a[i] + b[i] $t 1, 0($a 2) # c[i]=a[i] + b[i] $a 0, 4 # $a 0++ $a 1, 4 # $a 1++ $a 2, 4 # $a 2++ Loop $ra 87

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

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : 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] cslab@ntua 2016 -2017 addi $t 0, $a 0, 400 # beyond end of a[] addi $sp, -400 # space for c addi $t 3, $sp, 0 # ptr for c addi $v 0, $t 3, 0 # $v 0 = &c[0] 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($t 3) # c[i]=a[i] + b[i] addi $a 0, 4 # $a 0++ addi $a 1, 4 # $a 1++ addi $t 3, 4 # $t 3++ j Loop Exit: addi $sp, 400 # pop stack jr $ra 88

Πίνακες, Δείκτες και Μέθοδοι/Διαδικασίες : 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 2016 -2017 90 # 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

Επανάληψη (1) cslab@ntua 2016 -2017 92

Επανάληψη (1) cslab@ntua 2016 -2017 92

Επανάληψη (2) cslab@ntua 2016 -2017 93

Επανάληψη (2) cslab@ntua 2016 -2017 93

Επανάληψη : Τρόποι Διευθυνσιοδότησης 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 2016 -2017 95