lecture 19 control abstraction section 8 1 8 2 l.
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 19: Control Abstraction (Section 8.1-8.2) PowerPoint Presentation
Download Presentation
Lecture 19: Control Abstraction (Section 8.1-8.2)

Loading in 2 Seconds...

play fullscreen
1 / 17

Lecture 19: Control Abstraction (Section 8.1-8.2) - PowerPoint PPT Presentation

  • Uploaded on

Lecture 19: Control Abstraction (Section 8.1-8.2). CSCI 431 Programming Languages Fall 2002. A compilation of material developed by Felix Hernandez-Campos and Michael Scott. Abstraction.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Lecture 19: Control Abstraction (Section 8.1-8.2)' - anne

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lecture 19 control abstraction section 8 1 8 2

Lecture 19: Control Abstraction(Section 8.1-8.2)

CSCI 431 Programming Languages

Fall 2002

A compilation of material developed by Felix Hernandez-Campos and Michael Scott

  • Programming languages support the binding of names with potentially complex program fragments that can be used through an interface
    • Programmers only need to know about the purpose of the fragment rather than its implementation  Abstraction
  • A control abstraction performs a well-defined operation
    • Subroutines
  • A data abstraction represents information
    • Data structures
    • Most data structures include some number of control abstractions
  • Execute an operation on behalf of a calling program unit
  • Subroutines can be parameterized
    • The parameters in the definition of the function are known as formal parameters
    • The parameters passed in the subroutine call are known as actual parameters or arguments
    • At the time of the call, actual parameters are mapped to formal parameters
  • Functions are subroutines that return a value, while procedures are subroutines that do not return a value
subroutine frames

Actual Parameters

Formal Parameters

Subroutine Frames
  • Each subroutines requires a subroutine frame (a.k.a activation record) to keep track of
    • Arguments and return values
    • Local variables and temporaries
    • Bookkeeping information
  • When a subroutine returns, its frame is removed

1001: A(3)

2001: int A(int n) {

int m = n * n;

return m + A(n-1);


c example
C Example
  • Stack pointer sp
    • Top of the frame stack
  • Frame pointer fp
    • Access to arguments and locals via offset of fp
  • They differ if temporary space is allocated in the stack
c example7
C Example
  • Stack pointer sp
    • Top of the frame stack
  • Frame pointer fp
    • Access to arguments and locals via offset of fp
  • They differ if temporary space is allocated in the stack
stack maintenance
Stack Maintenance
  • Calling sequence (by caller) and prologue (by callee) are executed in the way into the subroutine:
    • Pass parameters
    • Save return address
    • Change program counter
    • Change stack pointer to allocate stack space
    • Save registers (including frame pointer)
    • Change frame pointer to new frame
    • Initialization code
  • Separation of tasks?
    • As much as possible in the callee (only once in the program)
stack maintenance9
Stack Maintenance
  • Epilogue (by callee) is executed in the way out of the subroutine:
    • Pass return value
    • Execute finalization code
    • Deallocate stack frame (restore stack pointer)
    • Restore registers
c example10
C Example

Calling sequence by the caller:

  • Caller saves registers in local variables and temporaries space
  • 4 scalar arguments in registers
  • Rest of the arguments at the top of the current frame
  • Return address in register ra and jump to target address
c example11
C Example

Prologue by the callee:

  • Subtract the frame size from the sp
  • Save registers at the beginning of the new frame using sp as the base for displacement
  • Copy the sp into the fp
c example12
C Example

Callee epilogue:

  • Set the function return value
  • Copy fp to sp to deallocate any dynamically allocated space
  • Restore registers including ra (based on sp)
  • Add frame size to sp
  • Return (jump to ra)
parameter passing
Parameter Passing
  • Pass-by-value
    • Input parameter
  • Pass-by-result
    • Output parameter
  • Pass-by-value-result
    • Input/output parameter
  • Pass-by-reference
    • Input/Output parameter, no copy
  • Pass-by-name
    • Textual substitution
  • Closure are subroutines that maintain all their referencing environment
    • This mechanism is also known as deep binding
  • This is significant when subroutines are passed as arguments
exception handling
Exception Handling
  • An exception is an unexpected or unusual condition that arises during program execution
    • Raised by the program or detected by the language implementation
  • Example: read a value after EOF reached
  • Alternatives:
    • Invent the value (e.g. –1)
    • Always return the value and a status code (must be checked every time)
    • Pass a closure (if available) to handle errors
exception handling17
Exception Handling
  • Exception move error-checking out of the normal flow of the program
    • No special values to be returned
    • No error checking after each call
  • Exceptions in Java
    • http://java.sun.com/docs/books/tutorial/essential/exceptions/