Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.

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.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
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)
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
Procedure call frame: Hold values passed to a procedure as arguments
Assembly Language II CPSC 321 Andreas Klappenecker.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
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.
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.
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 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
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 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
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.
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
CSCI206 - Computer Organization & Programming
Lecture 5: Procedure Calls
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Assembly Programming using MIPS R3000 CPU
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
Instructions - Type and Format
MIPS Procedures.
Addressing in Jumps jump j Label go to Label op address 2 address
Solutions Chapter 2.
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
MIPS function continued
Program and memory layout
Systems Architecture I
MIPS function continued
Assembly Programming using MIPS R3000 CPU
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
MIPS function continued
Presentation transcript:

Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11

Computer Architecture CSE 3322 Graduate Teaching Assistant Pramod Kumar Office Hours: Tues – Thurs 10 – 1 pm 114 Engr Annex West pxk Send to Pramod Kumar with the names and s of your Four Project team members by Mon Sept 15. If not on a team, send your address to Pramod.

Procedure Calls 1.Place parameters where the procedure can access them 2.Transfer control to the procedure 3.Perform the task of the procedure 4.Place the results where the calling program can access them 5. Return control to the point of the call Allocate registers to hold data for procedure calls $a0 - $a3 : four registers to pass parameters $v0 - $v1 : two registers to return values $ra : one return address register Need jump-and-link instruction : jal ProcedureAddress means :save return address in $ra and jumps to ProcedureAddress

Procedure Calls How can we preserve “saved registers” of the calling procedure ? What if there are not enough registers allocated to pass parameters and values ?

Procedure Calls Store the registers in memory using a stack. High Low $sp push $s0 contents of $s0 pop $s0 Stack

Stack Processes A stack is a last-in-first-out queue

Stack Processes A stack is a last-in-first-out queue The stack pointer, $sp, points to the most recently allocated address.

Stack Processes A stack is a last-in-first-out queue The stack pointer, $sp, points to the most recently allocated address. By convention, stacks grow from higher addresses to lower addresses.

Stack Processes A stack is a last-in-first-out queue The stack pointer, $sp, points to the most recently allocated address. By convention, stacks grow from higher addresses to lower addresses. To push $s0, $s1, and $s2, first reduce $sp three words and then save the registers.

Push on the Stack High Low $sp push $s2, $s1, and $s0 contents of $s2 addi $sp, $sp, -12# adjust stack pointer 3 words sw $s2, 8($sp)# store $s2 at $sp + 8 sw $s1, 4($sp)# store $s1 at $sp + 4 sw $s0, 0($sp)# store $s0 at $sp contents of $s1 contents of $s0

Pop off the Stack High Low $sp pop $s0, $s1, and $s2 contents of $s2 lw $s0, 0($sp) # restore $s0 from $sp lw $s1, 4($sp) # restore $s1 from $sp + 4 lw $s2, 8($sp) # restore $s2 from $sp + 8 addi $sp, $sp, 12 # adjust stack pointer 3 words contents of $s1 contents of $s0

Procedure Call High Low $sp push $s2, $s1, and $s0 contents of $s2 contents of $s1 contents of $s0 pop $s0, $s1, and $s2 1.Save the registers used by the procedure by pushing on the stack at the start

Procedure Call High Low $sp push $s2, $s1, and $s0 contents of $s2 contents of $s1 contents of $s0 pop $s0, $s1, and $s2 1.Save the registers used by the procedure by pushing on the stack at the start 2. Restore the registers used by the procedure by popping off the stack at the end

Procedure Call High Low $sp push $s2, $s1, and $s0 contents of $s2 contents of $s1 contents of $s0 pop $s0, $s1, and $s2 Also data and results can be transferred between the procedure and calling program using the stack

Procedure Call Conventions By agreement the following registers are preserved: Saved Registers: $s0 - $s7 Return Address: $ra Which means that the called routine must return to the calling program with these registers unchanged. If the called routine changes any of these ( includes calling a routine) it must first save them on the stack and restore them upon return.

Procedure Call Conventions By agreement the following registers are preserved: Saved Registers: $s0 - $s7 Return Address: $ra Which means that the called routine must return to the calling program with these registers unchanged. If the called routine changes any of these ( includes calling a routine) it must first save them on the stack and restore them upon return. The stack must be kept correct, so the Stack Pointer, $sp, and the Stack above the Stack Pointer must be the same across the procedure call.

Procedure Call Conventions By agreement the following registers are not preserved: Temporary Registers: $t0 - $t9 Argument Registers: $a0 - $a3 Return Value Registers: $v0 - $v1 Stack below the stack pointer Which means that the calling routine must push any of these registers on the stack that are needed after the call. Why not just push all the registers on the stack ? When would this be necessary ?

MIPS Register Conventions Name Register Usage Preserved Number Across Call $zero 0 constant 0na $v0-$v1 2-3values for resultsno $a0-$a3 4-7argumentsno $t0-$t78-15temporariesno $s0-$s savedyes $t8-$t more temporariesno $gp 28global pointeryes $sp 29stack pointeryes $fp 30frame pointeryes $ra 31return addressyes Registers not listed are used by Assembler and OS

Procedure Check List 1.Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops.

Procedure Check List 1.Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops. 2.To call a routine, the return address. $ra, must be saved before the call (jal) and restored upon return.

Procedure Check List 1.Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops. 2.To call a routine, the return address. $ra, must be saved before the call (jal) and restored upon return. Exception: Main program that cannot be called. 3.Any saved registers, $s0 - $s7, that are used must be saved and restored upon return to the contents at the time the procedure was called.

Procedure Check List 1.Maintain the integrity of the stack. Return it exactly as it was received. Balance pushes and pops. 2.To call a routine, the return address. $ra, must be saved before the call (jal) and restored upon return. Exception: Main program that cannot be called. 3.Any saved registers, $s0 - $s7, that are used must be saved and restored upon return to the contents at the time the procedure was called. 4.Any temporary, argument or value register, $t0 - $t9, $a0 - $a3, $v0 - $v1, that need to be preserved across the call must be saved before the call and restored upon returned

Calculate n factorial = n(n-1)(n-2)(n-3)...1

A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); }

Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 What do we need to save?

Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 What do we need to save? $a0 ? $v0 ? $ra ?

Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact:# push $a0 and $ra addi $sp, $sp, -8# adjust $sp for 2 words sw $ra, 4($sp)# save return address sw $a0, 0($sp)# save the argument n $sp $ra $a0

Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1

Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1 addi $v0, $zero, 1 # return value of 1

Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1 addi $v0, $zero, 1 # return value of 1 Return to calling procedure Pop $a0, $ra ?

Calculate n factorial = n(n-1)(n-2)(n-3)...1 A recursive C procedure that calculates factorial int fact (int n ) { if ( n<1 ) return (1); else return ( n * fact (n – 1)); } Assign n to $a0 and return value to $v0 fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1 addi $v0, $zero, 1 # return value of 1 addi $sp, $sp, 8 # adjust $sp for 2 words jr $ra # return

Calculate n * fact ( n – 1 ) L1:addi $a0, $a0, -1 # argument is ( n – 1) jal fact # call fact with ( n – 1)

Calculate n * fact ( n – 1 ) L1:addi $a0, $a0, -1 # argument is ( n – 1) jal fact # call fact with ( n – 1) Returns to this procedure with $v0 = (n-1)!

Calculate n * fact ( n – 1 ) L1:addi $a0, $a0, -1 # argument is ( n – 1) jal fact # call fact with ( n – 1) Returns to this procedure with $v0 = (n-1)! Restore $a0 and $ra Calculate n * (n-1)! Return to calling procedure

Calculate n * fact ( n – 1 ) L1:addi $a0, $a0, -1 # argument is ( n – 1) jal fact # call fact with ( n – 1) # pop $a0 and $ra lw $a0, 0($sp) # return from jal; restore to n lw $ra, 4($sp) # restore return address addi $sp, $sp, 8 # adjust $sp for 2 words $sp $ra $a0 $sp

Calculate n * fact ( n – 1 ) L1:addi $a0, $a0, -1 # argument is ( n – 1) jal fact # call fact with ( n – 1) # pop $a0 and $ra lw $a0, 0($sp) # return from jal; restore to n lw $ra, 4($sp) # restore return address addi $sp, $sp, 8 # adjust $sp for 2 words mul $v0, $a0, $v0 # return value of n * fact(n–1) jr $ra # return to caller mul, the multiply instruction will be covered later

fact: addi $sp, $sp, -8 # adjust $sp for 2 words sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1 go to L1 addi $v0, $zero, 1 # return value of 1 addi $sp, $sp, 8 # adjust $sp for 2 words jr $ra # return 8 for n<1 or n*5 instructions otherwise (n+1)*2 memory accesses

L1:addi $a0, $a0, -1 # argument is ( n – 1) jal fact # call fact with ( n – 1) # pop $a0 and $ra lw $a0, 0($sp) # return from jal; restore to n lw $ra, 4($sp) # restore return address addi $sp, $sp, 8 # adjust $sp for 2 words mul $v0, $a0, $v0 # return value of n * fact(n–1) jr $ra # return to caller n*7 instructions n* 2 memory accesses

n*7 instructions n* 2 memory accesses 8 or n*5 instructions (n+1)*2 memory accesses Total 8+n*12 instructions (2n+1)*2 memory accesses

Calculate n factorial = n(n-1)(n-2)(n-3)...1 int fact (int n, int k) {k=1; for ( i = 1; i <= n; i = i + 1) { k = k * i } return( k ) }

Addressing in Jumps op address 2address jump j Label go to Label 6 bits 26 bits

Addressing in Jumps op address 2address jump j Label go to Label 6 bits 26 bits The complete 32 bit address is : address 00 4 bits 26 bits 2 bits Upper 4 bits of the Program Counter, PC jump uses word addresses This is Pseudodirect Addressing. Note: 256 MB word boundaries address * 4 = address:00

Branch Addressing op rs rt address op rs rt address address branch on equal beq $s1, $s2, Label if ($s1 = =$s2) go to Label branch on not equal bne $s1, $s2, Label if ($s1 != $s2) go to Label

Branch Addressing op rs rt address address beq $s1, $s2, Label if ($s1 = =$s2) go to Label 6 bits 5 bits 5 bits 16 bits effective 32 bit address = ( PC + 4 ) + ( address * 4 ) Next Instruction address is the relative number of instructions This is PC-relative addressing address * 4 = address : 00

Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i Some Code beq $t0, $zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: ?

Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i Some Code beq $t0, $zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: ? (PC+4)+?*4

Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i Some Code beq $t0, #zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit:

Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i Some Code beq $t0, #zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: ? 48044

Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i Some Code beq $t0, #zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: ? ?*4

Branch Addressing Loop:add $t1, $s1, $s1# $t1 = 2 * i Some Code beq $t0, #zero, Exit# if A[i]=0 goto Exit add $s1, $s1, $s2# i = i + j j Loop# goto Loop Exit: