Unit 7.6 The IBM Convention
SILVER 6.1
SCHAUM Chapter 10

PROG

See corresponding PASCAL code

PED

Illustrates IBM convention


CONCEPTS

IBM Convention

Advantages:

-Ensures that subroutine does not clobber registers

-Ensures compatability with IBM subroutines

-Allows backtrace to work for some debugging aids

Disadvantages:

-Takes more human time

-Takes more computer time

-Takes more of the computer's space

Rules:

-Register 13 - points to an eighteen word save area

-Save  area  for  subroutine's registers  is  maintained  by
caller

-On  entry  to  subroutine, register 13 will point  to  this
location

-Subroutine will save registers at that location

-During bulk of subroutine operation, register 13 points  to
save area of subroutine

-At offset of four into that save area is a pointer back  to
caller's save area

-Register 14 points to return address for subroutine and  is
restored by LM at end of subroutine

-Register 15 will point to the subroutine beign called.

The main program is a subroutine of the operating system and
is treated accordingly

I.E.,  we save the operating system's registers in the  save
area  it provides; register 13 points to it on entry to  our
main program

The template for calling a subroutine is thus,

          LA                    13,save1
          LA                    15,subname
     BALR 14,15

where

save1 is an 18-word save area allocated by the main program
subname  is  the beginning address of the subroutine  to  be
called.

The template for a subroutine obeying the IBM-convention is:

subname                         equ  *
          STM                   14,12,12(13)
          ST                    13,save2+4
     LA   13,save2
       .
       .   body of subroutine
       .
          L                     13,save2+4
          LM                    14,12,12(13)
          BR                    14
where
save2 is the 18-word save area allocated by the subroutine

subroutine call template

          LA                    13,savearea
          LA                    15,subname
          BALR                  14,15

savearea is 18 word save area allocated by the main routine.

SF

      BALR regA,regB

-puts  the  address of the instruction after the  BALR  into
"regA" and branches to address in "regB"


      STM regA,regB,memory-address

-Stores   registers  from  "regA"  to  "regB"  at   location
indicated by "memory-address"

-Will wrap around (reg15, reg0) if regA  regB

-"memory-address" only has an offset and a single register


     LM  regA,regB,memory-address
-restores "regA" to "regB" from location indicated by memory
address
-will wrap around (reg15, reg0) if regA  regB

NEW.html
                  
Unit 7.6, The IBM Convention

The IBM convention is a standard mechanism for ensuring that
registers  are  saved at the beginning of a  subroutine  and
restored  at  the end.  It also provides a standard  set  of
registers  in  which  to put the return  address  and  other
guides.   By return address, I mean the register in the  BAL
statement and used in the BR at the end of the subroutine.

(There  should be a discussion of this in the second chapter
of your Supervisor Services Guide.)

Remember that the IBM operating system will be using the IBM
convention in calling what we perceive as our main program.

Each  subroutine  including the IBM  operating  system  must
allocate  18  bytes for the registers and other information.
This can be done with a
name DS   18F

This is laid out as follows

name+0   unused
name+4   pointer to the save area of the calling subroutine
name+8   unused
name+12  register 14 of the calling subroutine
name+16  register 15 of the calling subroutine
name+20  register 0 of the calling subroutine
name+24  register 1 of the calling subroutine
name+28  register 2 of the calling subroutine
name+32  register 3 of the calling subroutine
    etc.

You can think of this as an 18 element array.  Each position
or box of the array has a rigid purpose when being used as a
save area.

Note that the caller is responsible for allocating the space
in  which  its  registers will be saved by  the  subroutines
which it calls.

The registers to be used are as follows.)

12    base register
13     address  of  save  area  maintained  by  the  calling
subroutine
14    address to which the subroutine is to return
15    address of beginning of subroutine

The order of operations, and a statement of whether the main
program  or  the subroutine are responsible for them  is  as
follows.

1.the  main  program loads register 13 with the  address  of
  the 18-word save area which it allocated
2.the  main  program loads the address of the  beginning  of
  the subroutine into register 15
3.the  main  program  calls  the  subroutine,  it  puts  the
  address  of the next instruction into register 14.   (This
  is where the subroutine is to return.)
4.the  subroutine  saves  all  the  registers  in  the  area
  provided by the main program
5.the   subroutine  puts  the  address  of  the  save   area
  allocated by the caller into second word of the save  area
  it allocated
6.it sets register 13 to point to its save area.

Note that in the body of a subroutine, register 13 points to
its  save area.  Using the item in the second word (at  +4),
there is a chain back to the root somewhere in the operating
system.

The  IBM  operating  system supports a  backtrace  facility.
Should  a  subroutine develop a run-time  error  (addressing
exception,  divide  by 0, etc.), the operating  system  will
start  at  register 13 and determine the  location  in  each
calling  routine.  For example, it will say, that the  error
occurred  at  such  and  such  location  in  subroutine   X.
Subroutine  X  was called from location such and  such  from
subroutine  Y.   Subroutine Y was called from location  such
and  such  from subroutine Z.  And subroutine Z  was  called
from such and such location from the main program, which was
in  turn invoked from the operating system.  (Since register
15  contains  the address of the subroutine,  the  operating
system  can determine and give the starting address  of  the
subroutine.)

There are two new instructions.  These are used to save  and
restore the registers from the 15 words for register-storage
in the 18 word save area.

The  first  is  STM  (store multiple) which  stores  several
registers into a given memory location. The second, LM, does
the inverse of STM.  That is, it loads up the registers with
the values put in memory by a STM instruction.

The operands to STM are
          STM  regA,regB,memloc1

If regA < regB, then STM will put
register regA into address memloc1
register regA+1 into address memloc1+4
register 15 into address memloc1+(15-regA)*4
register 0 into address memloc1+(15-regA+1)*4
register regB into address memloc1+(15-regA+regB)*4

In other words, the registers will "wrap around."

The template for calling a subroutine is thus,

               LA              13,save1
               LA              15,subname
          BALR 14,15

where

save1 is an 18-word save area allocated by the main program
subname  is  the beginning address of the subroutine  to  be
called.

The template for a subroutine obeying the IBM-convention is:

subname        equ             *
               STM             14,12,12(13)
               ST              13,save2+4
          LA   13,save2
            .
            .   body of subroutine
            .
               L               13,save2+4
               LM              14,12,12(13)
               BR              14
where
save2 is the 18-word save area allocated by the subroutine

From  the description of the "STM" instruction, we see  that
the command, STM 14,12,12(13) does the following:

saves  the registers 14,15,0,1,2, etc. starting at 12  bytes
off register 13.

Recall  that  register 13 is supposed to point to  the  save
area  in which to put the registers.  12(13) means 12  bytes
after this value.

LM does the reverse.

Looking  at our sample program, we see the subroutine  begin
template on lines 3 through 7.  (remember, our main  program
is  just  a subroutine of the operating system.)  Note  that
the  establishment  of the base register  must  be  done  in
between the save of the registers, including register 12 for
the  operating  system,  and the  setup  of  the  save  area
allocated by the main-program.

We see the subroutine-end template in lines 52 to 54.

Our  example program for this unit illustrates a four  level
deep  nesting,  which  should help illustrate  how  the  IBM
convention works.

It compute GLOBAL:=GLOBAL+1000+200+30+4 with each successive
addition being done by a different routine.

That  is,  INC1000 will increment GLOBAL by  1000  and  call
INC200.
INC200 will increment GLOBAL by 200 and call INC30.
INC30 will increment GLOBAL by 30 and call INC4.
INC4 will increment GLOBAL by 4 and return.

This is exemplified by the PASCAL on a succeeding page.

The main program is from line 3 to line 19.  Note the use of
the  IBM convention at the start, lines 2 to 7.  INC1000  is
called  from line 11 to line 13.  Note the use  of  the  IBM
convention to return to the operating system on lines 16  to
18.

INC1000  starts off with the IBM convention on lines  22  to
25.   GLOBAL is incremented by 1000 on lines 26 to  28.  The
call to INCX200 occurs on lines 29 to 31. And it returns  to
its caller on lines 32 to 34.

INC200 starts off with the IBM convention from lines  37  to
40.  Lines 41 to 43 increment GLOBAL by 200.  Lines 44 to 46
call INC30.  Lines 47 to 49 return to the caller.

INC30  starts off with the IBM convention from lines  53  to
55.  Lines 56 to 58 increment GLOBAL by 30.  Lines 50 to  61
call INC4.  Lines 62 to 64 return to the caller.

INC4 starts off with the IBM convention from lines 68 to 70.
Lines  71  to  73 increment GLOBAL by 4.   Lines  74  to  76
return to the caller.

Note the presence of four separate save areas


allocated by  registers saved by    name          line
Main      INC1000               SAVEMAIN      19
program
INC1000   INC200                SAVESUB       35
INC200    INC30                 SAVESUB1      50
INC30     INC4                  SAVESUB2      65
INC4      none                  SAVESUB3      77