Presentation is loading. Please wait.

Presentation is loading. Please wait.

11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.

Similar presentations


Presentation on theme: "11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering."— Presentation transcript:

1 11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology Taxila. 1

2 Instructions for Making Decisions Distinguishes computer from simple calculator. – Decision making Instructions are if with goto and label. MIPS decision making statements are: – Branch if equal-> beq register1, register2, L1 Go to label L1, if register1==register2 – Branch not equal-> bne register1, register2, L1 Go to label L1, if register1!=register2 These two statements are traditionally called Conditional Branches. 11/02/2009 2 CA&O Lecture 03 by Engr. Umbreen Sabir

3 MIPS Control Flow Instructions MIPS conditional branch instructions: bne $s0, $s1, Lbl#go to Lbl if $s0  $s1 beq $s0, $s1, Lbl#go to Lbl if $s0=$s1  Ex: if (i==j) h = i + j; bne $s0, $s1, Lbl1 add $s3, $s0, $s1 Lbl1:… 11/02/2009 3 CA&O Lecture 03 by Engr. Umbreen Sabir

4 MIPS Control Flow Instructions Instruction Format (I format): How is the branch destination address specified? Keep it pending till next chapter…. 11/02/2009 4 CA&O Lecture 03 by Engr. Umbreen Sabir op rs rt 16 bit offset

5 Compiling if-then-else into Conditional Branches: In the code below f, g, h, I and j are in registers $s0 through $s4. C code is – if ( i==j ) f= g + h; else f=g-h; Its assembly notation is: – Bne $s3, $s4, Else # go to Else if I != j – add $s0, $s1, $s2 # f= g + h (skipped if i != j). – j Exit # go to Exit. – Else:sub $s0, $s1, #s2 # f= g – h (skipped if i == j). 11/02/2009 5 CA&O Lecture 03 by Engr. Umbreen Sabir

6 Loops Decisions are also important for iterating a computation. – Found in loops. – Refer to next slide for an example. 11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir 6

7 Example # 1 C statements are: – While ( save[i] ==k) – i += 1; Assume i and k are in $s3 and $s5 and base of save is in $6. We must multiply i by 4 due to byte addressing problem. We use shift left logical since shifting left by 2 bits multiply by 4. 11/02/2009 7 CA&O Lecture 03 by Engr. Umbreen Sabir

8 Example # 1 cont. Assembly code will be: – Loop:sll $t1, $s3, 2 # Temp reg $t1 = 4 * i – Add $t1, $t1, $s6 # $t1= address of save[i] – Lw $t0, 0( $t1) # Temp reg $t0 = save[i] – Bne $t0, $s5, Exit # go to Exit if save[i] != k – Add $s3, $s3, 1 # i = i + 1; – J Loop # go to Loop – Exit: 11/02/2009 8 CA&O Lecture 03 by Engr. Umbreen Sabir

9 Basic Block A sequence of instructions without branches, except possibly at the end, and without branch targets or branch labels, except possibly at the beginning. One of the early phases of compilation is breaking the program into basic blocks. 11/02/2009 9 CA&O Lecture 03 by Engr. Umbreen Sabir

10 More Branch Instructions We have beq, bne, but what about other kinds of branches (e.g., branch-if-less-than)? For this, we need yet another instruction, slt. Set on less than instruction: slt $t0,$s0,$s1 # if $s0 < $s1 then # $t0 = 1else # $t0 = 0 Instruction format (R format): 11/02/2009 10 CA&O Lecture 03 by Engr. Umbreen Sabir op rs rt rd funct

11 More Branch Instructions cont. 11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir 11 Constant operands are also used in comparisons. Register $zero always has 0, we can compare to zero. To compare to other values, instruction is: Set on less than immediate: slti $t0,$s2,10 # $t0 = 1 if $s2< 10 MIPS doesn’t include branch on less than because it is too complicated. Either it would stretch the clock cycle time or it would take extra clock cycles/instruction.

12 More Branch Instructions cont. Can use slt, beq, bne, and the fixed value of 0 in register $zero to create all relative conditions. – less than blt $s1, $s2, Label – less than or equal to ble $s1, $s2, Label – greater than bgt $s1, $s2, Label – great than or equal to bge $s1, $s2, Label 11/02/2009 12 CA&O Lecture 03 by Engr. Umbreen Sabir slt $at, $s1, $s2#$at set to 1 if bne $at, $zero, Label# $s1 < $s2

13 Case/switch Statement Case/switch can be implemented through a sequence of conditional tests. More effectively done by jump address table. For this MIPS include a jump register instruction (jr), meaning an unconditional jump to the address specified in a register. 11/02/2009 13 CA&O Lecture 03 by Engr. Umbreen Sabir

14 Case/switch Statement 11/02/2009 14 CA&O Lecture 03 by Engr. Umbreen Sabir MIPS also has an unconditional branch instruction or jump instruction: j label#go to label.  Instruction Format (J Format): op 26-bit address

15 Supporting Procedures in Computer Hardware Procedure: A stored subroutine that performs a specific task based on the parameters with which it is provided. Procedure execution step – Place Parameters. – Transfer control to the procedure. – Acquire storage resources – Perform desired task. – Place result value. – Return control back. 11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir 15

16 Supporting Procedures in Computer Hardware cont. MIPS register conventions for procedures: – $a0 - $a3: argument registers – $v0 - $v1: two registers to return values. – $ra – return address register MIPS instruction for procedures is jump and link instruction (jal). – It jumps to an address and simultaneously saves the address of following instruction in return register $ra. 11/02/2009 16 CA&O Lecture 03 by Engr. Umbreen Sabir

17 Instructions for Accessing Procedures MIPS procedure call instruction: jal ProcedureAddress #jump n link Machine format (J format): Then can do procedure return with a jr$ra#return Instruction format (R format): 11/02/2009 17 CA&O Lecture 03 by Engr. Umbreen Sabir op rs funct op 26 bit address

18 Next Lecture and Reminders Next lecture – MIPS ISA Assignment Due – 13 Feb 2009 11/02/2009 18 CA&O Lecture 03 by Engr. Umbreen Sabir

19 END OF LECTURE 3 11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir 19


Download ppt "11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering."

Similar presentations


Ads by Google