Download presentation
Presentation is loading. Please wait.
Published byMegan Mathews Modified over 8 years ago
1
1 Subroutines Advanced Programming
2
2 Top-Down approach to problem solving Algorithm step by step description of how to solve a problem Divide and Conquer paradigm the key to solve a problem is to break it down in smaller pieces (top-down approach) Structured Programming process of breaking down a program in smaller modules
3
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
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 subroutine’s input/output data?
5
5 Parameter passing techniques Parameter passing alternatives: - Use of CPU registers - Use of local variables - Use of the stack
6
6 * 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 Example of a subroutine using the stack (1)
7
7 * 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 Example of a subroutine using the stack (2)
8
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
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.