


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















We now begin discussion of subroutine linkages for simple subroutines without large argument blocks. With the mechanisms discussed in this lecture, one can use either global variables or general purpose registers to pass addresses, values, and results. As the textbook notes, subroutines may be written to facilitate repetitive tasks. Indeed, the âWheeler Jumpâ (EDSAC, 1952) was developed for just this purpose. Early operating systems began as collection of subroutines to facilitate handling of input and output devices, mainly line printers. When batch programming became common, the computer needed a control program to automate the processing of a sequence of jobs, read and processed one after another. This control program was merged with a set of subroutines for Input/Output handling and routines for standard mathematical functions to create the first operating system. Subroutines and/or functions can be invoked for side effect only or in order to compute and return results. Many of the subroutines used in the book are called for side effect â to manage print position on a page and issue a new page command when necessary.
The language requires two instructions associated with subroutines: one to call the subroutine and one to effect a return from the subroutine. Calling the subroutine The instruction used is either BAL (Branch and Link) or BALR. The format of the instruction is BAL Reg,Address An example of such an instruction is BAL 8,P10PAGE The first argument is a register number to be used for subroutine linkage. We explain this more fully in just a bit. The second argument is the address of the first instruction in the subroutine. There are other standards for this argument, but this is the one that IBM uses. Returning from the subroutine This instruction is used to return from execution of the subroutine. It is an unconditional jump to an address contained in the register. The format of the instruction is BR Reg An example of such an instruction is BR 8
The first thing to note is that the register used in the BR will, under almost all circumstances, be that used in the BAL instruction calling the subroutine. A bit of reflection will show that this mechanism, by itself, will not allow the use of recursive subroutines. Actually, there is a workaround, but it involves the use of a separate register for each level of subroutine nesting. Here is an example of subroutine usage in the standard of this discussion. **OPEN (FILEIN,(INPUT)) OPEN (PRINTER,(OUTPUT)) PUT PRINTER,PRHEAD GET FILEIN,RECORDIN
A10LOOP BAL 8,B10DOIT GET FILEIN,RECORDIN B A10LOOP A90END CLOSE FILEIN CLOSE PRINTER EXIT
B10DOIT MVC DATAPR,RECORDIN PUT PRINTER,PRINT BR 8**
The assembly language programmer will generally speak of âsubprogramsâ. The concepts of subroutines and functions are generally associated with higher level languages, though they are really just programming conventions placed on the use of the assembly language. Given a programming language, such as the IBM Assembler, that provides support for subprogram linkage, all that is required to support functions is the designation of one or more generalâpurpose registers to return values from the function. In CDCâ7600 FORTRAN, the two designated registers were X6 and X7. The usage: Singleâprecision results Register X7 would return the value. Doubleâprecision results Register X7 would return the lowâorder 32 bits Register X6 would return the highâorder 32 bits. As a result, any subroutine could be called as a function. What one got as a result would be the value that the subroutine last placed in X7 or both X6 and X7. While this was chancy, it was predictable.
There are two possibilities if your code contains a reference that cannot be found within the local CSECT:
Two of the more common methods for passing data between CSECTs are call by reference and call by value. Note that there are several other common methods. In call by value , the calling program delivers the actual values of the data to the subroutine. This can alter the values of those data, but the altered values will not be returned to the calling routine. Some languages call these âin parametersâ. In call by reference , the calling program delivers the addresses of the data items to the subroutine. The values of these data can be changed by the called subroutine and those values propagated back to the calling program. Note that the return address for the subroutine can also be changed. This is an old hacker trick. The next question we must ask is how these values and/or addresses will be passed to the subroutine.
The first example of a subroutine was the B10DOIT routine a few slides back. B10DOIT MVC DATAPR,RECORDIN PUT PRINTER,PRINT BR 8 While this is a valid subroutine, we note two facts of importance.
One key design feature when developing subroutines and functions is the minimization of side effects. One particular requirement concerns the general purpose registers. This design requirement is that after the return from a called subprogram, the contents of the general purpose registers be exactly the same as before the call. The only exception to this rule is the use of a general purpose register for the return of a function value. In this case, each of the calling routine and the called routine explicitly allows for the value of that one specified register to be changed. Save and Restore The strategy used is that each routine (including the main program) will save the contents of all but one of the general purpose registers on entry and restore those on exit. This has evolved from an earlier strategy in which a developer would save the contents of only those registers to be used explicitly in the subprogram. In the statically allocated, nonârecursive world of IBM assembler programs, each routine will declare an 18âfullword âsave areaâ , used to save important data.
The following is the Standard Linkage Convention for the use of generalâpurpose registers. Register Use 1 Address of the parameter list 13 Address of calling routineâs save area 14 Address in the calling routine to which control is to be returned. 15 The address of the entry point in the called subprogram. We have already seen an example of the use of registers 14 and 15. Consider the following code. L 15,=V(PROGB) LOAD ADDRESS OF EXTERNAL REFERENCE BALR 14,15 STORE RETURN ADDRESS INTO R
Often the first executable instruction of a program will be SAVE (14,12). Here is the actual code from the second lab assignment, in which I chose to expand macros. *31 LAB02 CSECT 32 SAVE (14,12) SAVE CALLERâS REGISTERS 34+ DS 0H 35+ STM 14,12,12(13) 36 BALR R12,0 ESTABLISH ADDRESSABILITY 37 USING ,R 38 LA R2,SAVEAREA LINK THE SAVE AREAS 39 ST R2,8(,R13) 40 ST R13,SAVEAREA+ 41 LR R13,R On entry, generalâpurpose register 13 contains the address of the supervisorâs save area. The SAVE macro is written under this assumption. The program executes as a subprogram of the supervisor (Operating System). Recall that the address designated 12(13) corresponds to an offset of 12 from the value stored in register 13. If R13 contains address S0, then 12(13) corresponds to address (S0 + 12).
The next step is to establish addressability in the user program, so that addresses such as that of the save area can be used. The user program then forward links the supervisorâs save area. Before that happens, we just have two save areas. Here is the code to do the forward link. The address of the start of the user save area is placed in the fullword at offset 8 from the start of the supervisorâs save area. 38 LA R2,SA LINK THE SAVE AREAS 39 ST R2,8(,R13) OFFSET 8 FROM S
The program now executes the following code to establish the backward link from the user program to the supervisorâs save area. 40 ST R13,SA+ At this point, register R13 contains the address of the supervisorâs save area. The result of this instruction is the completion of the twoâway links. Now R2 holds the address of the user save area. Note that this could have been any of the registers in the range 2 â 12, excepting 12 which was used for addressability. The next instruction established R13 as once again holding the address of the save area of the current program. 41 LR R13,R
The return process restores the generalâpurpose registers to their values before the call of the subroutine. R13 is first restored to point to the save area for the calling program. Then the other generalâpurpose registers are restored. Here is the situation on return from Subprogram PB. The code is as follows. L R13,SB+4 GET ADDRESS OF CALLERâS SAVE AREA LM R14,R12,12(R13) RELOAD THE REGISTERS. R14 IS LOADED WITH THE RETURN ADDRESS BR R14 RETURN TO THE CALLER
Consider how a program or subprogram starts and exits. The start code is something like the following. **SUB02 CSECT SAVE (14,12) SAVE CALLERâS REGISTERS