Microsoft KB Archive/37531

From BetaArchive Wiki

C Does Not Return to C after Calling a COBOL Subprogram

PSS ID Number: Q37531 Article last modified on 04-20-1993

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

The information in this article applies to:
- Microsoft COBOL for MS-DOS and OS/2, versions 3.0 and 3.0a

Summary: A C main-driver program that calls a COBOL subprogram will fail to return to the C program unless the C program has some local variables declared. The COBOL system attempts to determine whether the COBOL program is the main-driver program or a subprogram. This is done by checking if the BP register is equal to zero. If BP=O, it assumes that COBOL is the main and an EXIT PROGRAM is mapped to a STOP RUN. To set the BP register so it is not equal to zero in C, allocate one or more local variables. This explains why COBOL will fail to return to C if you remove the statement “int c1=5;” in the C program below.

More Information: A sample C program that calls a COBOL subprogram and returns control to the C program is copied below. The C compile command line is as follows: cl /c /AH /Au testparm.c The COBOL compile command line for OS/2 is as follows: PCOBOL _testpar; The COBOL compile command line for MS-DOS is as follows: COBOL _testpar; The LINK command line for OS/2 is as follows: LINK testparm+_testpar,,,llibcep+pcobol+doscalls /nop/nod/noe; The LINK command line for MS-DOS is as follows: LINK testparm+_testpar,,,llibce+lcobol /nod/noe; The C program is as follows:

/* testparm.c / #include “stdio.h” char fname[]=“This is a test of C” ; extern far cdecl testpar() ; / COBOL routines are cdecl; this means the name must be prefixed with ’_’ Alternatively, you can manually reverse the parameters. / main() { int c1=5 ; / COBOL will be the main program unless BP is non-zero. BP is zero until some local variables are allocated and used. / testpar((char far ) fname, (int far *) &c1) ; printf(“Back in C”) ; }

The COBOL Version 3.0 program is as follows:

  * _testpar.cbl
   working-storage section.
   01  i pic 9999 comp-5.
   linkage section.
   01  ws-buf  pic x(64).
   01  ws-num  pic s9999 comp-5.
   procedure division using ws-buf ws-num.
  * C passes NULL terminated strings, so we have to find
  * out how long it is.
       move 0 to i.
       inspect ws-buf tallying i for
                characters before initial x'00'
       display "passed buffer = " ws-buf(1:i).
       display "passed number = " ws-num.
  * EXIT PROGRAM will only work if COBOL receives BP !=0
       exit program.

Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.