Subroutines - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Subroutines

Description:

the key to solve a problem is to break it down in smaller pieces (top-down ... to recover from a stack overflow is to start all over again by resetting the CPU. ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 10
Provided by: UH73
Learn more at: https://www.ewu.edu
Category:

less

Transcript and Presenter's Notes

Title: Subroutines


1
Subroutines
  • Advanced Programming


2
Top-Down approach to problem solving
  • Algorithmstep by step description of how to
    solve a problem
  • Divide and Conquer paradigmthe key to solve a
    problem is to break it down in smaller pieces
    (top-down approach)
  • Structured Programmingprocess of breaking down a
    program in smaller modules

3
Modular programming using subroutines
  • Modular programming is achieved by using
    subroutines
  • A well-documented subroutine can be used without
    knowing its source code
  • Describe what the subroutine does
  • Use meaningful names and comments
  • Describe the parameters passed
  • Describe error conditions
  • Stack space used

4
Subroutine operation
  • Typically, a subroutine operation requires
    transferring and processing data
  • The information-passing process occurring during
    the operation of a subroutine is called parameter
    passing
  • The simplest strategy to hold a subroutine
    input/output data (parameters) is using the CPU
    registers.
  • What if there are not enough registers for
    holding the subroutines input/output data?

5
Parameter passing techniques
  • Parameter passing alternatives
  • - Use of CPU registers
  • - Use of local variables
  • - Use of the stack

6
Example of a subroutine using the stack (1)
  • Program that squares all the numbers in
    addresses C000 to C07F
  • ORG E000 start address
  • main LDS FF define the stack
  • LDX C000 init data block pointer
  • loop JSR square squaring loop
  • INX point next data
  • CPX C080 squared all data ?
  • BNE loop get more if not
  • here bra here stop program

7
Example of a subroutine using the stack (2)
  • Subroutine Square
  • Calculate the square of an 8 bit number
  • square PSHA preserve registers A and B
  • PSHB
  • The following instructions modify A and B
  • LDAA 0,X get data to square
  • TAB copy it to B
  • MUL square it
  • ADCA 00 round it to 8-bit result
  • STAA 0,X store result
  • PULB restore registers A and B
  • PULA
  • RTS return

8
Example of a subroutine using the stack (3)
ORG E000 main LDS FF LDX
C000 loop JSR square INX
CPX C080 BNE loop here bra
here square PSHA PSHB
LDAA 0,X TAB
MUL ADCA 00 STAA
0,X PULB PULA
RTS
9
Stack overflow
  • It happens if the stack exceeds the available
    memory.
  • If the stack continue to grow, the SP will
    eventually decrement past 0000 to wrap around to
    FFFF. At this point pushing data is of no use
    because this area of memory is read-only memory.
  • The only way to recover from a stack overflow is
    to start all over again by resetting the CPU.
Write a Comment
User Comments (0)
About PowerShow.com