Logical and Decision Operations CHAPTER 2 Logical operations
- Slides: 18
Logical and Decision Operations CHAPTER 2
Logical operations Shift left Example: sll $t 2, $so, 4 Reg t 2 = $so << 4 Shift right Example: srl $t 2, $so, 4 Reg t 2 = $so >> 4 Bit-wise AND Example: and $t 0, $t 1, $t 2 Reg t 0 = reg t 1 & reg t 2 Bit-wise OR Example: or $t 0, $t 1, $t 2 Reg $t 0 = $t 1 | $t 2
Instructions for Selection (if. . else) If (i == j) then f = g + h; else f = g – h; bne add j else: sub done: $s 3, $s 4, else $s 0, $s 1, $s 2 done $s 0, $s 1, $s 2
Instructions for Iteration (while) while (save[i] == k) i = i + 1; Let i be in reg $s 3 Let k be in reg $s 5 Let $t 1 have the address of Save array element Loop: sll $t 1, $s 3, 2 add $t 1, $t 1$s 6 lw $t 0, 0($t 1) bne $t 0, $s 5, Exit addi $s 3, 1 j Loop
Compiling C procedures int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) – (i + j); return f; } How do you pass the parameters? How does compiler transport the parameters?
Passing Parameters/arguments Special registers for arguments: a 0, a 1, a 2, a 3 Save temp register on the stack Perform operations And return value Restore values stored on the stack Jump back to return address
Steps in Execution of a Procedure Place parameters in a place where procedures can access them Transfer control to the procedure Acquire the storage resources needed for the procedure Perform the desired task Place the result value in a place where the calling program can access it Return control to the point of origin, since a procedure can be called from several points in a program
Register Mapping – Ahah! R 0 (r 0) = 0000 R 1 (at) = 0000 R 2 (v 0) = 0000 R 3 (v 1) = 0000 R 4 (a 0) = 0000 R 5 (a 1) = 0000 R 6 (a 2) = 0000 R 7 (a 3) = 0000 R 8 (t 0) = 0000 R 9 (t 1) = 0000 R 10 (t 2) = 0000 R 11 (t 3) = 0000 R 12 (t 4) = 0000 R 13 (t 5) = 0000 R 14 (t 6) = 0000 R 15 (t 7) = 0000 R 16 (s 0) = 0000 R 17 (s 1) = 0000 R 18 (s 2) = 0000 R 19 (s 3) = 0000 R 20 (s 4) = 0000 R 21 (s 5) = 0000 R 22 (s 6) = 0000 R 23 (s 7) = 0000 R 24 (t 8) = 0000 R 25 (t 9) = 0000 R 26 (k 0) = 0000 R 27 (k 1) = 0000 R 28 (gp) = 10008000 R 29 (sp) = 7 fffeffc R 30 (s 8) = 0000 R 31 (ra) = 0000
Procedure Call Conventions $a 0 -$a 3 : four argument registers in which to pass parameters $vo-$v 1: two value registers in which to return values $ra: one return address register to return to point of origin of the call MIPS assembly language also has a special instruction jal (jump and link) that saves the return address in $ra before transferring control to the procedure. Eg: jal Procedure. Address Another instruction “jr” transfers control back to the called location. Eg. jr $ra
Using the Stack “automatic storage” Automatic store for “workspace” and temporary registers. Use the stack: Use the stack pointer to access stack; Remember stack operates on LIFO Also note that $ZERO is a convenience register that stores the value 0 (check your green sheet attached to your textbook) Now we are ready to translate the procedure:
Translating the procedure int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) – (i + j); return f; } Parameters g, h, i, j will be stored in argument registers: $a 0, $a 1, $a 2, $a 3 before the call Once inside we plan to use $s 0, $t 1; so we need save their values on the stack; Then use them to compute (g+h), (i+j) and f
MIPS code addi sw sw sw $sp, -12 # make room on the stack $t 1, 8($sp) # save the temp registers $t 0, 4($sp) $so, o($sp) add $to, $a 1 # t 0 g + h add $t 1, $a 2, $a 3 # t 1 i + j sub $so, $t 0, $t 1 # f = t 0 – t 1 add $v 0, $so, $zero # returns f ($vo = $s 0 + 0) lw $s 0, 0(sp) # restore saved values into temp registers from stack lw $t 0, 4(sp) lw $t 1, 8(sp) addi $sp, 12 jr $ra # jump back to the return address
Allocating Space on Stack is used to save and restore registers when calling a procedure It may also be used to store return address It may be used to store arguments It may be used to store local arrays and data The segment of the stack containing a procedure’s saved registers and local variables is called “procedure frame” or “activation record” A frame pointer ($fp) points to first word of the frame of a procedure.
Stack and frame pointers before during fp sp fp Saved arguments Saved return addr Saved regs. Locals arrays & structures sp after fp sp
Delayed Branches Branch: Inst Fetch Dcd & Op Fetch Execute execute successor Inst Fetch even if branch taken! Then branch target or continue Dcd & Op Fetch Execute Inst Fetch Single delay slot impacts the critical path add r 3, r 1, r 2 • Compiler can fill a single delay slot with a useful instruction 50% of the time. sub r 4, 1 bz r 4, LL • try to move down from above jump NOP. . . • move up from target, if safe LL: add rd, . . .
Delayed Branches subi r 6, 2 li r 3, #7 sub r 4, 1 bz r 4, LL nop LL: slt compiler subi r 6, 2 r 1, r 3, r 5 LL: slt r 1, r 3, r 5
Branch and Pipelines Time li r 3, #7 execute sub r 4, 1 bz r 4, LL ifetch execute ifetch subi r 6, 2 LL: slt r 1, r 3, r 5 execute ifetch Branch Target Branch execute ifetch Delay Slot execute By the end of Branch instruction, the CPU knows whether or not the branch will take place. However, it will have fetched the next instruction by then, regardless of whether or not a branch will be taken. Why not execute it?
Putting it all together Sort procedure void sort (int v[], int n) { int i, j; for (i = 0; i < n; i = i+1) { // outer loop for (j = i - 1; j >= 0 && v[j] > v[j+1]; j = j-1){ swap(v, j); } }} See fig. 2. 34, 2. 36 Hwk 2: 2. 15 : due date: 2/6/2009, submit online.
- No decision snap decision responsible decision
- Slidetodoc.com
- Logical equivalence examples
- Logical form and logical equivalence
- Decision table and decision tree examples
- Boolean standpoint
- Chapter 6 prices and decision making assessment answers
- 5 step decision making process
- Chapter 2 economic systems and decision making
- Management chapter 5 planning and decision making
- Chapter 2 economic systems and decision making
- Chapter 2 economic systems and decision making answer key
- Chapter 11 decision making and relevant information
- Chapter 2 management supervision and decision making
- Relevant information for decision making
- Management chapter 5 planning and decision making
- Chapter 1 section 3 economic choices and decision making
- Operations strategy and competitiveness
- Chapter 2 operations strategy and competitiveness