MainframeMaster

COBOL Tutorial

COBOL NATIVE_BINARY Data Type - Quick Reference

Progress0 of 0 lessons

Overview

The NATIVE_BINARY data type is used in COBOL for handling native binary data representation that matches the underlying hardware architecture. It is essential for system-level programming, interfacing with other languages, and ensuring precise binary data compatibility.

Purpose and Usage

  • Native representation - Match hardware binary format exactly
  • System interfacing - Interface with system APIs and libraries
  • Language interoperability - Work with C/C++ and other languages
  • Performance optimization - Eliminate data conversion overhead
  • Platform compatibility - Ensure correct data representation

Native Binary Concept

Hardware Architecture: x86, ARM, PowerPC, etc.
Native Binary Format: Matches hardware exactly
Data Representation: No conversion needed
System APIs: Direct compatibility
External Libraries: Seamless integration

NATIVE_BINARY ensures data matches the underlying hardware representation.

Syntax and Usage

NATIVE_BINARY fields are declared using the COMP clause with NATIVE_BINARY to ensure native hardware binary representation.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
* Basic NATIVE_BINARY syntax level-number data-name PIC 9(length) COMP NATIVE_BINARY. * Examples 01 NATIVE-INTEGER PIC 9(9) COMP NATIVE_BINARY. 01 NATIVE-SHORT PIC 9(4) COMP NATIVE_BINARY. 01 NATIVE-LONG PIC 9(10) COMP NATIVE_BINARY. 01 NATIVE-FLOAT PIC 9(6)V99 COMP NATIVE_BINARY. * Complete example 01 NATIVE-DATA-STRUCTURE. 05 NATIVE-ID PIC 9(9) COMP NATIVE_BINARY. 05 NATIVE-COUNT PIC 9(4) COMP NATIVE_BINARY. 05 NATIVE-VALUE PIC 9(8)V99 COMP NATIVE_BINARY. 05 NATIVE-FLAG PIC 9(1) COMP NATIVE_BINARY. 05 NATIVE-TIMESTAMP PIC 9(10) COMP NATIVE_BINARY.

NATIVE_BINARY uses COMP with NATIVE_BINARY for native hardware representation.

Complete Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
* Complete example using NATIVE_BINARY IDENTIFICATION DIVISION. PROGRAM-ID. NATIVE-BINARY-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 NATIVE-STRUCTURE. 05 NATIVE-INT-VALUE PIC 9(9) COMP NATIVE_BINARY VALUE 12345. 05 NATIVE-SHORT-VALUE PIC 9(4) COMP NATIVE_BINARY VALUE 100. 05 NATIVE-FLOAT-VALUE PIC 9(6)V99 COMP NATIVE_BINARY VALUE 123.45. 05 NATIVE-FLAG-VALUE PIC 9(1) COMP NATIVE_BINARY VALUE 1. 01 STANDARD-STRUCTURE. 05 STANDARD-INT-VALUE PIC 9(9) COMP VALUE 12345. 05 STANDARD-SHORT-VALUE PIC 9(4) COMP VALUE 100. 05 STANDARD-FLOAT-VALUE PIC 9(6)V99 COMP VALUE 123.45. 05 STANDARD-FLAG-VALUE PIC 9(1) COMP VALUE 1. 01 COMPARISON-RESULTS. 05 NATIVE-SIZE PIC 9(4) COMP. 05 STANDARD-SIZE PIC 9(4) COMP. 05 SIZE-DIFFERENCE PIC 9(4) COMP. PROCEDURE DIVISION. MAIN-LOGIC. * Display native binary values DISPLAY "Native Binary Values:" DISPLAY "Integer: " NATIVE-INT-VALUE DISPLAY "Short: " NATIVE-SHORT-VALUE DISPLAY "Float: " NATIVE-FLOAT-VALUE DISPLAY "Flag: " NATIVE-FLAG-VALUE * Compare with standard values DISPLAY "Standard Values:" DISPLAY "Integer: " STANDARD-INT-VALUE DISPLAY "Short: " STANDARD-SHORT-VALUE DISPLAY "Float: " STANDARD-FLOAT-VALUE DISPLAY "Flag: " STANDARD-FLAG-VALUE * Perform arithmetic operations PERFORM ARITHMETIC-OPERATIONS STOP RUN. ARITHMETIC-OPERATIONS. * Add values ADD NATIVE-SHORT-VALUE TO NATIVE-INT-VALUE DISPLAY "After addition: " NATIVE-INT-VALUE * Multiply values MULTIPLY NATIVE-FLOAT-VALUE BY NATIVE-INT-VALUE DISPLAY "After multiplication: " NATIVE-INT-VALUE.

This example shows how to use NATIVE_BINARY for native hardware representation.

System API Interface

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
* System API interface using NATIVE_BINARY IDENTIFICATION DIVISION. PROGRAM-ID. SYSTEM-API-INTERFACE. DATA DIVISION. WORKING-STORAGE SECTION. 01 API-PARAMETERS. 05 API-FUNCTION-ID PIC 9(4) COMP NATIVE_BINARY VALUE 1001. 05 API-BUFFER-SIZE PIC 9(4) COMP NATIVE_BINARY VALUE 1024. 05 API-RETURN-CODE PIC 9(4) COMP NATIVE_BINARY. 05 API-ERROR-CODE PIC 9(4) COMP NATIVE_BINARY. 01 API-BUFFER. 05 BUFFER-DATA PIC X(1024). 01 SYSTEM-CALL-PARAMS. 05 CALL-FUNCTION PIC 9(4) COMP NATIVE_BINARY. 05 CALL-PARAM1 PIC 9(9) COMP NATIVE_BINARY. 05 CALL-PARAM2 PIC 9(9) COMP NATIVE_BINARY. 05 CALL-RETURN PIC 9(9) COMP NATIVE_BINARY. PROCEDURE DIVISION. MAIN-LOGIC. * Set up API call parameters MOVE API-FUNCTION-ID TO CALL-FUNCTION MOVE API-BUFFER-SIZE TO CALL-PARAM1 MOVE 0 TO CALL-PARAM2 * Call system API CALL "SYSTEM_API" USING SYSTEM-CALL-PARAMS * Check return code MOVE CALL-RETURN TO API-RETURN-CODE IF API-RETURN-CODE NOT = 0 MOVE CALL-RETURN TO API-ERROR-CODE DISPLAY "API Error: " API-ERROR-CODE ELSE DISPLAY "API call successful" END-IF STOP RUN.

NATIVE_BINARY ensures compatibility with system APIs and external libraries.

Common Use Cases

NATIVE_BINARY is essential in various scenarios where native hardware representation and system-level programming are required.

C/C++ Interfacing

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
* C/C++ interfacing with NATIVE_BINARY IDENTIFICATION DIVISION. PROGRAM-ID. C-INTERFACE-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-STRUCTURE. 05 C-INT-VALUE PIC 9(9) COMP NATIVE_BINARY. 05 C-SHORT-VALUE PIC 9(4) COMP NATIVE_BINARY. 05 C-FLOAT-VALUE PIC 9(6)V99 COMP NATIVE_BINARY. 05 C-CHAR-ARRAY PIC X(50). 05 C-POINTER-VALUE PIC 9(9) COMP NATIVE_BINARY. 01 C-FUNCTION-PARAMS. 05 FUNC-PARAM1 PIC 9(9) COMP NATIVE_BINARY. 05 FUNC-PARAM2 PIC 9(9) COMP NATIVE_BINARY. 05 FUNC-RETURN PIC 9(9) COMP NATIVE_BINARY. 01 C-STRING-BUFFER PIC X(100). PROCEDURE DIVISION. MAIN-LOGIC. * Initialize C structure MOVE 12345 TO C-INT-VALUE MOVE 100 TO C-SHORT-VALUE MOVE 123.45 TO C-FLOAT-VALUE MOVE "Hello from COBOL" TO C-CHAR-ARRAY MOVE 0 TO C-POINTER-VALUE * Call C function MOVE C-INT-VALUE TO FUNC-PARAM1 MOVE C-SHORT-VALUE TO FUNC-PARAM2 CALL "C_FUNCTION" USING C-STRUCTURE, FUNC-RETURN * Process return value IF FUNC-RETURN = 0 DISPLAY "C function call successful" ELSE DISPLAY "C function call failed" END-IF STOP RUN.

NATIVE_BINARY ensures proper data alignment with C/C++ structures.

System Programming

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
* System programming with NATIVE_BINARY IDENTIFICATION DIVISION. PROGRAM-ID. SYSTEM-PROGRAMMING. DATA DIVISION. WORKING-STORAGE SECTION. 01 SYSTEM-INFO. 05 PROCESS-ID PIC 9(9) COMP NATIVE_BINARY. 05 THREAD-ID PIC 9(9) COMP NATIVE_BINARY. 05 MEMORY-ADDRESS PIC 9(9) COMP NATIVE_BINARY. 05 SYSTEM-TIME PIC 9(10) COMP NATIVE_BINARY. 05 USER-ID PIC 9(4) COMP NATIVE_BINARY. 01 SYSTEM-CALL-STRUCTURE. 05 SYS-CALL-NUMBER PIC 9(4) COMP NATIVE_BINARY. 05 SYS-CALL-PARAM1 PIC 9(9) COMP NATIVE_BINARY. 05 SYS-CALL-PARAM2 PIC 9(9) COMP NATIVE_BINARY. 05 SYS-CALL-PARAM3 PIC 9(9) COMP NATIVE_BINARY. 05 SYS-CALL-RETURN PIC 9(9) COMP NATIVE_BINARY. 01 MEMORY-BUFFER. 05 BUFFER-SIZE PIC 9(4) COMP NATIVE_BINARY VALUE 4096. 05 BUFFER-DATA PIC X(4096). PROCEDURE DIVISION. MAIN-LOGIC. * Get system information PERFORM GET-SYSTEM-INFO * Allocate memory PERFORM ALLOCATE-MEMORY * Perform system operations PERFORM SYSTEM-OPERATIONS STOP RUN. GET-SYSTEM-INFO. * Get process ID MOVE 20 TO SYS-CALL-NUMBER CALL "SYSCALL" USING SYSTEM-CALL-STRUCTURE MOVE SYS-CALL-RETURN TO PROCESS-ID * Get system time MOVE 13 TO SYS-CALL-NUMBER CALL "SYSCALL" USING SYSTEM-CALL-STRUCTURE MOVE SYS-CALL-RETURN TO SYSTEM-TIME. ALLOCATE-MEMORY. * Allocate memory buffer MOVE 9 TO SYS-CALL-NUMBER MOVE 0 TO SYS-CALL-PARAM1 MOVE BUFFER-SIZE TO SYS-CALL-PARAM2 MOVE 3 TO SYS-CALL-PARAM3 CALL "SYSCALL" USING SYSTEM-CALL-STRUCTURE MOVE SYS-CALL-RETURN TO MEMORY-ADDRESS. SYSTEM-OPERATIONS. * Perform system operations here DISPLAY "System operations completed".

System programming requires NATIVE_BINARY for hardware-level operations.

Performance Optimization

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
* Performance optimization with NATIVE_BINARY IDENTIFICATION DIVISION. PROGRAM-ID. PERFORMANCE-OPTIMIZATION. DATA DIVISION. WORKING-STORAGE SECTION. 01 OPTIMIZED-DATA. 05 COUNTER PIC 9(9) COMP NATIVE_BINARY VALUE 0. 05 ARRAY-SIZE PIC 9(4) COMP NATIVE_BINARY VALUE 1000. 05 LOOP-COUNT PIC 9(6) COMP NATIVE_BINARY VALUE 100000. 05 START-TIME PIC 9(10) COMP NATIVE_BINARY. 05 END-TIME PIC 9(10) COMP NATIVE_BINARY. 05 ELAPSED-TIME PIC 9(10) COMP NATIVE_BINARY. 01 DATA-ARRAY. 05 ARRAY-ELEMENT PIC 9(4) COMP NATIVE_BINARY OCCURS 1000 TIMES. 01 STANDARD-DATA. 05 STD-COUNTER PIC 9(9) COMP VALUE 0. 05 STD-ARRAY-SIZE PIC 9(4) COMP VALUE 1000. 05 STD-LOOP-COUNT PIC 9(6) COMP VALUE 100000. 01 STD-ARRAY. 05 STD-ELEMENT PIC 9(4) COMP OCCURS 1000 TIMES. PROCEDURE DIVISION. MAIN-LOGIC. * Performance test with NATIVE_BINARY PERFORM NATIVE-BINARY-TEST * Performance test with standard COMP PERFORM STANDARD-COMP-TEST STOP RUN. NATIVE-BINARY-TEST. * Get start time MOVE FUNCTION CURRENT-DATE TO START-TIME * Perform intensive operations PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > LOOP-COUNT PERFORM PROCESS-NATIVE-ARRAY END-PERFORM * Get end time MOVE FUNCTION CURRENT-DATE TO END-TIME COMPUTE ELAPSED-TIME = END-TIME - START-TIME DISPLAY "Native Binary Time: " ELAPSED-TIME. STANDARD-COMP-TEST. * Get start time MOVE FUNCTION CURRENT-DATE TO START-TIME * Perform intensive operations PERFORM VARYING STD-COUNTER FROM 1 BY 1 UNTIL STD-COUNTER > STD-LOOP-COUNT PERFORM PROCESS-STANDARD-ARRAY END-PERFORM * Get end time MOVE FUNCTION CURRENT-DATE TO END-TIME COMPUTE ELAPSED-TIME = END-TIME - START-TIME DISPLAY "Standard COMP Time: " ELAPSED-TIME. PROCESS-NATIVE-ARRAY. * Process native binary array MOVE COUNTER TO ARRAY-ELEMENT(1). PROCESS-STANDARD-ARRAY. * Process standard array MOVE STD-COUNTER TO STD-ELEMENT(1).

Performance optimization uses NATIVE_BINARY to eliminate conversion overhead.

Best Practices and Tips

Following these best practices ensures effective use of NATIVE_BINARY in COBOL applications.

NATIVE_BINARY Best Practices

  • Use for system interfaces - Use NATIVE_BINARY when interfacing with system APIs
  • Match external structures - Ensure alignment with external data structures
  • Consider platform differences - Be aware of different hardware architectures
  • Document data layout - Document the binary layout for maintenance
  • Test on target platforms - Test on all target hardware platforms
  • Handle endianness - Consider byte order for cross-platform compatibility

Platform Compatibility

PlatformConsiderationsData Size
x86 (32-bit)Little-endian, 4-byte alignmentint: 4 bytes, long: 4 bytes
x86-64 (64-bit)Little-endian, 8-byte alignmentint: 4 bytes, long: 8 bytes
ARM (32-bit)Little-endian, 4-byte alignmentint: 4 bytes, long: 4 bytes
ARM (64-bit)Little-endian, 8-byte alignmentint: 4 bytes, long: 8 bytes
PowerPCBig-endian, 4-byte alignmentint: 4 bytes, long: 4 bytes

Performance Considerations

  • No conversion overhead - Eliminates data conversion costs
  • Direct memory access - Enables direct memory manipulation
  • System call efficiency - Optimizes system API calls
  • External interface speed - Faster interfacing with other languages
  • Memory alignment - Ensures proper memory alignment
  • Cache efficiency - Better cache utilization

When to Use NATIVE_BINARY

Use CaseNATIVE_BINARY SuitabilityReasoning
System APIsExcellentRequired for system compatibility
C/C++ interfacingExcellentEnsures data structure alignment
Performance criticalGoodEliminates conversion overhead
Business logicPoorUse standard COBOL types
Display dataPoorUse display-oriented types

NATIVE_BINARY Quick Reference

UsageSyntaxExample
Integer01 data-name PIC 9(n) COMP NATIVE_BINARY01 INT-VAL PIC 9(9) COMP NATIVE_BINARY
Short integer01 data-name PIC 9(n) COMP NATIVE_BINARY01 SHORT-VAL PIC 9(4) COMP NATIVE_BINARY
Long integer01 data-name PIC 9(n) COMP NATIVE_BINARY01 LONG-VAL PIC 9(10) COMP NATIVE_BINARY
Float01 data-name PIC 9(n)V99 COMP NATIVE_BINARY01 FLOAT-VAL PIC 9(6)V99 COMP NATIVE_BINARY
Pointer01 data-name PIC 9(n) COMP NATIVE_BINARY01 PTR-VAL PIC 9(9) COMP NATIVE_BINARY

Test Your Knowledge

1. What is the primary purpose of the NATIVE_BINARY data type in COBOL?

  • To perform arithmetic operations
  • To handle native binary data representation for system-level programming
  • To control file operations
  • To manage character data

2. How does NATIVE_BINARY differ from standard COMP data types?

  • They are exactly the same
  • NATIVE_BINARY uses native hardware representation while COMP uses COBOL-specific format
  • NATIVE_BINARY is for character data only
  • COMP is for binary data only

3. When would you use NATIVE_BINARY in COBOL applications?

  • For simple text processing
  • For system-level programming and interfacing with other languages
  • For file I/O operations only
  • For mathematical calculations only

4. Which of the following is a common use case for NATIVE_BINARY?

  • Display text to users
  • Interfacing with system APIs and external libraries
  • File sorting operations
  • Data validation

5. How do you declare a NATIVE_BINARY field in COBOL?

  • Using PIC 9 with NATIVE_BINARY
  • Using COMP with NATIVE_BINARY
  • Using PIC X with NATIVE_BINARY
  • Using PIC N with NATIVE_BINARY

Frequently Asked Questions