.1 1999©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan

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
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
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.
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
Procedure call frame: Hold values passed to a procedure as arguments
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
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.
Cs 61C L3 Decisions.1 Patterson Spring 99 ©UCB CS61C Constants and Making Decisions in C/Assembly Language Lecture 3 January 27, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
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.
RISC Concepts, MIPS ISA and the Mini–MIPS project
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
331 Practice Exam.1Fall 2003 Naming Conventions for Registers 0$zero constant 0 (Hdware) 1$atreserved for assembler 2$v0expression evaluation & 3$v1function.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures I Internet2, a non-profit advanced.
©UCB CPSC 161 Lecture 5 Prof. L.N. Bhuyan
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CHAPTER 2 ISA Instructions (logical + procedure call)
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CSE331 W02.1Irwin Fall 2001 PSU Computer Architecture Discussion Lecture # 2 MIPS Programming.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1.
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.
IT 251 Computer Organization and Architecture More MIPS Control Instructions Chia-Chi Teng.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
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.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I Apple took a bold step recently.
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
Lecture 2: Instruction Set Architecture part 1 (Introduction) Mehran Rezaei.
MIPS Lab CMPE 142 – Operating Systems. MIPS Simulator First Time Startup Setting Options.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
1. CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions 2 Instructors: Vladimir Stojanovic & Nicholas Weaver
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Logical and Decision Operations
Systems Architecture I
Computer Architecture
Presentation transcript:

©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan

©UCB While in C/Assembly: Summary while (save[i]==k) i = i + j; ( i, j, k : $s3,$s4,$s5: base of save[] :$s6) Loop: add $t1,$s3,$s3 #$t1 = 2*i add $t1,$t1,$t1 #$t1 = 4*i add $t1,$t1,$s6 #$t1=Addr lw $t1,0($t1) #$t1=save[i] bne $t1,$s5,Exit#goto Exit #if save[i]!=k add $s3,$s3,$s4# i = i + j j Loop# goto Loop Exit: C MIPSMIPS

©UCB If less in C/Assembly if (g < h) go to Less slt $t0,$s0,$s1 # $t0 = 1 if # $s0<$s1 (g<h) bne $t0,$zero, Less # goto Less # if $t0!=0... #(if (g<h)) Less: A branch if $t0 != 0 branches if g < h. Register $zero always 0, so use bne comparing register $t0 to register $zero °How test if (g >= h)? C MIPSMIPS

©UCB C case/switch statement °Choose among four alternatives depending on whether k has the value 0, 1, 2, or 3 switch (k) { case 0: f=i+j; break; /* k=0*/ case 1: f=g+h; break; /* k=1*/ case 2: f=g–h; break; /* k=2*/ case 3: f=i–j; break; /* k=3*/ }

©UCB Case/Switch via Jump Address Table °Notice that last case must wait for n-1 tests before executing, making it slow °Alternative tries to go to all cases equally fast: jump address table for scale-ability Idea: encode alternatives as a table of addresses of the cases -Table is an array of words with addresses corresponding to case labels Program indexes into table and jumps °MIPS instruction “jump register” ( jr ) unconditionally branches to address in register; use load to get address

©UCB Jump Address Table: Summary slti $t3,$s5,0 #Test if k =4,goto Exit add $t1,$s5,$s5# Temp reg $t1 = 2*k add $t1,$t1,$t1# Temp reg $t1 = 4*k add $t1,$t1,$t2 #$t1=addr JumpTable[k] lw $t1,0($t1) # $t1 = JumpTable[k] jr $t1 # jump based on $t1 L0:add $s0,$s3,$s4# k=0 so f = i + j j Exit# end case, goto Exit L1:add $s0,$s1,$s2# k=1 so f = g + h j Exit# end case, goto Exit L2:sub $s0,$s1,$s2# k=2 so f = g – h j Exit # end case, goto Exit L3:sub $s0,$s3,$s4# k=3 so f = i – j Exit:# end of switch statement

©UCB MIPS Instruction Set Revealed So Far °MIPS Instructions: arithmetic: add, sub, addi, slt, slti data transfer: lw, sw conditional branch: beq, bne unconditional branch: j, jr °Machine Language Formats: R-Register I-Immediate J- Jump

©UCB 2.7 Functions and procedures Place parameters in argument registers – where the procedure can access them Transfer control to the procedure Acquire storage needed for procedure Perform desired task Place the result in value registers – where the calling program can access them Return control to the main program by using jra instruction

©UCB Function Call Bookkeeping Registers used: $a0, $a1, $a2, $a3 $ra $v0, $v1 $s0, $s1, …, $s7 ; Must consider: Parameters (arguments) Return address Return value Local variables Jr $ra – Jump to the address contained in the return register ra.

©UCB Passing Parameters and Return... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } Caller Program Pass up to four parameters in the argument registers : $a0, $a1, $a2, $a3, and jump to procedure Sum add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero # y = b 1008 addi $ra,$zero,1016 #$ra= j sum #jump to sum <- Return address 2000 sum:... 20xx jr $ra C MIPSMIPS

©UCB Instruction Support for Functions °Want single instruction to jump and save return address: jump and link ( jal ): °Before: 1008 addi $ra,$zero,1016 #$ra= j sum #go to sum °After: 1008 jal sum # $ra=1012,go to sum

©UCB Saving the Register Contents ° The procedure may use the same registers that were used by the main (caller) and will be later needed by the main,so these must be saved in the stack by the procedure before using them °After the procedure completes, load these values from the stack back to the registers. Then return to the main by using jr. °To limit the register spilling, MIPS uses the following convention. 1. $t0-$t9: 10 temporary registers, not preserved by the procedure (callee) 2. $s0-s7: 8 saved registers that must be preserved if used by the procedure.

©UCB Exceeding limits of registers °Recall: assembly language has fixed number of operands, HLL doesn’t °Local variables: $s0,..., $s7,$t0, …, $t9 What if more than 18 words of local variables? °Arguments; $a0,..., $a3 What if more than 4 words of arguments? More argument registers needed for nested loops More return addresses (Ra) needed for nested loops °Place extra local variables and extra arguments onto stack

©UCB Convert to a Procedure °Compile by hand using registers: f = (g + h) - (i + j); Register Allocations: f: $s0, g: $s1, h: $s2, i: $s3, j: $s4 °MIPS Instructions: add $s0,$s1,$s2# $s0 = g+h add $t1,$s3,$s4# $t1 = i+j sub $s0,$s0,$t1# f=(g+h)-(i+j) If it’s a procedure, g,h,i,j will be passed through $a0,$a1,$a2,$a3. Registers $s0,and $t1 need to be saved in the stack. addi $sp,$sp, -8 # Adjust stack for 2 items sw $t1, 4($sp) # save register $t1 sw $s0, 0($sp) add $s0,$a0,$a1# $s0 = g+h add $t1,$a2,$a3# $t1 = i+j sub $s0,$s0,$t1# f=(g+h)-(i+j) add $v0,$s0,$zero # Return value of f Body

©UCB Restore Registers Before returning, restore the old values in the registers lw $s0, 0($sp) lw $t1, 4($sp) addi $sp, $sp, 8 # adjust stack pointer jr $ra # Jump back to main

©UCB 0zero constant 0 1atreserved for assembler 2v0expression evaluation & 3v1function results 4a0arguments 5a1 6a2 7a3 8t0temporary: caller saves...(callee can clobber) 15t7 MIPS: Software conventions for Registers 16s0callee saves... (caller can clobber) 23s7 24t8 temporary (cont’d) 25t9 26k0reserved for OS kernel 27k1 28gpPointer to global area 29spStack pointer 30fpframe pointer 31raReturn Address (HW) Plus a 3-deep stack of mode bits.

©UCB Nested Procedures Call Procedure within procedure. How to pass arguments? How to store return address? The conflicts are solved by pushing all these registers into the stack.

©UCB The Stack Stack Before procedure call $sp During procedure call stack grows down $sp Stack After procedure Call $sp $sp always points to “top” stack element and $FP (Frame Pointer) points to the first word of procedure frame $FP Stack Saved argument registers (if any, addressed by FP) 2. Saved return address 3. Saved saved registers (if any from nested loops) 4 Local arrays and structures

©UCB 2.8 Instruction Support for Characters °MIPS (and most other instruction sets) include 2 instructions to move bytes: Load byte ( lb ) loads a byte from memory, placing it in rightmost 8 bits of a register Store byte ( sb ) takes a byte from rightmost 8 bits of register and writes it to memory °Declare byte variables in C as “ char ” °Assume x, y are declared char, y in memory at 0($gp) and x at 1($gp). What is MIPS code for x = y ; ? lb $t0,0($gp)# Read byte y sb $t0,1($gp)# Write byte x

©UCB °Must keep instructions same size, but immediate field ( addi ) only 16 bits °Add instruction to load upper 16 bits, then later instruction gives lower 16 bits load upper immediate ( lui ) sets the upper 16 bits of a constant in a register Machine language version of lui $s0,15 Contents of $s0 after executing lui $s0, What if constant bigger than 16 bits? oprt rs

©UCB Big Constant Example °C: i = 80000; /* i:$s1 */ °MIPS Asm: ten = two lui $s1, 1 addi $s1,$s1,14464# °MIPS Machine Language $s1: