Parameter Passing - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Parameter Passing

Description:

Arguments are information passed to a procedure when it is called ... the procedure. Arguments may need to be removed from the stack if the procedure does not ... – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 15
Provided by: timma87
Category:

less

Transcript and Presenter's Notes

Title: Parameter Passing


1
Parameter Passing
  • Assembly Language Programming
  • University of Akron
  • Dr. Tim Margush

2
Arguments and Parameters
  • Arguments are information passed to a procedure
    when it is called
  • Parameters receive the information passed by an
    argument
  • Passing mechanisms
  • Call by Value
  • Call by Reference

3
Call by Value
  • The parameter is a copy of the data value
    represented by the argument
  • Commonly used for "small" data values or values
    that must be protected from change
  • Since the original data is copied and the
    procedure accesses the copy only, the original
    data is protected

4
Call by Reference
  • The parameter is the address of the data value
    represented by the argument
  • The address is passed using call by value
  • Used when the original data is to be changed by
    the procedure, or it is inefficient to make a
    copy of the argument due to size
  • Since the procedure receives the address of the
    argument it cannot be protected against change

5
Register Parameters
  • One or more registers may be designated as
    parameter locations
  • Caller places arguments (values or addresses) in
    designated registers and then calls the procedure
  • When the procedure starts, the parameters are
    found in the designated registers
  • Values may be used directly
  • Base or indexed addressing is used to access
    reference parameters

6
Parameter Block
  • Copy arguments (values or addresses) into
    components of a prearranged record structure
    devoted to communicating with the procedure
  • Call the procedure
  • The address of the parameter block may be passed
    to the procedure, or it may access it by name
  • The procedure accesses parameters in the
    prearranged storage locations

7
Inline Parameters
  • Arguments (values or addresses) are placed in
    storage immediately after the call statement
  • Call the procedure
  • Procedure must alter return address or a JMP
    instruction is used to skip the args
  • Procedure expects its return address to be on the
    top of stack
  • This is used to locate the parameters
  • The return address may be modified to point past
    the parameter area

8
Stack Frame
  • Arguments (values or addresses) are placed on the
    stack
  • Call the procedure
  • Arguments may need to be removed from the stack
    if the procedure does not do this
  • A standard protocol is followed to access the
    parameters using based addressing
  • The parameters may be removed by specifying the
    size of the parameter block in the RET statement

9
Return Values
  • If a procedure is to return a value, it is
    customary to place it in a register
  • AX is most common, but any register may be used
  • Return values are limited to byte or word data
  • Flags are sometimes used for Boolean return
    values (especially the Carry flag)
  • Use call by Reference for more complex data

10
Saving Registers
  • It is usually the procedure's responsibility to
    save and restore any registers it needs to use
  • Registers used for return values are of course
    exempt from this convention
  • The 286 and higher processor has a PUSHA and POPA
    to put all registers on the stack and restore
    them
  • This is local storage for the procedure

11
Conventional Stack Frames
  • Procedures can use the stack
  • for parameters
  • saved registers
  • local storage
  • This allows recursion, reduces dependency on a
    data segment, and standardizes procedure
    interfaces

12
Standard Entrance/Exit Code
  • Save current BP on stack
  • Record old BP's location in BP
  • Allocate local storage
  • push BP
  • mov BP,SP
  • sub SP,ls_size
  • Deallocate local storage by setting SP to old BP
    location
  • Restore old BP
  • Return, removing parameter block
  • mov SP,BP
  • pop BP
  • ret pb_size

13
A Typical Stack Frame
BP-2
BP4
BP-3
BP6
BP-5
BP8
LW
LW
LB
BP
RA
P1
P2
P3
Parameters
Locals
SP
BP
14
Stack Frame Access
  • sample equates
  • arry_ptr equ word ptr BP4
  • loc_char equ byte ptr BP-3
  • loc_BX equ word ptr BP-5
  • sample access
  • mov loc_char,'A' initialize
  • mov loc_BX,bx save BX
  • mov BX,arry_ptr addr of array
Write a Comment
User Comments (0)
About PowerShow.com