• Aucun résultat trouvé

STANDARD LINKAGE REGISTERS

Dans le document A Programmer's Introduction to IBM System /360 (Page 119-123)

So far we have seen a typical subroutine linkage in action, with a variation that allows a choice between two return points. Communication of data and results between the main program and the subroutine was made easily because the programmer knew which registers were used for what purpose in both. Supposing they had been written by different programmers?

To ease the problem of assuring proper communications between program segments, which often are written by different programmers, standard register assignments and techniques have been defmed in each of the IBM operating or programming support systems. (They are similar, but not identical, in all System/360 operating systems.) Standard register assignments in the Disk Operating System (DOS) are shown in Table 8-1. The items in this chart will be explained in the following pages of text, as their use is discussed. These registers are used for the purposes shown, both by programmers and by the DOS macros. The DOS macros for subroutine linking are CALL, SAVE, and RETURN.

Table 8-1. DOS Linkage Registers Register Register

Number Function Contents

0 Parameter register Parameters to be passed to the su brou tine.

1 Parameter register Parameters to be passed to the or su brou tine.

Parameter list Address of a parameter list to be register passed to either the control pnr

gram or a user's subroutine.

13 Save area Address of the register save area register to be used by the subroutine.

14 Return register Address of the location in the main program to which control should be returned after execu-tion of the subroutine.

15 Entry point Address of the entry point in the register subroutine.

Let us examine the standard linkage registers. We have been using register 14 for the return register and register 15 for the entry point register. Data may be passed to a sub-routine using registers 0 and 1. However, a more common practice is to use register 1 to hold the address of a parameter list, because we usually have more data than will fit in two registers. (The expression "parameter list" merely signifies a list of numbers of any desired value.) The parameter list may consist of either data or the addresses of data.

Addresses are used more often so that data of varying lengths can be handled easily. One common technique is to

stream immediately following the BALR. This is a point from which the subroutine can readily obtain them.

FigUre 8-5 illustrates the use of most of these techniques and includes a subroutine that averages a seri~s of numbers.

The main program stores two series of numbers as lists in consecutive fullword locations. The average of each list is to be calculated and stored, so the subroutine will be used twice and each time will return to different points in the main program. To simplify the averaging routine, each list begins with the total number of entries in the list. Each list is identified by its starting address. This address is to be communicated to the subroutine, along with the address at which the subroutine is to store the average.

There are several possible ways to give the necessary information to the subroutine. We chose one that is repre-sentative, using BALR and A-type address constants:

BALR DC DC

1,15 A(LISTl) A (AVER 1)

Link to subroutine

Address of first parameter list Address of second parameter list The address of the first word of the list and the address at which the average should be stored will immediately follow the BALR that branches to the subroutine. The subroutine will be required to pick up the information it needs from this parameter list. It can find it because it will have the address of the first word after the BALR in register 1, loaded there by the BALR. Of course, prior to this we had to load the address of our subroutine entry point into register 15. This was done by use of the Load Address (LA) instruction. In addition the return address was loaded into register 14. The return address must be carefully calculated to assure that the proper return point is stored. This address is the current value of the assembler's location counter at the start of this instruction· plus the length of the LA (4 bytes) plus the length of the BALR (2 bytes) plus the length of the two address constants (8 bytes). This could also have been accomplished by labeling the return point and using that in a Load Address instruction. In any case, after we branch to the subroutine, register 1 contains the address of the fIrst DC in the parameter list.

What about register 13, and what is a "save area"?

Usually the subroutine will need to use the same registers that are used in the main program, but for different purposes. The main program may use the registers for base addresses, index addresses, intermediate results, or other data vital to the main program. To keep this data from being destroyed by the subroutine it has become conven-tional to store the contents of these registers in an area called a save area and defined by the main program. This area is 18 words in length in most System/360 systems and its address is stored in register 13 prior to branching to the subroutine. It is aligned on a doubleword boundary. In a

LOC OBJECT CODE AooR 1 ACDR2 STMT SOURCE STA TEMENT

1 PRINT NOGEN

000000 2 L1NK3 SlART 0

OCOOOO 05BO 3 BEGIN BALR 11.0

00on02 4 USING * ,11

000002 4100 B086 C0088 5 LA 13.SAVEAREA ADDRESS OF SAVEAREA

000006 6 CNOP 2.4 CONDITIONAL No-oP FOR ALIGNMENT

000006 4lFO BOCE 00000 7 LA lS.AVER BRANCH ADDRESS

OOOOOA 41EO B016 CC018 8 LA 14.*+14 RETURN ADDRESS

OOOOOE 05lF 9 BALR 1,15 LINK TO SUBROUTINE

000010 0OOOO04C 10 DC A(LISTlI ADDRESS OF FIRST PARAMETER LI ST

000014 0000007C 11 DC A(AIIERll ACCRESS OF RESULT

000018 5860 B03E C0040 12 L 6.A eTHER PROCESSING

OOOOlC 51\60 B042 00044 13 A 6.B X

000020 5060 B046 00048 14 ST 6,C X

000024 41CO B086 C0088 15 LA 13.SAVEAREA ADDRESS OF SAVE AREA

OC0028 0700 16 CNOP 2,4 CONDITICNAL ND-OP FOR ALIGNMENT

00002A 41FO BOCE 00000 11 LA 15,AVER

0OO02E 41EO B03A 0003C 18 LA 14,*+14 RETURN ADDRESS

000032 05lF 19 BALR 1,15 LINK TO SUBROUTINE

000034 00000060 20 DC AILlST2) ADDRESS OF SECOND PARAMETER LIST

000038 00000080 21 DC AIAVER2) ADDRESS OF RESULT

22 EOJ PROGRAM TERMINATION

00003E 0000

000040 00000038 25 A DC F'56'

000044 00000040 26 B DC F'77'

000048 27 C OS F

00004C 00000004 28 L1STl DC F'4' NUMBER OF ENTRIES IN LIST 1

000050 OOOOOOOA 29 DC F'lO'

000054 OOOOOOOC 30 DC F'12'

000058 00000013 31 DC F'19'

00005C OOOOOOOF 32 DC F' 15'

000060 00000006 33 LI ST2 DC F'6' NUMBER OF ENTRIES IN LIST 2

000064 OOOOOOOB 34 DC F'11'

000068 00000002 35 DC F'2'

00006C 00000004 36 DC F' 4'

000070 FFFFFFFD 37 DC F'-3'

000074 00000005 38 DC F' 5'

000078 FFFFFFFF 39 DC F ' - I '

00007C 40 AVER1 OS F

000080 41 AVER2 OS F

000088 42 SAVEAREA OS 90

43 *

44 * THE EI'.D OF THE MAIN PRCGRAM 45 *

46 AVER SAVE ( 14,12) SAVE REGISTERS

0000C4 0590 49 BALR 9,0

000006 50 USING *.9

000006 5851 0000 ooeoo 51 L 5.0(1) STARTING ADDRESS

OOOOOA 4160 0004 00e04 52 LA 6.4 INCREMENT

OCOODE 5845 0000 cocoa 53 L 4,0(5) NUMBER OF ENTRIES

OUOOE2 1874 54 LR 7.4 NUMBER CF ENTRIES

OCOOE4 8H70 oe02 COO02 55 5LA 7.2 FOUR TIMES NUMBER OF ENTRIES

UOOOE8 11\15 56 AR 1.5 LIMIT

OOOOEA 5BI0 903A 00110 57 S 7.=F'1' REDUCE BY 1 SO LOOP WILL NOT REPEAT

OOOOH 1822 58 SR 2.2 CLEAR TO ZERO

OOOOFO lBB 59 SR 3,3 CLEAR TO ZERO

OOOOF2 5A35 0004 00e04 60 LOOP A 3,4(5) ADD A VALUE FROM THE LIST

OOOOF6 8756 901C COOF2 61 BXLE S.6,LCOP

OOOOFA 1024 62 OR 2.4 DIVIDE BY NUMBER OF TERMS

OOOOFe 5851 0004 00004 63 L 5,4111 PICK UP ADDRESS OF RESULT

oeOl00 5035 0000 OCCOO 64 5T 3.0(5) STORE RESULT

65 RETURN (14.12) RETURN TO THE MAIN PROGRAM

000000 69 END BEGIN

000110 00000001 70 =F'l'

Figure 8-5. A program with a subroutine that averages a series of numbers. The subroutine will be used twice and will store the results at AVERI and AVER2.

subroutines. Word 2 is the save area address of a preceding routine. Word 3 is the save area address of a succeeding routine. Words 4-18 are the contents of registers 14-12, respectively-that is, all the general registers except register 13.

Note that early in the main program after the usual preliminaries, we load register 13 with the address of the save area. Then follows the CNOP (which we will discuss later on), and the loading of the entry point and return addresses. In the subroutine we begin by saving the contents

of the registers in the save area. This is accomplished through the use of the SAVE macro in DOS, or by regular assembler language Store instructions in systems where this macro is not available. Register 14 is specified as the first register to be stored, and all additional registers are stored simply by specifying the last register to be stored. Thus the registers are stored in the order 14, 15, then 0 through 12. The instruction generated by the SAVE macro is STM 14, 12, 12( 13). Therefore the registers are stored in the save area (its address is designated by the contents of register 13)

starting at a point that is twelve bytes past the beginning of the area (we recall that the fIrst three words of the save area are reserved for other data). Now the subroutine can use the registers for its own purposes and, when its processing is fmished, can restore the registers to their status when the subroutine was entered. This entire procedure is normal practice; it can almost never be assumed that any registers are available to the subroutine unless their contents are first saved.

Let's proceed with the work performed by the sub-routine. Statement 51 gets the address of LIST 1 by a Load instruction in which the effective address is simply the con-tents of register 1. After execution of this LOAD, the address in register 1 (OOOO4C) is placed in register 5 for subsequent use. Stepping through the list will be done with a Branch on Index Low or Equal instruction (BXLE), so we proceed to set up the other parameters required. Register 6 is accordingly loaded with a 4, the increment between loca-tions that must be added on each repetition of the loop.

With register 6 containing the increment, register 7 must contain the fInal address, that is, the starting address plus four times the number of entries.

We load register 4 with the number of entries, which is the first fullword in each list. It is to be left in register 4 for computing the average later. For purposes of controlling the loop, we move it to register 7, shift left two places (in intro-ducing data into a program in a machine instruction, and is (in that one sense only) like immediate data in an SI instruction. It is simply a constant preceded by an equal sign.

It represents data rather than a reference to data. It can be used to enter a number for calculation, an address constant, or words or phrases for printing out a message. Unlike immediate data, a literal term is not assembled into the instruction in which it appears. The assembler generates the values of all literals in a program, collects them, and stores contents of register 5, which is the index register for the loop.

The address in register 5 is adjusted by 4 so that we will address the fIrst number to be averaged rather than the number of entries. Between loop repetitions, register 5 is incremented by the contents of register 6, which we set at 4.

Each time register 5 is lower than, or equal to, register 7 on comparison, we will branch back to LOOP. The looping stops when all entries in the list have been added to register 3.

To compute the average, which is simply a matter of dividing the contents of registers 2 and 3 (the sum of all the numbers in the list) by the contents of register 4 (the number of entries in the list), a Divide Register instruction (DR) is been saved and branch back to the main routine.

The RETURN macro is coded identically to the SAVE macro, that is, the operands are the starting and ending registers to be restored, RETURN 14,12. It restores the saved data to the specified registers and returns to the main program via the return address in register 14. The coding generated by the RETURN macro is a Load Multiple (LM) instruction and an unconditional branch.

Back in the main routine, we do some simple processing using register 6. We can use the same registers as we used in the subroutine because the SAVE and RETURN macros allow each section of programming to view the registers as their own.

We then wish to average another list, LIST2. At statement 1 5, note that we must again place the address of the save area in register 13 because we must enter the subroutine again. The execution of the subroutine follows the same lines as before, although this time it operates on different data and places the result in a different place.

The CNOP assembler instruction, which we have not dis-cussed so far, appears twice in the main program during the preparations for each branch to the subroutine. The func-tion of the CNOP (Condifunc-tional No..Qperafunc-tion) is to make sure that the two address constants appear in storage immediately after the BALR.

If the instruction before the BALR ended on a fullword boundary, the BALR (a two-byte instruction) would then occupy the first two bytes of the next word. The assembler, automatically aligning on a fullword boundary an A-type constant for which no length is specifIed, would skip two bytes before locating the constant. Then, when the BALR is executed, register 1 would contain the address of the byte following the BALR instruction, but this address would not be correct for the parameter list.

Before reaching the BALR we write the instruction CNOP 2,4. If the assembler's location counter is already set to a value that is two bytes greater than a fullword boundary, the CNOP is ignored, as is the case in statement 6. If not, as is the case in statement 16, the assembler inserts a Branch on Condition (BC) instruction with a mask of zero, which never causes a branch and therefore is equivalent to a "No-Operation". It occupies two bytes and thus causes the

para-meter list to be located immediately following the BALR.

The CNOP in statement 6 has no effect; we see that the location counter is already located as described by the CNOP (that is, it is in the second half of a fullword-000006). Therefore, no instruction is generated for the CNOP. Note that the DC will be on a fullword boundary without skipping any space between the BALR and the address constant.

The CNOP'in statement 16 resulted in the creation of a No-Operation instruction because the assembler's location counter is at 000028, which is not two bytes beyond a fullword. If this had not been done, the assembler would have placed the BALR at 000030 and the A-type constant at 000034, thereby leaving a two-byte gap. The next byte after the BALR (000032) would go into register 1. The subroutine's ensuing attempt to call for a fullword from

000032 would have caused a specification exception and a program interruption.

The CNOP could be used before or after the two LA instructions that load the return and entry point addresses into registers 14 and 15. We have used the CNOP ahead of the two LA instructions in this program.

Figure 8-6 shows the two lists of num bers followed by the average of each, as computed by the program in Figure 8-5.

10 12 19 15 14

11 2 4 -3 5 -1 3

Figure 8-6. The data and results of the program in Figure 8-6. The last number in each line is the average.

Dans le document A Programmer's Introduction to IBM System /360 (Page 119-123)