Subroutines … passing data

Slides:



Advertisements
Similar presentations
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Advertisements

Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
680XX Program Examples Outline –Binary to BCD –Matrix Addition –Ones Count –String Compare –Sector Map –Raster Graphics –Subroutine Calls Goal –Understand.
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
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.
Chapters 9 & 10 Midterm next Wednesday (11/19) Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET The Stack SSP & USP Interrupts RTI.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines.
EECC250 - Shaaban #1 lec #8 Winter Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables,
System Calls 1.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
The Stack This is a special data structure: –The first item to be placed into the stack will be the last item taken out. Two basic operations: –Push: Places.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
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:
by Richard P. Paul, 2nd edition, 2000.
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.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
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.
Assembly Language Co-Routines
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.
CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
C Calling Conventions parameters are passed on the run-time or system stack, SP (or A7) parameters pushed on stack in “right to left” order of call A6.
Computer Architecture & Operations I
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
Figure 8.1 of course package
Procedures 101: There and Back Again
Subroutines … passing data
CS 3305 System Calls Lecture 7.
Lab 3 - Branching & Subroutines
Subroutines and the Stack
Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)
Functions and Procedures
Lecture 3 - Instruction Set - Al
CS 301 Fall 2002 Control Structures
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
Application Binary Interface (ABI)
Stack Frame Linkage.
Subroutines and the Stack
Passing Parameters Data passed to a subroutine is called a parameter.
by Richard P. Paul, 2nd edition, 2000.
Subroutines – parameter passing
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
Figure 8.1 of course package
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
The MOVE Multiple: MOVEM Instruction
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
ECE 3430 – Intro to Microcomputer Systems
Basic Instruction Cycle
Program and memory layout
Subroutines and the Stack
Some Assembly (Part 2) set.html.
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Computer Organization and Assembly Language
Lecture 3 - Instruction Set - Al
ECE511: Digital System & Microprocessor
Lecture 3 - Instruction Set - Al
Presentation transcript:

Subroutines … passing data Techniques: in registers in memory locations (rarely used) in a parameter block in the code stream on the system stack

Passing data in the code stream parameters placed directly after the subroutine call JSR FINDMIN ; find minimum of list DC.W 500 ; list length DC.L LIST ; list address MOVE.W D0,MIN ; store the minimum use standard DC assembler directives but without labels parameters must be fixed when program written must adjust the return address so that return is to executable code not the data area

JSR BUF_CMP ;go compare buffers DC.W 256 ;buffer length Version 1 e.g. call subroutine to compare two buffers, passing buffer length and pointers to two buffers … JSR BUF_CMP ;go compare buffers DC.W 256 ;buffer length DC.L BUFA ;buffer 1 address DC.L BUFB ;buffer 2 address next line of code BUF_CMP MOVEM.L D0/A0-A2,-(SP) ;save context . MOVEM.L (SP)+, D0/A0-A2 RTS BUFA DS.L 500 BUFB DS.L 500

JSR BUF_CMP ;go compare buffers DC.L PLIST ;parameter block addr Version 2 … JSR BUF_CMP ;go compare buffers DC.L PLIST ;parameter block addr next line of code … BUF_CMP MOVEM.L D0/A0-A2,-(SP) ;save context . MOVEM.L (SP)+, D0/A0-A2 RTS PLIST DC.W 256 ;buffer length DC.L BUFA ;buffer 1 address DC.L BUFB ;buffer 2 address BUFA DS.L 500 ;buffer 1 BUFB DS.L 500 ;buffer 2

Passing data on the stack parameters placed on stack before the subroutine call in the called routine, pull in the parameters using the SP as the base register after returning to the calling routine, adjust the stack pointer to remove the parameters from the stack e.g. … in the calling routine put parameters on stack JSR SUBR adjust stack pointer to clear stack

MOVE.L #PLIST,-(SP) <- JSR BUF_CMP ;go compare buffers Version 1 … PEA PLIST <- MOVE.L #PLIST,-(SP) <- JSR BUF_CMP ;go compare buffers . ;adjust for parameters BUF_CMP MOVEM.L D0/A0-A2,-(SP) ;save context . MOVEM.L (SP)+, D0/A0-A2 ;restore context RTS PLIST DC.W 256 ;buffer length DC.L BUFA ;buffer 1 address DC.L BUFB ;buffer 2 address BUFA DS.L 500 ;buffer 1 BUFB DS.L 500 ;buffer 2 Showing 2 different ways to put address of param block on stack. Pick one.

MOVE.W #256,-(SP) ;push buff length PEA BUFA ;push buffer 1 addr Version 2 … MOVE.W #256,-(SP) ;push buff length PEA BUFA ;push buffer 1 addr MOVE.L #BUFB,-(SP) ;push buffer 2 addr JSR BUF_CMP ;go compare buffers . BUF_CMP MOVEM.L D0/A0-A2,-(SP) ;save context MOVEM.L (SP)+, D0/A0-A2 ;restore context RTS BUFA DS.L 500 ;buffer 1 BUFB DS.L 500 ;buffer 2

M68000 Assembly Language [92p; N. Znotinas] Reading: See the programming examples that compare pass by value with pass by reference done via registers and via the stack - recommend that you trace the program and construct the stack for the last two examples before you look at the stack slides M68000 Assembly Language [92p; N. Znotinas] review operation of PEA instruction Expectations: you should be able to write a program utilizing all parameter passing techniques you should be able to analyze and explain the system stack contents for any program