Docsity

Subroutine Linkage: Instructions - Assembly Lang Programming 1 | CPSC 3121, Study notes of Programming Languages

Toggle sidebar
bg1
Subroutine Linkage
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Subroutine Linkage: Instructions - Assembly Lang Programming 1 | CPSC 3121 and more Study notes Programming Languages in PDF only on Docsity!

Subroutine Linkage

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.

Subroutine Linkage: Instructions

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

Comments on the Subroutine Linkage

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**

Functions and Subroutines

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.

Declaring External References

There are two possibilities if your code contains a reference that cannot be found within the local CSECT:

  1. You forgot to declare the reference and have an error in your program.
  2. The reference is to a variable or address in another CSECT. In order to distinguish between the two cases, one must explicitly declare a reference as external if it is not declared within the local CSECT. The data type for such a reference is V (external defined address). Recall that all “variable” references are really address references that access the contents of the address. Presumably, one could write something like the following. PROGB DC V‘0000’ But this seems to be done rarely, if at all. Invocation of a subroutine found in a separate CSECT is achieved by use of the standard system macro, CALL. We give an example, and its expansion. **CALL PROGB
  • L 15,=V(PROGB) LOAD ADDRESS OF EXTERNAL REFERENCE
  • BALR 14,15 STORE RETURN ADDRESS INTO R**

Call By Reference vs. Call By Value

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.

Subroutines, Separate Assemblies and Linkage

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.

  1. It is in the same assembly as the calling module.
  2. It is in the same CSECT as the calling module. In general, we would like to link modules that are not assembled at the same time. It might even be necessary to link assembled code with code written in a higher–level language and compiled. Thus COBOL programs might call assembler language routines. We have already encountered the external declaration, which is used to declare that:
  3. A label corresponds to an address declared in another module, and
  4. The linking loader will have to resolve that address by loading the called module, assigning an address to this label, and updating that reference in the calling code. IBM has established a set of conventions for use by all modules when calling separately assembled modules. These insure that each module can work with any other module.

The Save Areas

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.

Register Usage in Subprogram Calling

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

The SAVE Macro

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 User Program Links the Save Areas

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 User Program Links the Save Areas (Part 2)

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

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

Entry/Exit Considerations

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

  • DS 0H
  • STM 14,12,12(13) BALR R12,0 LOAD R12 WITH THE ADDRESS USING ,R12 OF THE NEXT INSTRUCTION* Here is the standard exit code. Assume the save area is at address SB. L R13,SB+4 ADDRESS OF CALLERS SA LM R14,R12,12(R13) CALLER’S REGISTER VALUES BR R14 RETURN Note that the next to last instruction in the second section causes addressability in the called program to be lost. This is due to the fact that the value that had been loaded into R12 to serve as the base register for this code’s addressability has been overwritten.