CIS 020 Assembly Programming Chapter 18 - Sub-routines and Sub-program Linkage 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Objectives Understand the Types of Subroutines Describe how to create and invoke Subroutines Describe how communications with Subroutines work Describe the differences and similarities between Subroutines and Subprograms Demonstrate understanding of Subprogram Linkage and Communications 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Subroutines Generally a block of instructions that perform a specific function needed from several points within a program. Provides storage efficiency by creating the specific function once and “Calling” that function from wherever it is needed. Subroutines are identified by a label at their entry point and generally terminate with a BR instruction 5/27/2012 © John Urrutia 2012, All Rights Reserved.
All needed instructions Subroutines Entry Point Designated Flowchart symbol Subroutine CALL Subroutine All needed instructions Return to CALL 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Two Types of Subroutines Internal – Written specifically for this program for use anywhere in this program. Relies on the supporting environment provided by the main program. Generally, must be within the address range of the current base register. 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Two Types of Subroutines External – Written as an independent stand-alone set of reusable code that can be added anywhere into a program. Generally stored in a system library and copied into your program via a copybook or MACRO. 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Subroutine LINKAGE To utilize subroutines You must address the subroutine entry point You must address the next instruction to execute after the subroutine completes. Linkage between the main routine and subroutines Use special branch instructions – BAL & BALR Use the branch register to return to caller– BR 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Subroutine LINKAGE BAL – Branch and Link Stores the Address of the instruction following the BAL in the Register of Operand 1 Then Branches to the Address of Operand 2 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Subroutine LINKAGE BAL – Branch and Link Use caution – inadvertently changing the contents of the register in Operand 1 will have unpredictable results!!! What do I do if I need to use this register?? Upon entry to the subroutine – Store the Register using the ST instruction Create a 4 Byte storage area for this purpose. 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Subroutine LINKAGE BALR – Branch and Link Register This instruction is primarily used for calling external subroutines / subprograms. Stores the Address of the instruction following the BALR in the Register of Operand 1 Then Branches to the Address of Operand 2 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Subroutine LINKAGE Program Housekeeping Keeping track of where we are and where we’re going Standard Register Linkage Conventions R1 – Address of the list of data addresses R13 – Address of Calling Program’s Register Save Area R14 – Address of Calling Program’s Next Instructions R15 – On Entry – Called Program’s Entry Point Address On Exit – Called Program’s Return Code 5/27/2012 © John Urrutia 2012, All Rights Reserved.
5/27/2012 © John Urrutia 2012, All Rights Reserved.
RR – Register to Register RR – are the fastest instructions. 2 Byte instruction Length with the coding format Translates to B'0001 1100 1010 1000' X' 1 C 6 8' 5/27/2012 © John Urrutia 2012, All Rights Reserved.
RS – Register to Storage RS – Transfer data between a register and storage. 4 Byte instruction Length with the coding format Translates to B'1001 0000 1110 1100 1101 0000 0000 1100' X' 9 0 E C D 0 0 C' 5/27/2012 © John Urrutia 2012, All Rights Reserved.
RX – Register to Indexed Storage RX – Transfer control by mask to storage address . 4 Byte instruction Length with the coding format Translates to B'0100 0111 1111 0101 0110 0011 1111 1000' X' 4 7 F 5 6 3 E 8' 5/27/2012 © John Urrutia 2012, All Rights Reserved.
RX – Register to Indexed Storage RX – Transfer data between a register and storage. 4 Byte instruction Length with the coding format Translates to B'0100 0000 0100 0101 0110 0011 1111 1000' X' 4 0 4 5 6 3 E 8' 5/27/2012 © John Urrutia 2012, All Rights Reserved.
SI – Storage Immediate SI –Data imbedded in instruction to storage. 4 Byte instruction Length with the coding format Translates to B‘1001 0101 0110 1111 0110 0011 1111 1000' X' 9 5 6 F 6 3 E 8' 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Storage to Storage Instructions Least efficient instructions SS Formats 5/27/2012 © John Urrutia 2012, All Rights Reserved.
SS – Storage to Storage SS – Storage to Storage, Character Format 6 Byte instruction Length with the coding format Translates to B‘1101 0010 1111 1111 0101 0000 0000 1100 0110 0000 0000 0000' X' D 2 F F 5 0 0 C 6 0 0 0' 5/27/2012 © John Urrutia 2012, All Rights Reserved.
SS – Storage to Storage SS – Storage to Storage, Packed Decimal Format 6 Byte instruction Length with the coding format Translates to B‘1111 0010 0011 0110 0101 0000 0000 1100 0110 0000 0000 1000' X' F 2 3 6 5 0 0 C 6 0 0 8' 5/27/2012 © John Urrutia 2012, All Rights Reserved.
SS – Storage to Storage SS – Storage to Storage, Shift Packed Decimal Format 6 Byte instruction Length with the coding format Translates to B‘1111 0010 0011 0100 0101 0000 0000 1100 0000 0000 0000 0011' X' F 0 3 4 5 0 0 C 6 0 0 3' 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Program Interrupts & the PSW Program interrupts occur when you tell the system to do something it can’t. Old School – System simply stops working Today – System is built with redundancy and continues to work while suspending your program execution When an interrupt occurs, generally your program is terminated with an appropriate system code. There are 5 types of interrupts 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Program Interrupts & the PSW There are 5 types of interrupts External – caused by an action outside of the system. I/O problem – external or internal associated with I/O subsystem Program – caused by a faulty instruction or address Machine – hardware, faulty circuit or equipment Supervisor – caused by an error detected by the Supervisor Program. 5/27/2012 © John Urrutia 2012, All Rights Reserved.
Program Interrupts & the PSW The PSW – Program Status Word Identifies the state of the system at the time the interrupt occurred. z390 PSW Consists of the following information PSW=07050600 800FFFDA D2030000D088 MVC ABEND S0C5 5/27/2012 © John Urrutia 2012, All Rights Reserved.