CHAPTER 2 ISA Instructions (logical + procedure call)

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
1 Warning! Unlike the previous lessons, for today's lesson you will have to listen, think and even understand (for the exam, of course). Individuals with.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
CHAPTER 6 Instruction Set Architecture 12/7/
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Lec 6Systems Architecture1 Systems Architecture Lecture 6: Branching and Procedures in MIPS Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
CSCI206 - Computer Organization & Programming
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
What's wrong with this procedure?
Instructions - Type and Format
Logical and Decision Operations
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Logical and Decision Operations
Chapter 2 Instructions: Language of the Computer part 2
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Systems Architecture I
Computer Architecture
Program and memory layout
Compilers Jakub Yaghob
Instruction Set Architecture
Presentation transcript:

CHAPTER 2 ISA Instructions (logical + procedure call)

Logical operations Shift left  Example: sll $t2,$so,4  Reg t2 = $so << 4 Shift right  Example: srl $t2,$so,4  Reg t2 = $so >> 4 Bit-wise AND  Example: and $t0,$t1, $t2  Reg t0 = reg t1 & reg t2 Bit-wise OR  Example: or $t0,$t1,$t2  Reg $t0 = $t1 | $t2

Instructions for Selection (if..else) If (i == j) then f = g + h; else f = g – h; bne $s3,$s4, else add $s0,$s1,$s2 j done else: sub $s0,$s1,$s2 done:

Instructions for Iteration (while) while (save[i] == k) i = i + 1; Let i be in reg $s3 Let k be in reg $s5 Let $t1 have the address of Save array element Loop: sll $t1,$s3,2 add $t1,$t1$s6 lw $t0,0($t1) bne $t0,$s5,Exit addi $s3,$s3,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: a0, a1, a2, a3 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! R0 (r0) = R1 (at) = R2 (v0) = R3 (v1) = R4 (a0) = R5 (a1) = R6 (a2) = R7 (a3) = R8 (t0) = R9 (t1) = R10 (t2) = R11 (t3) = R12 (t4) = R13 (t5) = R14 (t6) = R15 (t7) = R16 (s0) = R17 (s1) = R18 (s2) = R19 (s3) = R20 (s4) = R21 (s5) = R22 (s6) = R23 (s7) = R24 (t8) = R25 (t9) = R26 (k0) = R27 (k1) = R28 (gp) = R29 (sp) = 7fffeffc R30 (s8) = R31 (ra) =

Procedure Call Conventions $a0-$a3 : four argument registers in which to pass parameters $vo-$v1: 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 ProcedureAddress 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: $a0,$a1,$a2,$a3 before the call Once inside we plan to use $s0, $t0, $t1; so we need save their values on the stack; Then use them to compute (g+h), (i+j) and f

MIPS code addi $sp,$sp,-12 # make room on the stack sw $t1,8($sp) # save the temp registers sw $t0,4($sp) sw $so,o($sp) add $to,$ao,$a1 # t0  g + h add $t1,$a2,$a3 # t1  i + j sub $so,$t0,$t1 # f = t0 – t1 add $v0,$so,$zero # returns f ($vo = $s0 + 0) lw $s0,0(sp) # restore saved values into temp registers from stack lw $t0,4(sp) lw $t1,8(sp) addi $sp,$sp,12 jr $ra # jump back to the return address

Allocating Space on Stack 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 sp fp Saved arguments Saved return addr Saved regs. Locals arrays & structures fp sp before during after sp fp

Delayed Branches Inst FetchDcd & Op Fetch Execute Branch: Inst Fetch Dcd & Op Fetch Inst Fetch Execute execute successor even if branch taken! Then branch target or continue Single delay slot impacts the critical path Compiler can fill a single delay slot with a useful instruction 50% of the time. try to move down from above jump move up from target, if safe add r3, r1, r2 sub r4, r4, 1 bzr4, LL NOP... LL: add rd,...

Delayed Branches li r3, #7 sub r4, r4, 1 bzr4, LL nop LL:sltr1, r3, r5 subir6, r6, 2 li r3, #7 sub r4, r4, 1 bzr4, LL subir6, r6, 2 LL:sltr1, r3, r5 compiler

Branch and Pipelines 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? ifetch LL:sltr1, r3, r5 li r3, #7 sub r4, r4, 1 bzr4, LL subi r6, r6, 2 Time execute Branch Delay Slot Branch Target ifetchexecute ifetchexecute ifetchexecute

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); } }