Instruction Set Architecture

# Instruction Set Architecture

Télécharger la présentation

## Instruction Set Architecture

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Instruction Set Architecture MIPS Section 2.6-2.7,2.8,2.9

2. Instructions for Making Decisions beq reg1, reg2, L1 Go to the statement labeled L1 if the value in reg1 equals the value in reg2 bne reg1, reg2, L1 Go to the statement labeled L1 if the value in reg1does not equals the value in reg2 j L1 Unconditional jump jr \$t0 “jump register”. Jump to the instruction specified in register \$t0 Conditional Branches 2

3. Making Decisions Example if ( a != b) goto L1; // x,y,z,a,b mapped to \$s0-\$s4 x = y + z; L1 : x = x – a; bne \$s3, \$s4, L1 # goto L1 if a != b add \$s0, \$s1, \$s2 # x = y + z (ignored if a!=b) L1:sub \$s0, \$s0, \$s3 # x = x – a (always ex) Reminder Registers variable in C code \$s0 ... \$s7 \$16 ... 23 Registers temporary variable \$t0 ... \$t7 \$8 ... 15 Register \$zero always 0 3

4. if-then-else Example: if ( a==b) x = y + z; else x = y – z ; bne \$s3, \$s4, Else # goto Else if a!=b add \$s0, \$s1, \$s2 # x = y + z j Exit # goto Exit Else : sub \$s0,\$s1,\$s2 # x = y – z Exit : 4

5. Example: Loop with array index Loop: g = g + A [i]; i = i + j; if (i != h) goto Loop .... \$s1, \$s2, \$s3, \$s4 = g, h, i, j, array A base = \$s5 LOOP:add \$t1, \$s3, \$s3 #\$t1 = 2 * i add \$t1, \$t1, \$t1 #\$t1 = 4 * i add \$t1, \$t1, \$s5 #\$t1 = adr. Of A[i] lw \$t0, 0(\$t1) #load A[i] add \$s1, \$s1, \$t0 #g = g + A[i] add \$s3, \$s3, \$s4 #i = i + j bne \$s3, \$s2, LOOP 5

6. Loops Example : while ( A[i] == k ) // i,j,k in \$s3. \$s4, \$s5 i = i + j; // A is in \$s6 Loop: sll \$t1, \$s3, 2 # \$t1 = 4 * i add \$t1, \$t1, \$s6 # \$t1 = addr. Of A[i] lw \$t0, 0(\$t1) # \$t0 = A[i] bne \$t0, \$s5, Exit # goto Exit if A[i]!=k add\$s3, \$s3, \$s4 # i = i + j j Loop # goto Loop Exit: 6

7. Other decisions Set R1 on R2 less than R3slt R1, R2, R3 Compares two registers, R2 and R3 R1 = 1 if R2 < R3 elseR1 = 0 if R2 >= R3 Exampleslt \$t1, \$s1, \$s2 Branch less than Example: if(A < B) goto LESS slt \$t1, \$s1, \$s2 #t1 = 1 if A < B bne \$t1, \$0, LESS 7

8. Switch statement switch(k) { case 0 : f = i + j; break; case 1 : f = g + h; break; case 2 : f = g – h; break; case 3 : f = i – j; break; } f-k in \$s0-\$s5 and \$t2 contains 4 (maximum of var k) The switch statement can be converted into a big chain of if-then-else statements. A more efficient method is to use a jump address table of addresses of alternative instruction sequences and the jr instruction. Assume the table base address in \$t4 8

9. Switch cont. slt \$t3, \$s5, \$zero # is k < 0 bne \$t3, \$zero, Exit # if k < 0, goto Exit slt \$t3, \$s5, \$t2 # is k < 4, here \$t2=4 beq \$t3, \$zero, Exit # if k >=4 goto Exit sll \$t1, \$s5, 2 # \$t1 = 4 * k add \$t1, \$t1, \$t4 # \$t1 = addr. Of \$t4[k] lw \$t0, 0(\$t1) # \$t0 = \$t4[k] jr \$t0 # jump to addr. In \$t0 # \$t4[0]=&L0, \$t4[1]=&L1, …, L0 : add \$s0, \$s3, \$s4 # f = i + j j Exit L1 : add \$s0, \$s1, \$s2 # f = g + h j Exit L2 : sub \$s0, \$s1, \$s2 # f = g – h j Exit L3 : sub \$s0, \$s1, \$s2 # f = i – j Exit : 9

10. Instructions: bne \$t4,\$t5,Label Next instruction is at Label if \$t4 ≠ \$t5 beq \$t4,\$t5,Label Next instruction is at Label if \$t4 = \$t5 j Label Next instruction is at Label Formats: Addresses are not 32 bits — How do we handle this with large programs? Addresses in Branches and Jumps I J op rs rt 16 bit address op 26 bit address 10

11. PC-relative addressing For larger distances: Jump register jr required. 11

12. Example LOOP: mult \$9, \$19, \$10 # R9 = R19*R10lw \$8, 1000(\$9) # R8 = @(R9+1000) bne \$8, \$21, EXIT add \$19, \$19, \$20 #i = i + jj LOOP EXIT: ...... Assume LOOP is placed at location 80000 op rs rt 24 9 80000 0 19 10 0 1000 80004 35 9 8 80008 5 8 21 8 19 32 0 19 20 0 80012 80000 80016 2 80020 ... 12

13. Example LOOP: mult \$9, \$19, \$10 # R9 = R19*R10lw \$8, 1000(\$9) # R8 = @(R9+1000) bne \$8, \$21, EXIT add \$19, \$19, \$20 #i = i + jj LOOP EXIT: ... Assume LOOP is placed at location 80000 op rs rt 24 9 80000 0 19 10 0 1000 80004 35 9 8 80008 5 8 21 2 19 32 0 19 20 0 80012 20000 80016 2 80020 ... 2 20000 13

14. 15

15. Load Upper Immediate Example: lui \$t0, 255 Transfers the immediate field into the register’s top 16 bits and fills the register’s lower 16 bits with zeros R8[31:16] <-- IR[15:0] ; top 16 bits of R8 <-- bottom 16 bits of the IR R8[15:0] <-- 0; bottom 16 bits of R8 are zeroed 17

16. We'd like to be able to load a 32 bit constant into a register Must use two instructions, new "load upper immediate" instructionlui \$t0, 1010101010101010 Then must get the lower order bits right, i.e.,addi \$t0, \$t0, 0010101010101010 ori \$t0, \$t0, 0010101010101010 How about larger constants? filled with zeros 1010101010101010 0000000000000000 1010101010101010 0010101010101010 1010101010101010 0000000000000000 0000000000000000 0010101010101010 addi 18

17. Procedure calls Procedures or subroutines: Needed for structured programming Steps followed in executing a procedure call: Place parameters in a place where the procedure (callee) can access them Transfer control to the procedure Acquire the storage resources needed for the procedure Perform desired task Place results in a place where the calling program (caller) can access them Return control to the point of origin Leaf Procedures 19

18. Resources Involved Registers used for procedure calling: \$a0 - \$a3 : four argument registers in which to pass parameters \$v0 - \$v1 : two value registers in which to return values \$ra : one return address register to return to the point of origin Transferring the control to the callee: jal ProcedureAddress jump-and-link to the procedure address the return address (PC+4) is saved in \$ra Example: jal 20000 Returning the control to the caller: jr \$ra instruction following jal is executed next 20

19. Memory Stacks Useful for stacked environments/subroutine call & return even if operand stack not part of architecture Stacks that Grow Up vs. Stacks that Grow Down: High address 0 Little inf. Big a Memory Addresses grows up grows down SP b c inf. Big 0 Little Low address 21

20. Calling conventions int func(int g, int h, int i, int j) { int f; f = ( g + h ) – ( i + j ) ; return ( f ); } // g,h,i,j - \$a0,\$a1,\$a2,\$a3, f in \$s0 func : addi \$sp, \$sp, -12 #make room in stack for 3 words sw \$t1, 8(\$sp) #save the regs we want to use sw \$t0, 4(\$sp) sw \$s0, 0(\$sp) add \$t0, \$a0, \$a1 #\$t0 = g + h add \$t1, \$a2, \$a3 #\$t1 = i + j sub \$s0, \$t0, \$t1 #\$s0 has the result add \$v0, \$s0, \$zero #return reg \$v0 has f 22

21. Calling (cont.) lw \$s0, 0(\$sp) # restore \$s0 lw \$t0, 4(\$sp) # restore \$t0 lw \$t1, 8(\$sp) # restore \$t1 addi \$sp, \$sp, 12 # restore sp jr \$ra we did not have to restore \$t0-\$t9 (caller save) we do need to restore \$s0-\$s7 (must be preserved by callee) 23

22. Nested Calls Stacking of Subroutine Calls & Returns and Environments: A A: CALL B CALL C C: RET RET A B B: D: A B C A B A • Some machines provide a memory stack as part of the architecture (e.g., VAX, JVM) • Sometimes stacks are implemented via software convention 24

23. Compiling a String Copy Proc. void strcpy ( char x[ ], y[ ]) { int i=0; while ( x[ i ] = y [ i ] != 0) i ++ ; } // x and y base addr. are in \$a0 and \$a1 strcpy : addi \$sp, \$sp, -4 # reserve 1 word space in stack sw \$s0, 0(\$sp) # save \$s0 add \$s0, \$zer0, \$zer0 # i = 0 L1 : add \$t1, \$a1, \$s0 # addr. of y[ i ] in \$t1 lb \$t2, 0(\$t1) # \$t2 = y[ i ] add \$t3, \$a0, \$s0 # addr. Of x[ i ] in \$t3 sb \$t2, 0(\$t3) # x[ i ] = y [ i ] beq \$t2, \$zero, L2 # if y [ i ] = 0 goto L2 addi \$s0, \$s0, 1 # i ++ j L1 # go to L1 L2 : lw \$s0, 0(\$sp) # restore \$s0 addi \$sp, \$sp, 4 # restore \$sp jr \$ra # return 25

24. Thank You