www.sas.com > Service and Support > Technical Support
 
Technical Support SAS - The power to know(tm)
  TS Home | Intro to Services | News and Info | Contact TS | Site Map | FAQ | Feedback


*---------------------------------------------------------------------*
*                                                                     *
*            Copyright(c) 1995, SAS Institute Inc.                    *
*              Unpublished - All Rights Reserved                      *
*                                                                     *
*                     S A S / C  S A M P L E                          *
*                                                                     *
*       Name: A2CMMASM                                                *
*                                                                     *
*   Language: Assembler                                               *
*                                                                     *
* EntryPoint: ASMAIN                                                  *
*                                                                     *
* Entry Type: Standard OS Entry Linkgage                              *
*                                                                     *
*   Function: Call a C main via SAS/C entry point MAIN passing a      *
*             parameter list tailored for entry point MAIN.  Entry    *
*             point MAIN will process the parameter list passing      *
*             arguments as elements of the argv array, handling       *
*             runtime opitons, providing environment variables as     *
*             elements of the argv array, and handling redirection    *
*             of stdout to DD name SYSOUTM.                           *
*                                                                     *
*    Purpose: Demonstrate how to call a SAS/C main via entry point    *
*             MAIN from assembler.  OS load was chosen in as the      *
*             loading method.  However, a statically linked C main    *
*             or a C main loaded via attach would have exactly the    *
*             same parameter list requirements for calling purposes.  *
*                                                                     *
*             Additionally, the following are demonstrated:           *
*              - setting environment variables at runtime             *
*              - redirection of standard output (stdout) via the      *
*                parameter list                                       *
*              - calling a non-library C function from assembler      *
*              - using inline machine code                            *
*              - usage of System Interface Functions                  *
*                  - intractv()                                       *
*                  - cuserid()                                        *
*                                                                     *
* Files Note: 'prefix' is the installation defined high level         *
*             qualifier for the SAS/C product.                        *
*                                                                     *
* MVS - Compile/Link/Go: submit prefix.SAMPLE.AUX(A2CMMJ)             *
*                                                                     *
* CMS - Will not run on CMS due to the parameter list setup.          *
*                                                                     *
* Misc Notes:                                                         *
*            - A2CMMASM is not assembler dependent, use either        *
*              Assembler H or High Level Assembler.                   *
*---------------------------------------------------------------------*
*                                                                     *
*                                                                     *
* SAS/C Entry Point 'MAIN' parameter list:                            *
*                                                                     *
* At entry MAIN expects R1 to point to a standard OS VL type          *
* parameter list, with the VL bit of the last argument set.  The      *
* first argument is a pointer to a string prefixed with a halfword    *
* containing the length of the string.  The first argument should     *
* point to the string prefix, not the string.  This is the only       *
* argument to entry point MAIN.                                       *
*                                                                     *
*     R1-> +---------+       +-----------------------------------+    *
*          |VL| *char|-----> |Halfword Length of string following|    *
*          +---------+       +-----------------------------------+    *
*                            | string with:                      |    *
*                            | - arguments                       |    *
*                            | - runtime options                 |    *
*                            | - environment variables           |    *
*                            | - standard file redirections      |    *
*                            +-----------------------------------+    *
*                                                                     *
*                            NOTE: VL bit is set                      *
*                                                                     *
*---------------------------------------------------------------------*
         SPACE
R0       EQU     0
R1       EQU     1
R2       EQU     2
R3       EQU     3
R4       EQU     4
R5       EQU     5
R6       EQU     6
R7       EQU     7
R8       EQU     8
R9       EQU     9
R10      EQU     10
R11      EQU     11
R12      EQU     12
R13      EQU     13
R14      EQU     14
R15      EQU     15
         SPACE
*---------------------------------------------------------------------*
* Standard OS Entry Linkage    Note: this is not reentrant            *
*---------------------------------------------------------------------*
ASMAIN   CSECT
         STM   R14,R12,12(R13)   Save entry regs in callers area
         BALR  R9,R0             Load Base
         USING *,R9              Tell Assembler
         LR    R14,R13           Save callee save area addr
         LA    R13,SAVEAREA      Put mine in R13
         ST    R14,4(R13)        Save callee save area in mine
         ST    R13,8(R14)        Tell callee my save area adr
         SPACE
*---------------------------------------------------------------------*
* Load the C main, if the module is not loaded an abend will          *
* occur.  Otherwise, the load was successful and R0 has entry point   *
* address to the module.                                              *
*---------------------------------------------------------------------*
         SPACE
         LA    R2,LOADNAME          Ptr to module name to load
         LOAD  EPLOC=(R2)           Load the module
         LR    R5,R0                Save the entry point address
*---------------------------------------------------------------------*
* Setup parameter list address and call the C main                    *
*---------------------------------------------------------------------*
         SPACE
         LA    R1,VLPLIST           Ptr to plist for call
         LR    R15,R5               Entry point to R15
         BALR  R14,R15              Call the program
         LTR   R15,R15              Did the called program finish ok?
         BZ    UNLOAD               Yes, Continue
         SPACE
*---------------------------------------------------------------------*
* The C main did not finish up correctly issue a message and exit.    *
*---------------------------------------------------------------------*
         SPACE
         LR    R3,R15               Save return code for exit
         ST    R3,RETCODE           Set return code for msg display
         MVC   MSG,=A(MSG1)         Set msg to display
         MVC   RC,=A(RETCODE)       Move ptr to rc into plist
         LA    R1,MSGPLIST          Point r1 to plist
         L     R15,=V(A2CMEMG)      Ptr to C msg display function
         BALR  R14,R15              Display the msg
         B     EXIT                 Go exit
         SPACE
*---------------------------------------------------------------------*
* Delete the module and setup R3 with return code for exit            *
*---------------------------------------------------------------------*
         SPACE
UNLOAD   DS    0H
         LA    R2,LOADNAME          Ptr to module name loaded
         DELETE EPLOC=(R2)          Delete it, no longer needed
         SR    R3,R3                Ensure return code is zero
         SPACE
EXIT     DS    0H
         L     R13,4(R13)           Ptr to entry reg save area          00290015
         LR    R15,R3               Set return code
         LM    R0,R12,20(R13)       Reload registers  0-12..ONLY..      00300006
         L     R14,12(R13)          Restore return address to R14
         BR    R14                  Return to caller                    00320015
         EJECT
*------------------------------------------------------------------*
* Constant Area Follows                                            *
*------------------------------------------------------------------*
         SPACE
         LTORG
LOADNAME DC    CL8'A2CMM',X'00'      MAIN filename as a string
         SPACE
*------------------------------------------------------------------*
* The address of VLPLIST will be loaded into R1 when the C main    *
* is called with the VL bit set.  Note that the argument passed is *
* the address of the prefix length, not the character string.      *
*------------------------------------------------------------------*
VLPLIST  DS    0F
         DC    A(X'80000000'+OPTPREFX)
         SPACE
*------------------------------------------------------------------*
* The following is the parameter list that will be passed to       *
* entry point MAIN.  It is preceeded by a halfword with the number *
* of characters in the string.                                     *
*                                                                  *
* Note: To ensure that argument parsing does not concantenate      *
*       arguments, each option string should start with a          *
*       blank and end with a blank.                                *
*------------------------------------------------------------------*
         SPACE
         DS    0F
OPTPREFX DC    AL2(L'OPTION1+L'OPTION2+L'OPTION3)       <-- Length
OPTION1  DC    C' 1 2 3 4 5 6 >SYSOUTM '
OPTION2  DC    C' =ENTRY=MAIN =SYSDATA=Y =ENVDATA=Y '
OPTION3  DC    C' =BTRACE =FDUMP '
         SPACE
*---------------------------------------------------------------------*
* The following is the parameter list and parameters that will be     *
* used to call the message display C function, A2CMMEMG.              *
*---------------------------------------------------------------------*
         SPACE
MSG1     DC    X'1515'
         DC    C'ERROR: C Main returned non-zero return code.'
         DC    X'151500'   <== Note: MSG1 is a null terminated string
         SPACE
RETCODE  DS    F
         SPACE
MSGPLIST DS    0F          Parmlist
MSG      DS    A           Argument 1 - the message
RC       DS    A           Argument 2 - the return code
         SPACE
*------------------------------------------------------------------*
* Working Storage                                                  *
*------------------------------------------------------------------*
SAVEAREA DC    18F'0'
         SPACE
         END   ASMAIN

Copyright (c) 2000 SAS Institute Inc. All Rights Reserved.
Terms of Use & Legal Information | Privacy Statement