1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.

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

Lecture 5: MIPS Instruction Set
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.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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.
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
Lecture 8: MIPS Instruction Set
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
RISC Concepts, MIPS ISA and the Mini–MIPS project
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 4. Miscellaneous Addressing Mode, Memory Map, Pointer, and ASCII Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced.
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
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
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.
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.
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.
CPEG323 Homework Review I Long Chen October, 17 th, 2005.
MIPS Programming.
Computer Architecture & Operations I
CSCI206 - Computer Organization & Programming
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
CSCI206 - Computer Organization & Programming
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
Instructions - Type and Format
CSCI206 - Computer Organization & Programming
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 5: Procedure Calls
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS Microarchitecture Multicycle Processor
MIPS Functions.
Lecture 6: Assembly Programs
MIPS function continued
MIPS Functions.
Topic 2b ISA Support for High-Level Languages
Conditional Branching (beq)
Presentation transcript:

1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007

2 Arrays

3 Arrays // high-level code int array[5]; array[0] = array[0] * 2; array[1] = array[1] * 2; # MIPS assembly code # array base address = $s0 lui $s0, 0x1234 # put 0x1234 in upper half of $S0 ori $s0, $s0, 0x8000 # put 0x8000 in lower half of $s0 lw $t1, 0($s0) # $t1 = array[0] sll $t1, $t1, 1 # $t1 = $t1 * 2 sw $t1, 0($s0) # array[0] = $t1 lw $t1, 4($s0) # $t1 = array[1] sll $t1, $t1, 1 # $t1 = $t1 * 2 sw $t1, 4($s0) # array[1] = $t1

4 Arrays: Using for Loops // high-level code int array[1000]; int i; for (i=0; i < 1000; i = i + 1) array[i] = array[i] * 2;

5 Accessing Arrays: Using for Loops

6 Procedure Calls // high-level code int main() { simple();... } void simple(){ return; } # MIPS assembly code 0x00 main: jal simple 0x04 add $s0, $s1, $s2... 0x20 simple: jr $ra

7 Input Arguments, Return Values // high-level code int main() { int y;... y = diffofsums(2, 3, 4, 5);... } int diffofsums(int f, int g, int h, int i) { int result; result = (f + g) - (h + i); return result; }

8 Input Arguments, Return Values # MIPS assembly # $s0 = y main:... addi $a0, $0, 2 # argument 0 = 2 addi $a1, $0, 3 # argument 1 = 3 addi $a2, $0, 4 # argument 2 = 4 addi $a3, $0, 5 # argument 3 = 5 jal diffofsums # call procedure add $s0, $v0, $0 # y = returned value... # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

9 The Stack The stack is accessed using the stack pointer ($sp)

10 How Procedures use the Stack Called procedures must store the return value in $v0 but have no other unintended side effects. # MIPS assembly # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

11 Storing Register Values on the Stack # $s0 = result diffofsums: addi $sp, $sp, -12 # make space on stack # to store 3 registers sw $s0, 8($sp) # save $s0 on stack sw $t0, 4($sp) # save $t0 on stack sw $t1, 0($sp) # save $t1 on stack add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $t1, 0($sp) # restore $t1 from stack lw $t0, 4($sp) # restore $t0 from stack lw $s0, 8($sp) # restore $s0 from stack addi $sp, $sp, 12 # deallocate stack space jr $ra # return to caller

12 The Stack during Procedure Call

13 Registers PreservedNonpreserved $s0 - $s7$t0 - $t9 $ra$a0 - $a3 $sp$v0 - $v1 stack above $spStack below $sp

14 Storing Register Values on the Stack # $s0 = result diffofsums: addi $sp, $sp, -4 # make space on stack to # store one register sw $s0, 0($sp) # save $s0 on stack add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $s0, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller

15 Addressing Modes How do we address the operands? Register Only Immediate Base Addressing PC-Relative Pseudo Direct

16 Addressing Modes Register Only Addressing Operands found in registers Immediate Addressing 16-bit immediate used as an operand

17 Addressing Modes Base Addressing Address of operand is: a register value (base address) + sign-extended immediate

18 Addressing Modes PC-Relative Addressing 0x10 beq $t0, $0, else 0x14 addi $v0, $0, 1 0x18 addi $sp, $sp, i 0x1C jr $ra 0x20 else: addi $a0, $a0, -1 0x24 jal factorial

19 Addressing Modes Pseudo-direct Addressing 0x C jal sum... 0x004000A0 sum: add $v0, $a0, $a1

20 How do we run an Application?

21 What needs to be stored in memory?

22 What needs to be stored in memory?

23 Running a Program

24 Example Program: C Code int f, g, y; // global variables int main(void) { f = 2; g = 3; y = sum(f, g); return y; } int sum(int a, int b) { return (a + b); }

25 Example Program: Assembly Code int f, g, y; // global variables int main(void) { f = 2; g = 3; y = sum(f, g); return y; } int sum(int a, int b) { return (a + b); }.data f: g: y:.text main: addi $sp, $sp, -4 # stack frame sw $ra, 0($sp) # store $ra addi $a0, $0, 2 # $a0 = 2 sw $a0, f # f = 2 addi $a1, $0, 3 # $a1 = 3 sw $a1, g # g = 3 jal sum # call sum sw $v0, y # y = sum() lw $ra, 0($sp) # restore $ra addi $sp, $sp, 4 # restore $sp jr $ra # return to OS sum: add $v0, $a0, $a1 # $v0 = a + b jr $ra # return

26 Example Program: Symbol Table SymbolAddress

27 Example Program: Executable

28 Example Program: In Memory

29 Next Time Transmission Lines