Registermemory 80386 ABC Registerregister load store 1980 Load
Αρχιτεκτονικές Καταχωρητών γενικού σκοπού Register-memory Αφήνουν το ένα όρισμα να είναι στη μνήμη (πχ. 80386) A=B+C Register-register (load store) (1980+) Load R 1, B Add R 1, C Store A, R 1 Load R 1, B Load R 2, C Add R 3, R 1, R 2 Store A, R 3 extended-accumulator Memory-memory accumulator register-register-memory 7 cslab@ntua © 2006 -07
Μορφή Εντολής - 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 32 cslab@ntua © 2006 -07
MIPS R-Type (ALU) R-Type: Όλες οι εντολές της ALU που χρησιμοποιούν 3 καταχωρητές OP 6 bits rs 5 bits rt rd shamt funct 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 33 cslab@ntua © 2006 -07
MIPS I-Type : Load/Store OP rs rt 6 bits 5 bits address 16 bits – address: 16 -bit memory address offset in bytes added to base register. • Παραδείγματα : Offset base register in rs – Store word: sw – Load word: lw $1, 30($2) Destination register in rt source register in rt 500($4), $3 Offset base register in rs 37 cslab@ntua © 2006 -07
MIPS ALU I-Type Οι I-Type εντολές της ALU χρησιμοποιούν 2 καταχωρητές και μία σταθερή τιμή I-Type είναι και οι εντολές Loads/stores, conditional branches. OP rs rt 6 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 Result register in rt Constant operand in immediate Source operand register in rs cslab@ntua © 2006 -07 38
Αναπαράσταση Εντολών στον Υπολογιστή εντολή μορφή op rs rt rd shamt funct address 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 δ. ε. διευθ. 39 cslab@ntua © 2006 -07
Αναπαράσταση Εντολών στον Υπολογιστή Παράδειγμα (συνέχεια): op 35 0 43 rs 9 18 9 rt 8 8 8 op 100011 000000 101011 rs 010010 01001 rt 01000 rd 8 shamt 1200 0 1200 funct 32 rd shamt funct 0000 0100 1011 0000 8 0 32 0000 0100 1011 0000 42 cslab@ntua © 2006 -07
Λογικές Λειτουργίες (Πράξεις) 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 46 cslab@ntua © 2006 -07
Λογικές Λειτουργίες (Πράξεις) 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 47 cslab@ntua © 2006 -07
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 Έννοια $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 Σχόλια 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 49 cslab@ntua © 2006 -07
MIPS Logic/Shift Instructions Παραδείγματα Instruction and or xor nor and immediate or immediate xor immediate shift left logical shift right arithm. Παράδειγμα 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 Σχόλια 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 50 cslab@ntua © 2006 -07
Εντολές Λήψης Αποφάσεων Παράδειγμα: if(i == j) f = g + h; else f = g – h; με f, g, h, i, j αντιστοιχούνται σε $s 0, . . . , $s 4 version 1 version 2 bne $s 3, $s 4, Else beq $s 3, $s 4, Then add $s 0, $s 1, $s 2 sub $s 0, $s 1, $s 2 j Exit Else: sub $s 0, $s 1, $s 2 Then: add $s 0, $s 1, $s 2 Exit: 52 cslab@ntua © 2006 -07
Εντολές Λήψης Αποφάσεων Βρόχοι (Loops) while (save[i] == k) i += 1; με i = $s 3, k = $s 5, save base addr = $s 6 Loop: επί 4 sll add lw bne addi j Exit: cslab@ntua © 2006 -07 $t 1, $s 3, 2 #πολ/ζω i $t 1, $s 6 $t 0, 0($t 1) $t 0, $s 5, Exit $s 3, 1 Loop 53
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 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 55 cslab@ntua © 2006 -07
Υποστήριξη διαδικασιών στο υλικό των υπολογιστών leaf_example: int leaf_example(int g, addi $sp, -12 sw $t 1, 8($sp) #σώζουμε $t 1 int h, int i, int sw $t 0, 4($sp) #σώζουμε $t 0 j) sw $s 0, 0($sp) #σώζουμε $s 0 { add $t 0, $a 1 int f; add $t 1, $a 2, $a 3 # f=(g+h)–(i+j); f = (g+h) – (i+j); sub $s 0, $t 1 add $v 0, $s 0, $zero return f; lw $s 0, 0($sp) #επαν. $s 0 } lw $t 0, 4($sp) #επαν. $t 0 lw $t 1, 8($sp) #επαν. $t 1 Σκονάκι addi $sp, 12 g: $a 0, h: $a 1, i: $a 2, jr $ra # πίσω στον caller j: $a 3, f: $s 0 Παράδειγμα 63 cslab@ntua © 2006 -07
Υποστήριξη διαδικασιών στο υλικό των υπολογιστών Αναδρομική διαδικασία παραγοντικού int fact (int n){ if(n<1) return(1); else return(n*fact(n-1)); } fact: addi $sp, -8 sw $ra, 4($sp) #διεύθ. επιστροφής sw $a 0, 0($sp) #όρισμα n slti $t 0, $a 0, 1 beq $t 0, $zero, L 1 addi $v 0, $zero, 1 addi $sp, 8 jr $ra L 1: addi $a 0, -1 jal fact lw $a 0, 0($sp) lw $ra, 4($sp) addi $sp, 8 mul $v 0, $a 0, $v 0 jr $ra 67 cslab@ntua © 2006 -07
Υποστήριξη διαδικασιών στο υλικό των υπολογιστών Εκτέλεση: Μνήμη Έστω ότι καλούμε fact(3) και περιμένουμε να μας επιστρέψει το αποτέλεσμα στον καταχωρητή $v 0 fact: PC addi $sp, -8 sw $ra, 4($sp) #διεύθ. επιστροφής sw $a 0, 0($sp) #όρισμα n PC PC slti $t 0, $a 0, 1 PC beq $t 0, $zero, L 1 PC addi $v 0, $zero, 1 PC addi $sp, 8 jr $ra PC L 1: PC addi $a 0, -1 PC jal fact d: lw $a 0, 0($sp) PC PC lw $ra, 4($sp) PC addi $sp, 8 PC mul $v 0, $a 0, $v 0 PC jr $ra cslab@ntua © 2006 -07 PC: $sp $sp PC finish $sp l l-4 l-8 l-12 l-16 l-20 l-24 l-28 l-32 l-36 l-40 high m+8 3 d 2 d 1 d 0 βλ. m+8 PC m m+4 . . . $sp: l-32 l l-16 l-8 l-24 $ra: d m+8 $v 0: 2 1 6 PC PC PC m+8 m+4 m jal fact addi $a 0, $zero, 3 $a 0: 1 3 2 0 0 $t 0: 1 68 low
Υποστήριξη διαδικασιών στο υλικό των υπολογιστών Αναδρομική διαδικασία παραγοντικού int fact (int n){ if(n<1) return(1); else return(n*fact(n-1)); } Εκτός ύλης!!!! #include <stdio. h> int __attribute__((regparm(3)))fact(register int n); int main(void){ printf("%dn", fact(10)); } -----------------------#gcc –c fact. s #gcc caller. c fact. o –o exec #. /exec cslab@ntua © 2006 -07 x 86 Assembly 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. . globl fact: pushl %ebx movl %eax, %ebx decl %eax jz. L 4 call fact imull %ebx, %eax. L 3: popl %ebx ret. L 4: 69 movl $1, %eax
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) 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 0000 … 0000 74 cslab@ntua © 2006 -07
Εντολές διακλάδωσης-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 Jump jr $t 1 # goto $t 1 . . . είναι 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 © 2006 -07 75
MIPS Branch I-Type OP rs rt 6 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 rt Register in rs • Branch on equal beq $1, $2, 100 • Branch on not equal bne $1, $2, 100 offset in bytes equal to instruction field address x 4 76 cslab@ntua © 2006 -07
MIPS J-Type: jump j, jump and link jal OP jump target 6 bits 26 bits – jump target: jump memory address in words. Jump memory address in bytes equal to instruction field jump target x 4 • Παραδείγματα : – Branch on equal j – Branch on not equal jal 10000 77 cslab@ntua © 2006 -07
Τρόποι Διευθυνσιοδότησης στον 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) 79 cslab@ntua © 2006 -07
addi $rt, $rs, immediate π. χ. lui $t 0, 255 slti $t 0, $s 1, 10 add $rd, $rs, $rt π. χ. add $t 0, $s 1, $s 2 I-Type R-Type I-Type π. χ. lw $rt, address($rs) lw $t 1, 100($s 2) I-Type bne $rs, $rt, address π. χ. bne $s 0, $s 1, L 2 J-Type j address # goto (4 x address)(0: 28)-(29: 32) (PC) cslab@ntua © 2006 -07 80
Addressing Modes : Παραδείγματα 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 © 2006 -07 81
Επεξεργαστής Αριθμός καταχωρητών γενικού σκοπού Αρχιτεκτονική Έτος 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 Power. PC 32 load-store 1992 DEC Alpha 32 load-store 1992 82 cslab@ntua © 2006 -07 Στοιχεία από το: COD 2 e Patterson & Hennessy
Κανόνες Ονοματοδοσίας και Χρήση των 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 © 2006 -07 Χρήση 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 n. a. όχι ναι ναι ναι 83
- Slides: 83