Search code examples
assemblycobolmainframe

How to address a databuffer from a pointer returned from Assembly to COBOL


I have a COBOL program that call a Assembly module. This module returns a Pointer to some data (I'll call it 'buffer') obtained inside this module. How can I address a variable in my working storage to this pointer and use the data?

In my COBOL caller I have something like:

Working Storage:

01 WK-MOD-AREA.
   03 WK-MOD-PTR          PIC 9(09) COMP VALUE 0.

01 WK-BUFFER-PTR          USAGE POINTER.
01 FILLER                 REDEFINES WK-BUFFER-PTR.
   03 WK-BUFFER-PTR-COMP  PIC 9(09) COMP.
01 WK-BUFFER-DATA         PIC X(5656) VALUE SPACES.

Procedure Division:

CALL 'MYMOD' USING WK-MOD-AREA.

MYMOD returns an address in WK-MOD-PTR.

I just tryed address the data using:

MOVE WK-MOD-PTR TO WK-BUFFER-PTR-COMP.

SET ADDRESS OF WK-BUFFER-PTR TO WK-BUFFER.

But it gives me the compiller error:

==000657==> IGYPS2161-S "ADDRESS OF" operand "WRK-BUFFER" 
was found as the receiving operand of a "SET" statement, 
but was not a level-01 or level-77 "LINKAGE SECTION" item.  
The statement was discarded.   

Thanks in advance.


Solution

  • You can do what you want but you can't change the address of an item in WORKING-STORAGE SECTION, these items have a fixed address and are allocated when the program is activated for the first time. As your compiler told you already you can do this with items in LINKAGE SECTION as these have no fixed address and aren't allocated.

    And depending on your actual use you may pass the POINTER directly to the program instead of an intermediate variable:

    Working-Storage SECTION.
    
    01 WK-BUFFER-PTR          USAGE POINTER.
    
    LINKAGE SECTION.
    01 WK-BUFFER-DATA         PIC X(5656) VALUE SPACES.
    
    Procedure Division.
    
    CALL 'MYMOD' USING WK-BUFFER-PTR.
    SET ADDRESS OF WK-BUFFER-DATA TO WK-BUFFER-PTR.
    

    Notes:

    • You'll still have to make sure that the data is freed somewhere after using it (maybe by calling the allocating program with a parameter to free).
    • You have to make sure that you don't access the field over the actual allocated length, if it isn't always 5656 Bytes. To ensure this you may use reference-modification WK-BUFFER-DATA (1:actual-length) or a table with variable size DEPENDING ON (especially useful with OCCURS UNBOUNDED).