Example of Calling an Assembler Subprogram

Restriction: The following applies to native COBOL only.

An assembler subprogram that obeys the standard COBOL calling convention takes the following form:

The assembler subprogram can consist of multiple code and data segments. You should ensure that the initial code entry point is at the start of the first code segment defined or alternatively that you explicitly define the entry point label after the End directive.

Note:
  • It is essential that each code segment is defined as having a class name of CODE or have a class-name with the suffix CODE. Failure to do so means that the Linker fails to give the segment the access rights which permits it to be executed
  • When linking dynamically, the entry point to the subprogram should be defined in the .def file which you must supply when linking. For the example above the .def file should contain the following lines:
    LIBRARY libname
    EXPORTS entry-point-name @1
;---------------------------------------------------------
; Example skeleton assembler subprogram
;---------------------------------------------------------
;---------------------------------------------------------
; Declare local data segment(s)
;---------------------------------------------------------

.386 
.MODEL FLAT ;
_DATA   segment dword use32 public 'DATA'
        ...
    data used by subprogram
        ...
_DATA ends

;---------------------------------------------------------
; Declare local code segment(s)
;---------------------------------------------------------

_TEXT   segment dword use32 public 'CODE'

assume cs:flat, ds:flat, ss:flat, es:flat

;---------------------------------------------------------
; Declare public entry point
;---------------------------------------------------------

PUBLIC EXAMPLE

;---------------------------------------------------------
; Declare equates for accessing params(COBOL convention)
;---------------------------------------------------------

param1 equ  ebp+8
param2  equ  ebp+12

EXAMPLE proc  near
    push  ebp              ; preserve ebp 
    mov   ebp,esp          ; address params on stack  
    push  esi              ; preserve esi, edi  
    push  edi

    ...
    program code
    ...

    mov  esi,[param1]      ; access parameter 1
    ...
    mov  esi,[param2]      ; access parameter 2 
    ...
    mov  eax,{return-code} ; set up 
                           ; return code
    ...

    pop  edi
    pop  esi               ; restored esi,edi
    pop  ebp               ; restored ebp
    ret

EXAMPLE endp

    _TEXT    ends
    end