VAX Subroutines

Mark Smotherman. Last updated September 2002.

(under construction)

Introduction

DEC VAX ... first described in 1978 ... 16 general registers ...

(JA: Utilization of full VAX calls vs simpler calling sequences)

Dedicated registers

        r12 (ap) - argument pointer
        r13 (fp) - frame pointer
        r14 (sp) - stack pointer

Subroutine call instructions

        bsb   - branch to subroutine   - pushes updated pc onto stack
        jsb   - jump to subroutine
        rsb   - return from subroutine - pops new pc from stack

Procedure call instructions

The first word of procedure must contain an entry mask that specifies which registers are to be saved and what overflow enables are to be used during execution of the procedure; the entry mask is saved on the stack along with bits from the PSW (including the condition codes); the calls instruction includes a count of the number of arguments pushed onto the stack, this count is pushed onto the stack and used by the ret instruction to adjust the stack pointer upon return.

        calls - call with arguments on stack
        callg - call with arguments in memory
        ret

Calling program structure

           ...
           pushl parm2
           pushl parm1
           calls 2,subr
           ...

Subroutine structure

        subr:
           (entry word)                ! callee saves convention
                                       !  using entry mask
           ... body of subroutine ...

           ret

Example assembly code generated by C compiler

        void main() {       _main:
          void swap();        subl2 $8,sp          / provide space for a,b
          int a,b;            movl  $5,-4(fp)      / a = 5
          a = 5;              movl  $44,-8(fp)     / b = 44
          b = 44;             subl3 $8,fp,r0       / r0 = &b
          swap(&a,&b);        pushl r0             / push &b onto stack
        }                     subl3 $4,fp,r0       / r0 = &a
                              pushl r0             / push &a onto stack
                              calls $2,_swap       / call swap with 2 parms
                              ret

        void swap(x,y)      _swap:
        int *x,*y;            (entry word, incld. register save mask)
        {                     subl2 $4,sp          / provide space for temp
          int temp;           movl  *4(ap),-4(fp)  / temp=*x => temp=*(&a)
          temp = *x;          movl  *8(ap),*4(ap)  / *x=*y   => *(&a)=*(&b)
          *x = *y;            movl  -4(fp),*8(ap)  / *y=temp => *(&b)=temp
          *y = temp;          ret
          return;
        }

stack contents as example code executes

                      A                                       +--------+
        low addresses |                                       | temp   |<-sp
                                          +--------+          +--------+
                                          | -      |<-sp,fp   | -      |<-fp
                                          | mask   |          | mask   |
                                          | old ap |          | old ap |
        high addresses |                  | old fp |          | old fp |
                       V                  | old pc |          | old pc |
                                          | old ri |          | old ri |
                                          | ...    |          | ...    |
                                          | old rj |          | old rj |
                                          +--------+          +--------+
                         +--------+       | 2      |<-ap      | 2      |<-ap
                         | &a     |<-sp   | &a     |          | &a     |
                         | &b     |       | &b     |          | &b     |
        +--------+       +--------+       +--------+          +--------+
        | b      |<-sp   | b = 44 |       | b = 44 |          | b = 44 |
        | a      |       | a = 5  |       | a = 5  |          | a = 5  |
        +--------+       +--------+       +--------+          +--------+
        | -      |<-fp   | -      |<-fp   | -      |          | -      |

        after 1st        just before      just after          after 1st
        inst. of main    call in main     call                inst. of swap

Other resources


[History of subroutines page] [Mark's homepage] [CPSC homepage] [Clemson Univ. homepage]

mark@cs.clemson.edu