Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack.

Slides:



Advertisements
Similar presentations
Ch. 7 Local Variables and Parameter Passing From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.
Advertisements

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.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
10/6: Lecture Topics Procedure call Calling conventions The stack
Stack Frames: Using LINK. CEG 320/5208: Stack frames and LINK2 Assembling source code One source file: –Use ORG statements for data and code –Assemble.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself.
The Structure of the CPU
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Subroutines and Stacks Lecture L3.1. Subroutine and Stacks The System Stack Subroutines A Data Stack.
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays (part 2) Differentiating Pointers from Data.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
7/23 C Programming in Embedded Systems Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
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.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
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:
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.
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.
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
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.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Figure 8.1 of course package
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
MIPS Instructions.
Subroutines and the Stack
Passing Parameters Data passed to a subroutine is called a parameter.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Logical and Decision Operations
Figure 8.1 of course package
10/4: Lecture Topics Overflow and underflow Logical operations
The MOVE Multiple: MOVEM Instruction
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Lecture 3 - Instruction Set - Al
Lecture 3 - Instruction Set - Al
Presentation transcript:

Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack

© 2006 Department of Computing Science CMPUT 229 A stack-based machine  Problem: Compute (A+B)  (C-D) in a stack-based machine. A Push A A B Push B A+B Add A+B C Push C A+B C D Push D A+B C-D Add (A+B)(C-D) Multiply Clements, pp. 264

© 2006 Department of Computing Science CMPUT 229 A memory stack-based machine  Problem: Compute (A+B)  (C-D) in a stack-based machine. Clements, pp. 264

© 2006 Department of Computing Science CMPUT 229 Pushing D0 into the stack Stack pointer (A7) always points to element at the top of the stack. - Decrement A7 before a push - Increment A7 after a pull Clements, pp. 265

© 2006 Department of Computing Science CMPUT 229 MOVEM  MOVEM saves and restores group of registers. Clements, pp. 265

© 2006 Department of Computing Science CMPUT 229 Subroutine Calling Conventions  Parameter Passing –Where? On registers On the stack frame –How? By value By reference  Register Preservation Conventions –Which registers are preserved by a function call?

© 2006 Department of Computing Science CMPUT 229 Register-Saving Convention for CMPUT 229  After Apple Computer’s C convention: RegisterPreserved by Function Call? D0-D2No D3-D7Yes A0No A1No A2-A6Yes A7Stack Pointer

© 2006 Department of Computing Science CMPUT 229 BSR and JSR  There are two instructions to call a subroutine: –BSR (Branch to Subroutine) Is relative to the current address –Range of - 32 kbytes to + 32 kbytes –Allows position-independent code –JSR (Jump to Subroutine) The address is absolute –Range is not limited –Code is position dependent

© 2006 Department of Computing Science CMPUT 229 Passing Parameter On The Stack Clements, pp. 273

© 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

© 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

© 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

© 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

© 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

© 2006 Department of Computing Science CMPUT 229 State of the Stack Clements, pp. 273

© 2006 Department of Computing Science CMPUT 229 PEA - Push Effective Address  PEA pushes the address specified into the stack. PEAX Is equivalent to MOVE.L#X, -(A7)

© 2006 Department of Computing Science CMPUT 229 Example: Add Two Numbers MK68K assembly: ORG$400 LEA$1000, A7Set up stack pointer PEAXPush address of variable X PEAYPush address of variable Y PEAZPush address of variable Z (the result) BSRAddUpCall adder routine MOVE.WZ, D2Read result (a dummy operation) LEA12(A7),A7Clean up stack STOP#$2700 * AddUpMOVEA.L12(A7), A0Get address of parameter X MOVEA.L8(A7), A1Get address of parameter Y MOVE.W(A0),D2Get value of X MOVE.W(A1),D3Get value of Y ADDD2, D3Add them MOVEA.L4(A7),A3Get address of parameter Z MOVE.WD3,(A3)Put result in variable Z RTS * ORG$500 XDC.W1 YDC.W2 ZDC.W1

© 2006 Department of Computing Science CMPUT 229 Example: Add Two Numbers (Parameter Passing) MK68K assembly: ORG$400 LEA$1000, A7Set up stack pointer PEAXPush address of variable X PEAYPush address of variable Y PEAZPush address of variable Z (the result) BSRAddUpCall adder routine MOVE.WZ, D2Read result (a dummy operation) LEA12(A7),A7Clean up stack STOP#$2700 * AddUpMOVEA.L12(A7), A0Get address of parameter X MOVEA.L8(A7), A1Get address of parameter Y MOVE.W(A0),D2Get value of X MOVE.W(A1),D3Get value of Y ADDD2, D3Add them MOVEA.L4(A7),A3Get address of parameter Z MOVE.WD3,(A3)Put result in variable Z RTS * ORG$500 XDC.W1 YDC.W2 ZDC.W1

© 2006 Department of Computing Science CMPUT 229 Example: Add Two Numbers MK68K assembly: ORG$400 LEA$1000, A7Set up stack pointer PEAXPush address of variable X PEAYPush address of variable Y PEAZPush address of variable Z (the result) BSRAddUpCall adder routine MOVE.WZ, D2Read result (a dummy operation) LEA12(A7),A7Clean up stack STOP#$2700 * MOVEA.L12(A7), A0Get address of parameter X MOVEA.L8(A7), A1Get address of parameter Y MOVE.W(A0),D2Get value of X MOVE.W(A1),D3Get value of Y ADDD2, D3Add them MOVEA.L4(A7),A3Get address of parameter Z MOVE.WD3,(A3)Put result in variable Z RTS * ORG$500 XDC.W1 YDC.W2 ZDC.W1

Parameter Passing By Reference Clements, pp. 278

Parameter Passing By Reference Clements, pp. 278

Parameter Passing By Reference Clements, pp. 278

Parameter Passing By Reference Clements, pp. 278

Parameter Passing By Reference Clements, pp. 278

© 2006 Department of Computing Science CMPUT 229 Procedure Call 1Place parameters in a place where the procedure can access them. 2Transfer control to procedure. 3 Acquire the storage resources needed for the procedure. 4Perform the procedure’s task. 5Place the result value in a place where the calling program can access it. 6Return control to the point of origin. Pat.-Hen. pp. 132

© 2006 Department of Computing Science CMPUT 229 The LINK instruction Clements, pp. 625 $1234 $8000 $8004 $7FFC $7FF8 $7FF4 $7FF0 $7FFC $7FF8 Memory SP $8000 A7(SP) $ABCC A5 BEFORE LINK A5, #-12 $ABCC $1234 $8000 $8004 $7FFC $7FF8 $7FF4 $7FF0 $7FFC $7FF8 Memory SP $7FF0 A7(SP) $7FFC A5 AFTER A5 Link creates a Local “workspace” in the stack to be used by a subroutine.

© 2006 Department of Computing Science CMPUT 229 The UNLK instruction Clements, pp. 639 $1234 $8000 $8004 $7FFC $7FF8 $7FF4 $7FF0 $7FFC $7FF8 Memory SP $8000 A7(SP) $ABCC A5 AFTER UNLK A5 $ABCC $1234 $8000 $8004 $7FFC $7FF8 $7FF4 $7FF0 $7FFC $7FF8 Memory SP $7FF0 A7(SP) $7FFC A5 BEFORE A5 Unlink collapses the stack to release workspace previously allocated by LINK.

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS i h g j $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC Memory A7(SP) $1234 A6 Before subroutine starts

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS $1234 i h g j Memory A7 $8000 A6 After the LINK instruction A6 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS f $1234 i h g j Memory A7 $8000 A6 Before UNLK instruction A6 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS f $1234 i h g j Memory A7 $1234 A6 After UNLK instruction $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS f $1234 i h g j Memory A7 $1234 A6 After RTS instruction $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L8(A6), D0Load g from stack into D0 ADD.L12(A6), D0 D0  g+h MOVE.L16(A6), D1Load i into D1 ADD.L20(A6), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS f $1234 i h g j Memory A7 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC Optimization

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } MK68K assembly: LINK.w A6,#-4 Make room in stack for 1 more item MOVE.L4(A7), D0Load g from stack into D0 ADD.L8(A7), D0 D0  g+h MOVE.L12(A7), D1Load i into D1 ADD.L16(A7), D1D1  i+j SUB.LD1, D0D0  D0-D1 MOVE.LD0, -4(A6)Write f into stack MOVE.L-4(A6), D0Read f from stack into D0 UNLKA6 RTS i h g j Memory A7 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC Further Optimization