CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I If cars broadcast their speeds to.
Introduction to SPIM Simulator. 2 SPIM Simulator SPIM is a software simulator that runs programs written for MIPS R2000/R3000 processors SPIM ’ s name.
©UCB CPSC 161 Lecture 3 Prof. L.N. Bhuyan
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.
The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
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.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
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.
CSE331 W04.1Irwin&Li 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 4 Section 1: Mary Jane Irwin (
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
CS 300 – Lecture 10 Intro to Computer Architecture / Assembly Language Strings and Characters and More.
Procedure call frame: Hold values passed to a procedure as arguments
Lecture 8: MIPS Instruction Set
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
331 Week 3. 1Spring 2005 Review: MIPS Organization Processor Memory 32 bits 2 30 words read/write addr read data write data word address (binary) 0…0000.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
331 Week 3. 1Fall 2003 Head’s Up  This week’s material l MIPS control flow operations (for, while, if-the-else, …) -Reading assignment - PH 3.5  Reminders.
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 )
CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
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.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
CS Computer Architecture Spring 2006 Week 4 Paul Durand ( Course url:
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
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.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
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.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
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.
Computer Architecture & Operations I
Lecture 5: Procedure Calls
CS 230: Computer Organization and Assembly Language
Lecture 6: Assembly Programs
Procedures (Functions)
Procedures (Functions)
Assembly Programming using MIPS R3000 CPU
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
The University of Adelaide, School of Computer Science
Chapter 2 Instructions: Language of the Computer part 2
Lecture 6: Assembly Programs
Systems Architecture I
Assembly Programming using MIPS R3000 CPU
CSE 331 Computer Organization and Design Fall 2007 Week 4
Computer Architecture
MIPS function continued
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics Arizona State University Slides courtesy: Prof. Yann Hang Lee, ASU, Prof. Mary Jane Irwin, PSU, Ande Carle, UCB

CML CMLAnnouncements Quiz 1 –Collect your answer sheet from TA –Grades are online –Grade distribution is online (Avg. 53 points) Project 1 –Grades online Quiz 2 –Complete Chapter 2 MIPS Assembly Language Programming, including function calls –Thursday, Sept 24, 2009 Project 2 –MIPS Assembly Language Programming, including function calls –Will be posted tonight, will be due in a week –Will take a day or two to program

CML CML What have we learned So far –Write any program in MIPS Today –More Examples of Function Calls

CML CML High-level language program (in C) swap (int v[], int k)... Assembly language program (for MIPS) swap: sll $2, $5, 2 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31 Machine (object) code (for MIPS) Below the Program C - Compiler Assembler

CML MIPS Instructions, so far CategoryInstrOp CodeExampleMeaning Arithmetic (R format) add0 and 32add $s1, $s2, $s3$s1 = $s2 + $s3 subtract0 and 34sub $s1, $s2, $s3$s1 = $s2 - $s3 Data transfer (I format) load word35lw $s1, 100($s2)$s1 = Memory($s2+100) store word43sw $s1, 100($s2)Memory($s2+100) = $s1 load byte32lb $s1, 101($s2)$s1 = Memory($s2+101) store byte40sb $s1, 101($s2)Memory($s2+101) = $s1 Cond. Branch br on equal4beq $s1, $s2, Lif ($s1==$s2) go to L br on not equal5bne $s1, $s2, Lif ($s1 !=$s2) go to L set on less than0 and 42slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else $s1=0 Uncond. Jump jump2j 2500go to jump register0 and 8jr $t1go to $t1 jump and link3jal 2500go to 10000; $ra=PC+4

CML CML 32 read data MIPS Organization Processor Memory 32 bits 2 30 words read/write addr write data word address (binary) 0…0000 0…0100 0…1000 0…1100 1…1100 Register File src1 addr src2 addr dst addr write data 32 bits src1 data src2 data 32 registers ($zero - $ra) 32 PC ALU byte address (big Endian) Fetch PC = PC+4 DecodeExec Add 4 br offset

CML CML MIPS R3000 ISA Instruction Categories –Arithmetic –Load/Store –Jump and Branch –Floating Point coprocessor –Memory Management –Special R0 - R31 PC HI LO OP rsrt rdsafunct rs rtimmediate Registers R Format I Format 6 bits5 bits 6 bits 3 Instruction Formats: all 32 bits wide 6 bits5 bits 16 bits J Format 6 bits26 bits jump target

CML CML Programming Styles Procedures (subroutines) allow the programmer to structure programs making them –easier to understand and debug and –allowing code to be reused Procedures allow the programmer to concentrate on one portion of the code at a time –parameters act as barriers between the procedure and the rest of the program and data, allowing the procedure to be passed values (arguments) and to return values (results)

CML CML Requirements for Functions Pass arguments to the function –$a0, $a1, $a2, $a3 Get results from the function –$v0, $v1 Can call from anywhere –jal Can always return back –jr Nested and Recursive Functions –Save $ra on stack Saving and Restoring Registers Functions with more than 4 parameters

CML CML Steps for Making a Function Call 1) Save necessary values onto stack 2) Assign argument(s), if any 3) jal call 4) Restore values from stack

CML CML Example Function sumSquare: addi $sp,$sp,-8# space on stack sw $ra, 4($sp)# save ret addr sw $a1, 0($sp)# save y add $a1,$a0,$zero # mult(x,x) jal mult # call mult lw $a1, 0($sp) # restore y add $v0,$v0,$a1 # mult()+y lw $ra, 4($sp) # get ret addr addi $sp,$sp,8 # restore stack jr $ra mult:... int sumSquare(int x, int y) { return mult (x,x)+ y; } push pop

CML CML Rules for Function Calls Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3 Return value is always in $v0 (and if necessary in $v1) Must follow register conventions –even in functions that only you will call!

CML CML Other Registers $at : may be used by the assembler at any time; unsafe to use $k0-$k1 : may be used by the OS at any time; unsafe to use $gp, $fp : dont worry about them –Feel free to read up on $gp and $fp in Appendix A, but you can write perfectly good MIPS code without them.

CML CML Basic Structure of a Function entry_label: addi $sp,$sp, -framesize sw $ra, framesize-4($sp) # save $ra save other regs if need be... restore other regs if need be lw $ra, framesize-4($sp) # restore $ra addi $sp,$sp, framesize jr $ra Epilogue Prologue Body (call other functions…) ra memory

CML CML Register Conventions CalleR: the calling function CalleE: the function being called When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged. Register Conventions: A set of generally accepted rules as to which registers will be unchanged after a procedure call ( jal ) and which may be changed.

CML CML Register Conventions None guaranteed inefficient –Caller will be saving lots of regs that callee doesnt use! All guaranteed inefficient –Callee will be saving lots of regs that caller doesnt use! Register convention: A balance between the two.

CML CML Register Conventions – Saved Registers $0: No Change. Always 0. $s0-$s7: Restore if you change. Very important, thats why theyre called saved registers. If the callee changes these in any way, it must restore the original values before returning. $sp: Restore if you change. The stack pointer must point to the same place before and after the jal call, or else the caller wont be able to restore values from the stack. HINT -- All saved registers start with S!

CML CML Register Conventions – Volatile Registers $ra: Can Change. The jal call itself will change this register. Caller needs to save on stack if nested call. $v0-$v1: Can Change. These will contain the new returned values. $a0-$a3: Can change. These are volatile argument registers. Caller needs to save if theyll need them after the call. $t0-$t9: Can change. Thats why theyre called temporary: any procedure may change them at any time. Caller needs to save if theyll need them afterwards.

CML MIPS Register Convention NameRegister Number UsageShould preserve on call? $zero0the constant 0n.a. $v0 - $v12-3returned valuesno $a0 - $a34-7argumentsyes $t0 - $t78-15temporariesno $s0 - $s716-23saved valuesyes $t8 - $t924-25temporariesno $gp28global pointeryes $sp29stack pointeryes $fp30frame pointeryes $ra31return addressyes

CML CML Register Conventions What do these conventions mean? –If function R calls function E, then function R must save any temporary registers that it may be using onto the stack before making a jal call. –Function E must save any S (saved) registers it intends to use before garbling up their values –Remember: Caller/callee need to save only temporary/saved registers they are using, not all registers.

CML CML Requirements for Functions Pass arguments to the function –$a0, $a1, $a2, $a3 Get results from the function –$v0, $v1 Can call from anywhere –jal Can always return back –jr Nested and Recursive Functions –Save $ra on stack Saving and Restoring Registers –Register Conventions Functions with more than 4 parameters –Pass them on the stack

CML CML Nested Procedures Leaf procedures do not call other procedures. –What happens to return addresses with nested procedures? int rt_1 (int i) { if (i == 0) return 0; else return rt_2(i-1); } caller:jalrt_1 next:... rt_1:bne$a0, $zero, to_2 add$v0, $zero, $zero jr$ra to_2:addi$a0, $a0, -1 jalrt_2 jr$ra rt_2:...

CML CML Nested Procedures Outcome caller:jalrt_1 next:... rt_1:bne$a0, $zero, to_2 add$v0, $zero, $zero jr$ra to_2:addi$a0, $a0, -1 jalrt_2 jr$ra rt_2:... On the call to rt_1, the return address ( next in the caller routine) gets stored in $ra. What happens to the value in $ra (when i != 0 ) when rt_1 makes a call to rt_2 ? int rt_1 (int i) { if (i == 0) return 0; else return rt_2(i-1); }

CML CML caller rt addr Saving the Return Address, Part 1 Nested procedures ( i passed in $a0, return value in $v0 ) high addr $sp low addr $a0 value $ra bk_2 old TOS rt_1:bne$a0, $zero, to_2 add$v0, $zero, $zero jr$ra to_2:addi$sp, $sp, -8 sw$ra, 4($sp) sw$a0, 0($sp) addi$a0, $a0, -1 jalrt_2 bk_2:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 jr$ra Save the return address (and arguments) on the stack int rt_1 (int i) { if (i == 0) return 0; else return rt_2(i-1); } $a0 $rt_2 $a0 value $a0 value$a0 value - 1

CML CML Compiling a Recursive Procedure Calculating factorial: int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } Recursive procedure (one that calls itself!) fact (0) = 1 fact (1) = 1 * 1 = 1 fact (2) = 2 * 1 * 1 = 2 fact (3) = 3 * 2 * 1 * 1 = 6 fact (4) = 4 * 3 * 2 * 1 * 1 = Assume n is passed in $a0 ; result returned in $v0

CML CML Compiling a Recursive Procedure fact:addi$sp, $sp, -8#adjust stack pointer sw$ra, 4($sp)#save return address sw$a0, 0($sp)#save argument n slt$t0, $a0, 1#test for n < 1 beq$t0, $zero, L1#if n >=1, go to L1 addi$v0, $zero, 1#else return 1 in $v0 addi$sp, $sp, 8#adjust stack pointer jr$ra#return to caller L1:addi$a0, $a0, -1#n >=1, so decrement n jalfact#call fact with (n-1) #this is where fact returns bk_f:lw$a0, 0($sp)#restore argument n lw$ra, 4($sp)#restore return address addi$sp, $sp, 8#adjust stack pointer mul$v0, $a0, $v0#$v0 = n * fact(n-1) jr$ra#return to caller int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); }

CML 1 caller rt addr A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 $a0 = 2 2 bk_f old TOS int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } fact:addi$sp, $sp, -8 sw$ra, 4($sp) sw$a0, 0($sp) slt$t0, $a0, 1 beq$t0, $zero, L1 addi$v0, $zero, 1 addi$sp, $sp, 8 jr$ra L1:addi$a0, $a0, -1 jalfact bk_f:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 mul$v0, $a0, $v0 jr$ra

CML 1 caller rt addr A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 $a0 = 2 bk_f old TOS Stack state after execution of first encounter with the jal instruction (second call to fact routine with $a0 now holding 1) –saved return address to caller routine (i.e., location in the main routine where first call to fact is made) on the stack –saved original value of $a0 on the stack int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); }

CML 1 bk_f A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 $a0 = 1 0 bk_f old TOS int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } caller rt addr $a0 = 2 fact:addi$sp, $sp, -8 sw$ra, 4($sp) sw$a0, 0($sp) slt$t0, $a0, 1 beq$t0, $zero, L1 addi$v0, $zero, 1 addi$sp, $sp, 8 jr$ra L1:addi$a0, $a0, -1 jalfact bk_f:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 mul$v0, $a0, $v0 jr$ra

CML caller rt addr A Look at the Stack for $a0 = 2 $ra $a0 $v0 $a0 = 2 0 bk_f old TOS $sp $a0 = 1 bk_f Stack state after execution of second encounter with the jal instruction (third call to fact routine with $a0 now holding 0) –saved return address of instruction in caller routine (instruction after jal ) on the stack –saved previous value of $a0 on the stack int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); }

CML 1 bk_f A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 $a0 = 1 0 bk_f old TOS int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } caller rt addr $a0 = 2 fact:addi$sp, $sp, -8 sw$ra, 4($sp) sw$a0, 0($sp) slt$t0, $a0, 1 beq$t0, $zero, L1 addi$v0, $zero, 1 addi$sp, $sp, 8 jr$ra L1:addi$a0, $a0, -1 jalfact bk_f:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 mul$v0, $a0, $v0 jr$ra bk_f $a0 = 0

CML 1 bk_f A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 $a0 = 1 0 bk_f old TOS int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } caller rt addr $a0 = 2 Stack state after execution of first encounter with the first jr instruction ( $v0 initialized to 1) –stack pointer updated to point to third call to fact

CML 1 A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 0 bk_f old TOS int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } caller rt addr $a0 = 2 fact:addi$sp, $sp, -8 sw$ra, 4($sp) sw$a0, 0($sp) slt$t0, $a0, 1 beq$t0, $zero, L1 addi$v0, $zero, 1 addi$sp, $sp, 8 jr$ra L1:addi$a0, $a0, -1 jalfact bk_f:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 mul$v0, $a0, $v0 jr$ra bk_f $a0 = 1 1 1*1 = 1

CML A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 bk_f old TOS int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } caller rt addr $a0 = Stack state after execution of first encounter with the second jr instruction (return from fact routine after updating $v0 to 1 * 1) –return address to caller routine ( bk_f in fact routine) restored to $ra from the stack –previous value of $a0 restored from the stack –stack pointer updated to point to second call to fact

CML 1 A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 1 bk_f old TOS int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } caller rt addr $a0 = 2 fact:addi$sp, $sp, -8 sw$ra, 4($sp) sw$a0, 0($sp) slt$t0, $a0, 1 beq$t0, $zero, L1 addi$v0, $zero, 1 addi$sp, $sp, 8 jr$ra L1:addi$a0, $a0, -1 jalfact bk_f:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 mul$v0, $a0, $v0 jr$ra 2 2*1 = 2 caller rt addr

CML A Look at the Stack for $a0 = 2 $sp $ra $a0 $v0 old TOS 2 2 caller rt addr Stack state after execution of second encounter with the second jr instruction (return from fact routine after updating $v0 to 1 * 1 * 2) –return address to caller routine (main routine) restored to $ra from the stack –original value of $a0 restored from the stack –stack pointer updated to point to first call to fact int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); }

CML MIPS Register Convention NameRegister Number UsageShould preserve on call? $zero0the constant 0n.a. $v0 - $v12-3returned valuesno $a0 - $a34-7argumentsyes $t0 - $t78-15temporariesno $s0 - $s716-23saved valuesyes $t8 - $t924-25temporariesno $gp28global pointeryes $sp29stack pointeryes $fp30frame pointeryes $ra31return addressyes

CML MIPS Instructions CategoryInstrOp CodeExampleMeaning Arithmetic (R format) add0 and 32add $s1, $s2, $s3$s1 = $s2 + $s3 subtract0 and 34sub $s1, $s2, $s3$s1 = $s2 - $s3 Data transfer (I format) load word35lw $s1, 100($s2)$s1 = Memory($s2+100) store word43sw $s1, 100($s2)Memory($s2+100) = $s1 load byte32lb $s1, 101($s2)$s1 = Memory($s2+101) store byte40sb $s1, 101($s2)Memory($s2+101) = $s1 Cond. Branch br on equal4beq $s1, $s2, Lif ($s1==$s2) go to L br on not equal5bne $s1, $s2, Lif ($s1 !=$s2) go to L set on less than0 and 42slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else $s1=0 Uncond. Jump jump2j 2500go to jump register0 and 8jr $t1go to $t1 jump and link3jal 2500go to 10000; $ra=PC+4

CML CML Yoda says… Do or do not... there is no try