Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)

Slides:



Advertisements
Similar presentations
Week 8 Stack and Subroutines. Stack  The stack is a section of data memory (or RAM) that is reserved for storage of temporary data  The data may represent.
Advertisements

The University of Adelaide, School of Computer Science
680XX Program Examples Outline –Binary to BCD –Matrix Addition –Ones Count –String Compare –Sector Map –Raster Graphics –Subroutine Calls Goal –Understand.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
The University of Adelaide, School of Computer Science
9/20/6Lecture 3 - Instruction Set - Al Instruction Set.
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
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Data Transfer Group ECE 511: Digital System & Microprocessor.
Accessing parameters from the stack and calling functions.
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.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
5/6/99 Ashish Sabharwal1 JVM Architecture n Local storage area –Randomly accessible –Just like standard RAM –Stores variables (eg. an array) –Have to specify.
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.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack.
EECC250 - Shaaban #1 lec #8 Winter Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables,
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Stacks and Subroutines. Some example stacks Stacks and subroutine usage The stack is a special area of the random access memory in the overall memory.
7/23 C Programming in Embedded Systems Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Simple Procedure Calls jal: performs the control transfer (unconditional jump) to the starting address of procedure, while also saving the return.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
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:
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
1 Stacks, Subroutines, I/O Routines Today: First Hour: Stacks, Subroutines –Section 3.9,3.10 of Huang’s Textbook –In-class Activity #1 Second Hour: I/O.
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
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.
1 Subroutines Advanced Programming. 2 Top-Down approach to problem solving Algorithm step by step description of how to solve a problem Divide and Conquer.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
Figure 8.1 of course package
Subroutines … passing data
Subroutines and the Stack
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)
Lecture 3 - Instruction Set - Al
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C
Stack Frame Linkage.
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
68000 Architecture, Data Types and Addressing Modes
Reentrant Code a reentrant procedure can have several calls open to it at the same time in an interrupt environment, different ISRs may call the same routine.
; main program ; main move Param2, -(sp) move Param1, -(sp) Call Sub1
Review.
Figure 8.1 of course package
The MOVE Multiple: MOVEM Instruction
MIPS function continued
Subroutines … passing data
Program and memory layout
Program and memory layout
Subroutines and the Stack
Assembly Language Programming
Program and memory layout
ECE511: Digital System & Microprocessor
Lecture 3 - Instruction Set - Al
Presentation transcript:

Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr) then pushes the return address on stack. Subroutine should therefore provide an additional offset of four to access parameters on stack After returning from subroutine (rts), original parameters are still pushed Calling program must increment the stack pointer by the number of bytes the parameters occupy, in order to clean up the stack and bring the Stack Pointer (SP) to its original position.

; main program ; mainequ* … leastring, -(sp) ; move start address on stack clr.w-(sp) ; reserve space for result jsrconvert ; call subroutine move (sp)+, d0 ; result is in (sp), save it addi#4, sp ; clean up the stack … … ; end of code ; subroutine convert ; convertequ* movea.l6(sp), a2; first param is at 6(sp) ; second param is at 4(sp) …. move.wd1, 4(sp); assume result was in d1 rts; return ; ; data area ; stringds.b20 end SP  Return address4 bytes Space for result2 bytes String address4 bytes SP 

POWER Subroutine Example (Case 3) Parameter Passing by Value: Using The Stack - Main Program - MAINORG $400Main Program origin MOVEA.L#$07FFE,SPInitialize Stack Pointer MOVE.B B,D1 Put base number into D1 EXT.WD1Sign extend base to word length MOVE.W D1,-(SP) push base B onto the stack CLR.WD2Clear D2 before loading exponent MOVE.B E,D2 Put exponent number into D2 MOVE.W D2,-(SP) push exponent E onto the stack BSR POWER Call subroutine POWER MOVE.L (SP)+,D3 pop answer from stack resetting SP LEA A,A5 put address of answer into A5 MOVE.L D3,(A5)save answer MOVE#228,D7Done TRAP#14 ORG$600 B DC.B4Base number stored here E DC.B 2Exponent number stored here A DS.L1answer to be stored here

POWER Subroutine Example (Case 3) POWER Subroutine Example (Case 3) Parameter Passing by Value: Using The Stack Continued - Subroutine - ORG$800Subroutine POWER origin POWER MOVE.W 6(SP),D1copy base from stack to D1 CLR.WD2Clear D2 before loading exponent MOVE.B4(SP),D2copy exponent from to D2 MOVE.L #1,D3 initialize result in D3 to 1 LOOP MULS D1,D3 multiply result D3 with base D1 SUB #1,D2 decrement power in D2 by one BNE LOOPand repeat as long as power > 0 MOVE.LD3,4(SP)Push result onto the stack RTSDone, return to calling program

Effect on The Stack Word Just before calling Subroutine Current SP Base B Exponent E Initial SP Word Just before end of main Current SP = Initial SP Just after calling Subroutine Word Current SP Base B Exponent E Initial SP Return address 8 Word Just before return to main Current SP Answer Return address Initial SP 4 8 (Case 3)

POWER Subroutine Example (Case 4) POWER Subroutine Example (Case 4) Parameter Passing by Reference: Using The Stack - Main Program - MAINORG $400 Main Program origin MOVEA.L#$07FFE,SP Initialize Stack Pointer PEA B Push address of Base onto the stack PEA E Push address of Exponent onto the stack PEA A Push address of Answer onto the stack BSR POWER Call subroutine POWER LEA 12(SP),SP Stack clean-up: stack pointer reset MOVE#228,D7 Done TRAP#14 ORG$600 B DC.B4 Base number stored here E DC.B 2 Exponent number stored here A DS.L1 answer to be stored here

POWER Subroutine Example (Case 4) Parameter Passing by Reference: Using The Stack Continued - Subroutine - ORG$800Subroutine POWER origin POWER MOVEA.L12(SP),A1load Base address in A1 MOVEA.L 8(SP),A2load Exponent address in A2 MOVEA.L 4(SP),A3load Answer address address in A3 MOVE.B (A1),D1 Put base number into D1 EXT.WD1Sign extend base to word length CLR.WD2Clear D2 before loading exponent MOVE.B(A2),D2copy exponent from to D2 MOVE.L #1,D3 initialize result in D3 to 1 LOOP MULS D1,D3 multiply result D3 with base D1 SUB #1,D2 decrement power in D2 by one BNE LOOPand repeat as long as power > 0 MOVE.LD3,(A3)Save result in memory RTSDone, return to calling program

Effect on The Stack Word Just before calling Subroutine Current SP Initial SP Base Address Exponent Address Answer Address Word Just after calling Subroutine Current SP Initial SP Base Address Exponent Address Answer Address Return Address (Case 4) PEA B PEA E PEA A BSR POWER

Effect on The Stack Just after return to main Word Just after stack clean-up in Main Current SP = Initial SP Word Current SP Initial SP Base Address Exponent Address Answer Address (Case 4) LEA 12(SP),SP RTS

Saving/Restoring -Storage is in the order from a7 to a0, then d7 to d0 -Restore is in the opposite order, first d0 to d7, then a0 to a7

The MOVE Multiple: MOVEM Instruction This instruction saves or restores multiple registers. Useful in subroutines to save the values of registers not used to pass parameters. MOVEM has two forms: MOVEM register_list, MOVEM,register_list No effect on CCR. Example: Saving/restoring registers to from memory SUBR1MOVEM D0-D7/A0-A6,SAVEBLOCKSAVE D0-D7/A0-A6... MOVEM SAVEBLOCK,D0-D7/A0-A6Restore D0-D7/A0-A6 RTS Example: Saving/restoring registers using the stack (preferred method). SUBR1MOVEM D0-D7/A0-A6,-(SP)Push D0-D7/A0-A6 onto the stack... MOVEM (SP)+,D0-D7/A0-A6Restore D0-D7/A0-A6 from the stack RTS