Chapter Contents

Previous

Next
Communication with Assembler Programs

Calling a C Program from Assembler

Before a C program can be executed, the C execution framework must be created. Normally, the framework is created by the library routine L$CMAIN, which is defined by the linkage editor to be the first routine executed in a C load module. L$CMAIN expects to be called by the operating system and therefore expects a standard OS/390 or CMS format parameter list, consisting of a character string plus various system-dependent format information. L$CMAIN processes this information, transforms it into the C standard argc and argv format, and calls the C main function with the constructed argc and argv . L$CMAIN can be called directly from assembler to pass control to a main C routine via the normal C entry point, MAIN. However, invoking a C program via MAIN is rarely convenient because the type of parameter list required is both inflexible (allowing only character data to be passed) and operating system dependent.

To avoid this problem, two additional entry points, named $MAINC and $MAINO, are provided to L$CMAIN.

Note:    The behavior of $MAINC and $MAINO in CICS is different than the behavior of these entry points under OS/390 or CMS. See Calling a C Program from Assembler via $MAINO in CICS.  [cautionend]

Entry point $MAINC expects to receive a list of addresses in the standard OS VL-type parameter list format. $MAINC transforms the input parameters into the standard C argc value (number of arguments plus 1) and the argv vector. Each element of argv after argv[0] contains the corresponding address from the input parameter list. (For example, argv[1] contains the first address from the list.)

Entry point $MAINO expects a list of addresses in the standard OS VL-type parameter list format. The first argument to $MAINO is a pointer to a string containing run-time options, preceded by a halfword containing the number of characters in the string. The first word in the argument list should address the prefix, not the string itself. This information is processed by the run-time library and is not passed to the C main program. Each element of argv after argv[0] contains the corresponding address from the input parameter list. (For example, argv[1] contains the second address from the list, which represents the first argument.)

Calling a C main Function from Assembler via $MAINO shows an assembler program that calls a C function through $MAINO. The C program using the argument as passed by assembler through $MAINO is in C main Function Called from an Assembler Driver via $MAINO.


Calling a C main Function from Assembler via $MAINO
MAINASM CSECT STM 14,12,12(13) standard OS entry linkage BALR 9,0 USING *,9 LR 14,13 LA 13,SAVEAREA ST 14,4(13) ST 13,8(14) end of standard entry * * Assembler segment that calls $MAINO * LA 1,PARMLIST L 15,=V($MAINO) BALR 14,15 * * Other processing can go here before exiting * MAINXT L 13,4(13) standard exit linkage LM 14,12,12(13) BR 14 end of standard exit * LTORG , SAVEAREA DC 18F'0' PARMLIST DS 0F DC A(RNTMPRM) DC A(ARGV1) DC A(ARGV2) DC A(X'80000000'+ARGV3) ARGV1 DC F'42' ARGV2 DC D'67.4242' ARGV3 DC CL4'HELP' RNTMPRM DC AL2(L'RNTMOPT) RNTMOPT DC C'=FILLMEM =FDUMP' END


C main Function Called from an Assembler Driver via $MAINO
#include <options.h> void main(int argc,char **argv) { int i; double f; char verb[4]; i = *(int *) argv[1]; f = *(double *) argv[2]; memcpy(verb,argv[3],4); }

Calling a C Program from Assembler via $MAINO in CICS shows sample code that calls a C program from assembler using the entry point $MAINO from CICS. CICS command-level programs are called with a parameter list of at least two entries: the address of the EXEC interface block (EIB) and the COMMAREA address. If there is no COMMAREA, a value of x'ff000000' is passed in its place.

When you use the $MAINO or $MAINC entry points, a VL-format parameter list must be passed to the library. Make sure that the last address in the list has the high-order (VL) bit set. If you are passing parameters other than the EIB and COMMAREA addresses, you cannot specify the pseudo-null value of X'FF000000' for the COMMEAREA address. The library interprets X'FF000000' as the last parameter in the list.


Calling a C Program from Assembler via $MAINO in CICS
CALLMNO DFHEIENT CODEREG=(5), EIBREG=, DATAREG=(13) base reg = R5, dynamic storage = R13 SPACE EXEC CICS ADDRESS EIB(R4) SPACE LA R1,PGMPARMS Point to program parms. ST R1,ARGPTR Save in parm list. ST R4,ARGV1 Save address of EIB. MVC ARGV2,=X'FF000000' Indicate no commarea. LA R1,PARMLIST Point to the parm list. L R15,=V($MAINO) Call the C program. BALR R14,R15 SPACE DFHEIRET SPACE PGMPARMS DS 0H DC AL2(L'ARGSTR) length of run-time argument string ARGSTR DC C'=56K =storage' SPACE DFHEISTG SPACE PARMLIST DS 0F ARGPTR DS A pointer to run-time arguments ARGV1 DS A argv [1] pointer to the EIB ARGV2 DS A argv [2] pointer to any commarea SPACE DFHEIEND SPACE CREGS SPACE END


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.