![]() |
Assembler Bit Processing Bit Manipulation http://www.simotime.com Copyright © 1987-2010 SimoTime Enterprises All Rights Reserved |
| Table of Contents | Version 09.05.17 |
This is an example of how a COBOL program can call an Assembler routine to do bit-level manipulation. The three basic functions provided are as follows.
| 1. | Determine the setting of a bit (0 or 1). | |
| 2. | Set a bit to 0. | |
| 3. | Set a bit to 1. |
This example uses an approach of converting the bit information in a single byte to or from an eight-byte field of COBOL accessible zeroes and ones. This example contains one JCL member to execute the the COBOL program that calls the Assembler routine. The main COBOL program (ASMBITC1.CBL) is used to demonstrate and test the bit manipulation functions. The bit manipulation functions are in a separate, callable Assembler program (ASM4BITS.CBL) . The COBOL program was written and tested using the VS COBOL II dialect. Also, the COBOL program will work with COBOL for MVS and COBOL/370. A JCL member is provided to run the job as an MVS batch job on an IBM mainframe or as a project with Micro Focus Mainframe Express (MFE) running on a PC with Windows (refer to http://www.microfocus.com ).
In the world of programming there are many ways to solve a problem. This suite of programs is provided as a programming example of one of the possible solutions to the problem of determining and changing the bits within a byte. This example may serve as a tutorial for new programmers and as a reference for experienced programmers. Additional information (including a COBOL routine that does bit manipulation) is provided in the Downloads and Links to Similar Pages section of this document.
Note: The source code for this example is available from the SimoTime Library under Download Directory at www.simotime.com
For each bit that is ON (1) in the BTS-PASS-BITS field the correspondong byte in the BTS-PASS-BYTES field is set to a value of one. For each bit that is OFF (0) in the BTS-PASS-BITS field the correspondong byte in the BTS-PASS-BYTES field is set to a value of zero.
| Input | BTS-PASS-BITS, a one byte field (8-bits) | |
| Output | BTS-PASS-BYTES, an eight byte field | |
| Example | if BTS-PASS-BITS = x'55' then BTS-PASS-BYTES will be '01010101' | |
For each byte that is a one in the BTS-PASS-BYTES field the correspondong bit in the BTS-PASS-BITS field is set to ON (1). For each byte that is zero in the BTS-PASS-BYTES field the correspondong bit in the BTS-PASS-BITS field is set to OFF (0).
| Input | BTS-PASS-BYTES, an eight byte field | |
| Output | BTS-PASS-BITS, a one byte field (8-bits) | |
| Example | if BTS-PASS-BYTES = '01010101' then BTS-PASS-BITS will be set to x'55' | |
The callable interface requires a data structure to be defined. A copy file (PASSBITS.CPY) is provided with the following source code.
*****************************************************************
* Data Structure used for calling SIMOBITS. *
*****************************************************************
* Copyright (C) 1987-2010 SimoTime Enterprises *
* All Rights Reserved *
*****************************************************************
* Provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
01 BTS-PASS-AREA.
05 BTS-PASS-REQUEST PIC X(8).
05 BTS-PASS-RESULT PIC S9(9) COMP.
05 BTS-PASS-RECORD.
10 BTS-PASS-BITS PIC X.
10 BTS-PASS-BYTES.
15 BTS-PASS-BYTE-01 PIC X.
15 BTS-PASS-BYTE-02 PIC X.
15 BTS-PASS-BYTE-03 PIC X.
15 BTS-PASS-BYTE-04 PIC X.
15 BTS-PASS-BYTE-05 PIC X.
15 BTS-PASS-BYTE-06 PIC X.
15 BTS-PASS-BYTE-07 PIC X.
15 BTS-PASS-BYTE-08 PIC X.
The following will translate an eight character field of 0's and 1's to a one byte field with the bits set to match to 0's and 1's of the eight character field. Please note: the content of the BTS-PASS-REQUEST field must be upper case and contain 'COMPRESS'
*****************************************************************
* The coding required to do the call to the Byte-Bit Routine *
* (translate an 8-byte field to a 1-byte field). *
*****************************************************************
MOVE '11111111' to BTS-PASS-BYTES
MOVE 'COMPRESS' to BTS-PASS-REQUEST
CALL 'ASM4BITS' using BTS-PASS-AREA
In the preceding example the contents of BTS-PASS-BITS will be X'FF' or high-value upon return from the call to SIMOBITS.
The following will set the individual bytes of an eight character field to 0's or 1's based on the bit settings of a one byte field. Please note: the content of the BTS-PASS-REQUEST field must be upper case and contain 'EXPAND' followed by two spaces
*****************************************************************
* The coding required to do the call to the Byte-Bit Routine *
* (translate a 1-byte field to an 8-byte field). *
*****************************************************************
MOVE X'55' to BTS-PASS-BITS
MOVE 'EXPAND ' to BTS-PASS-REQUEST
CALL 'ASM4BITS' using BTS-PASS-AREA
In the preceding example the contents of BTS-PASS-BYTES will be '01010101' upon return from the call to SIMOBITS.
The following is the mainframe JCL member (ASMBITJ1.JCL) that is required to run as a job on the mainframe or as a Mainframe Express project on the PC.
//ASMBITJ1 JOB SIMOTIME,ACCOUNT,CLASS=1,MSGCLASS=0,NOTIFY=CSIP1 //* ******************************************************************* //* This program is provided by: * //* SimoTime Enterprises, LLC * //* (C) Copyright 1987-2010 All Rights Reserved * //* Web Site URL: http://www.simotime.com * //* e-mail: helpdesk@simotime.com * //* ******************************************************************* //* //* Text - COBOL calls ASSEMBLER for Bit and Byte manipulation //* Author - SimoTime Enterprises //* Date - January 01, 1989 //* //* This set of programs illustrate the use a COBOL program that calls //* an Assembler routine to do bit manipulation. //* This suite of programs will convert between a one-byte //* (consisting of eight-bits) field and an eight-byte field //* consisting of ZEROES and ONES. //* //* The DD statement for SYSOUT is required by this example. The //* COBOL program may be compiled using the SYSOUT directive and //* without the DD statement for SYSOUT an RTS173 or RTS227 may occur. //* //* This set of programs will run on a mainframe under MVS or on //* a Personal Computer running Windows and Mainframe Express by //* Micro Focus. //* //* ************ //* * CBLBITJ1 * //* ********jcl* //* * //* * //* ************ ************ //* * ASMBITC1 *-----* DISPLAY * //* ********cbl* ************ //* * * //* * * ************ //* * *-CALL--* ASM4BITS * //* * ********cbl* //* * //* ************ //* * EOJ * //* ************ //* //* ******************************************************************* //* Step 1 of 1 This is a single step job. //* //ASMBITS1 EXEC PGM=ASMBITC1 //STEPLIB DD DSN=SIMOTIME.DEMO.LOADLIB1,DISP=SHR //SYSOUT DD SYSOUT=* //*
This program (ASMBITC1.CBL) was written to test the callable Assembler routine (ASM4BITS.MLC) that does the conversion between a 1-byte field of 8-bits and a field of 8-bytes.
IDENTIFICATION DIVISION.
PROGRAM-ID. ASMBITC1.
AUTHOR. SIMOTIME ENTERPRISES.
*****************************************************************
* Copyright (C) 1987-2010 SimoTime Enterprises, LLC. *
* *
* All rights reserved. Unpublished, all rights reserved under *
* copyright law and international treaty. Use of a copyright *
* notice is precautionary only and does not imply publication *
* or disclosure. *
* *
* Permission to use, copy, modify and distribute this software *
* for any non-commercial purpose and without fee is hereby *
* granted, provided the SimoTime copyright notice appear on all *
* copies of the software. The SimoTime name or Logo may not be *
* used in any advertising or publicity pertaining to the use *
* of the software without the written permission of SimoTime *
* Enterprises. *
* *
* Permission to use, copy, modify and distribute this software *
* for any commercial purpose requires a fee to be paid to *
* SimoTime Enterprises. Once the fee is received by SimoTime *
* the latest version of the software will be delivered and a *
* license will be granted for use within an enterprise, *
* provided the SimoTime copyright notice appear on all copies *
* of the software. The SimoTime name or Logo may not be used *
* in any advertising or publicity pertaining to the use of the *
* software without the written permission of SimoTime *
* Enterprises. *
* *
* SimoTime Enterprises makes no warranty or representations *
* about the suitability of the software for any purpose. It is *
* provided "AS IS" without any express or implied warranty, *
* including the implied warranties of merchantability, fitness *
* for a particular purpose and non-infringement. SimoTime *
* Enterprises shall not be liable for any direct, indirect, *
* special or consequential damages resulting from the loss of *
* use, data or projects, whether in an action of contract or *
* tort, arising out of or in connection with the use or *
* performance of this software *
* *
* SimoTime Enterprises *
* 15 Carnoustie Drive *
* Novato, CA 94949-5849 *
* 415.883.6565 *
* *
* RESTRICTED RIGHTS LEGEND *
* Use, duplication, or disclosure by the Government is subject *
* to restrictions as set forth in subparagraph (c)(1)(ii) of *
* the Rights in Technical Data and Computer Software clause at *
* DFARS 52.227-7013 or subparagraphs (c)(1) and (2) of *
* Commercial Computer Software - Restricted Rights at 48 *
* CFR 52.227-19, as applicable. Contact SimoTime Enterprises, *
* 15 Carnoustie Drive, Novato, CA 94949-5849. *
* *
*****************************************************************
* This program is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
*
*****************************************************************
* Source Member: ASMBITC1.CBL
* Copy Files: PASSBITS.CPY
* Calls to: ASM4BITS
*****************************************************************
*
* ASMBITC1 - Call ASM4BITS to convert between bits and bytes.
*
* CALLING PROTOCOL
* ----------------
* Use standard procedure to RUN or ANIMATE.
*
* DESCRIPTION
* -----------
* This program will call a COBOL routine to access a
* routine to convert between bits and bytes..
*
* ************
* * CBLBITJ1 *
* ********jcl*
* *
* *
* ************ ************
* * ASMBITC1 *-----* CONSOLE *
* ********cbl* ******dsply*
* *
* *
* ************
* * ASM4BITS *
* ********cbl*
*
* This program calls ASM4BITS to perform two functions:
*
* EXPAND - translate the bits of a one-byte field to bytes
* in an eight-byte field.
* COMPRESS - translate the bytes of an eight-byte field into
* bits in a one-byte field.
*
*****************************************************************
* The EXPAND function will do the following:
*
* Input: BTS-PASS-BITS, a one byte field (8-bits)
* OUTPUT: BTS-PASS-BYTES, an eight byte field
*
* For each bit that is on in the BTS-PASS-BITS field set the
* corresponding byte in the BTS-PASS-BYTES field to a value
* of one.
*
* For each bit that is off in the BTS-PASS-BITS field set the
* corresponding byte in the BTS-PASS-
* BYTES field to a value of zero.
*
* Example: if BTS-PASS-BITS = x'55'
* then BTS-PASS-BYTES will be '01010101'
*
*****************************************************************
* The COMPRESS function will do the following:
*
* Input: BTS-PASS-BYTES, an eight byte field
* Output: BTS-PASS-BITS, a one byte field (8-bits)
*
* For each byte that is a one in the BTS-PASS-BYTES field the
* corresponding bit in the BTS-PASS-BITS field is set to ON (1)
*
* For each byte that is zero in the BTS-PASS-BYTES field the
* corresponding bit in the BTS-PASS-BITS field is set to OFF (0).
*
* Example: if BTS-PASS-BYTES = '01010101'
* then BTS-PASS-BITS set to x'55'
*
*****************************************************************
*
* MAINTENANCE
* -----------
* 1989/02/27 Simmons, Created program.
* 1997/03/17 Simmons, Updated for call to ASM4BITS.
*
*****************************************************************
*
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
*****************************************************************
* Data-structure for Title and Copyright...
* ------------------------------------------------------------
01 SIM-TITLE.
05 T1 pic X(11) value '* ASMBITC1 '.
05 T2 pic X(34) value 'Convert between Bits and Bytes '.
05 T3 pic X(10) value ' v08.06.05'.
05 T4 pic X(24) value ' http://www.simotime.com'.
01 SIM-COPYRIGHT.
05 C1 pic X(11) value '* ASMBITC1 '.
05 C2 pic X(20) value 'Copyright 1987-2010 '.
05 C3 pic X(28) value ' SimoTime Enterprises, LLC '.
05 C4 pic X(20) value ' All Rights Reserved'.
01 SIM-THANKS-01.
05 C1 pic X(11) value '* ASMBITC1 '.
05 C2 pic X(32) value 'Thank you, this technology was p'.
05 C3 pic X(32) value 'roduced at SimoTime Enterprises,'.
05 C4 pic X(04) value ' LLC'.
01 SIM-THANKS-02.
05 C1 pic X(11) value '* ASMBITC1 '.
05 C2 pic X(32) value 'Please send all inquires or sugg'.
05 C3 pic X(32) value 'estions to the helpdesk@simotime'.
05 C4 pic X(04) value '.com'.
COPY PASSBITS.
*****************************************************************
* BTS-PASS-REQUEST values when calling ASM4BITS.
* ------------------------------------------------------------
01 REQUEST-4-EXPAND pic X(8) value 'EXPAND '.
01 REQUEST-4-COMPRESS pic X(8) value 'COMPRESS'.
*****************************************************************
* Buffer used for posting messages to the console.
* ------------------------------------------------------------
01 MESSAGE-BUFFER.
05 MESSAGE-HEADER pic X(11) value '* ASMBITC1 '.
05 MESSAGE-TEXT pic X(68).
*****************************************************************
* Work fields used for testing call to ASM4BITS.
* ------------------------------------------------------------
01 HEX-00 pic X value x'00'.
01 HEX-55 pic X value x'55'.
01 HEX-AA pic X value x'AA'.
01 HEX-FF pic X value x'FF'.
01 BYTES-00 pic X(8).
01 BYTES-55 pic X(8).
01 BYTES-AA pic X(8).
01 BYTES-FF pic X(8).
01 ZERO-VALUE pic 9 value 0.
01 MINUS-ONE pic S9 value -1.
01 MINUS-ONE-X redefines MINUS-ONE
pic X.
01 POSITIVE-BIT-VALUE pic X(8) value '00000000'.
01 NEGATIVE-BIT-VALUE pic X(8) value '00000000'.
01 THREE-BYTES.
05 PACK-03 pic S9(5) comp-3 value 0.
01 TWO-BYTES pic X(2) value '00'.
01 EIGHT-BYTES pic X(8) value LOW-VALUES.
01 THE-EASY-WAY.
05 FILLER pic X(9) value 'Efficient'.
05 FILLER pic X(10) value ', PACK-03 '.
05 PACK-03-E1L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-E1R pic X(4) value SPACES.
05 FILLER pic X value SPACE.
05 PACK-03-E2L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-E2R pic X(4) value SPACES.
05 FILLER pic X value SPACE.
05 PACK-03-E3L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-E3R pic X(4) value SPACES.
05 FILLER pic X(13) value ', UNPACKED-5='.
05 UNPACKED-5 pic 9(5) value 0.
01 THE-HARD-WAY.
05 FILLER pic X(9) value 'Difficult'.
05 FILLER pic X(10) value ', PACK-03 '.
05 PACK-03-H1L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-H1R pic X(4) value SPACES.
05 FILLER pic X value SPACE.
05 PACK-03-H2L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-H2R pic X(4) value SPACES.
05 FILLER pic X value SPACE.
05 PACK-03-H3L pic X(4) value SPACES.
05 FILLER pic X value '-'.
05 PACK-03-H3R pic X(4) value SPACES.
05 FILLER pic X(13) value ', FIVE-BYTES='.
05 FIVE-BYTES pic X(5) value '00000'.
01 ALPHABET-UPPER pic X(26) value 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
01 IX-1 pic 9(3) value 0.
01 IX-2 pic 9(3) value 0.
01 IX-3 pic 9(3) value 0.
*****************************************************************
PROCEDURE DIVISION.
perform Z-POST-COPYRIGHT
perform BYTES-TO-BITS-COMPRESS.
perform BITS-TO-BYTES-EXPAND.
perform ALPHABET-DUMP.
perform COBOL-UNPACK.
perform Z-THANK-YOU.
move ZERO to RETURN-CODE
GOBACK.
*****************************************************************
ALPHABET-DUMP.
move 'Starting ALPHABET-DUMP Routine...' to MESSAGE-TEXT
perform Z-POST-MESSAGE
add 1 to ZERO giving IX-1
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
perform 26 times
move ALPHABET-UPPER(IX-1:1) to BTS-PASS-BITS
call 'ASM4BITS' using BTS-PASS-AREA
move 'Position nnn is x, the binary value is xxxx-xxxx'
to MESSAGE-TEXT
move IX-1 to MESSAGE-TEXT(10:03)
move BTS-PASS-BITS to MESSAGE-TEXT(17:1)
move BTS-PASS-BYTES(1:4) to MESSAGE-TEXT(40:4)
move BTS-PASS-BYTES(5:4) to MESSAGE-TEXT(45:4)
perform Z-POST-MESSAGE
add 1 to IX-1
end-perform
exit.
*****************************************************************
BITS-TO-BYTES-EXPAND.
move 'Starting BITS-TO-BYTES-EXPAND Routine...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
move HEX-00 to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to BYTES-00
perform DISPLAY-BYTES
move HEX-FF to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to BYTES-FF
perform DISPLAY-BYTES
move HEX-55 to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to BYTES-55
perform DISPLAY-BYTES
move HEX-AA to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to BYTES-AA
perform DISPLAY-BYTES
exit.
*****************************************************************
BYTES-TO-BITS-COMPRESS.
move 'Starting BYTES-TO-BITS-COMPRESS Routine...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
move '00000000' to BTS-PASS-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BITS to HEX-00
perform DISPLAY-BYTES
move '11111111' to BTS-PASS-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BITS to HEX-FF
perform DISPLAY-BYTES
move '01010101' to BTS-PASS-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BITS to HEX-55
perform DISPLAY-BYTES
move '10101010' to BTS-PASS-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BITS to HEX-AA
perform DISPLAY-BYTES
exit.
*****************************************************************
* If you thought the UnPack was reserved for the mainframe *
* assembler programmers then take a look at the following *
* routine. The is a bit of an esoteric use of bit mainipulation *
* but it does unpack a data string into a new data string. *
* *
* A quicker and better way to unpack a field is as follows: *
* ADD PACKED-FIELD TO ZERO GIVING UNPACKED-FIELD. *
*****************************************************************
COBOL-UNPACK.
move 'Starting the COBOL-UNPACK Routine...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
add 615 to ZERO giving PACK-03
* Do the UNPACK the easy way...
add PACK-03 to ZERO giving UNPACKED-5
* First, determine the bit configuration for zero to be
* used to set the left-nibble of the unpacked bytes.
* Then determine the negative sign bit configuration.
* This section of code is used so the program will run
* properly in both an EBCDIC and ASCII environment.
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
move ZERO-VALUE to BTS-PASS-BITS
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to POSITIVE-BIT-VALUE
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
move MINUS-ONE-X to BTS-PASS-BITS
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to NEGATIVE-BIT-VALUE
move '0000' to NEGATIVE-BIT-VALUE(5:4)
* Do the UNPACK the esoteric way...
move all '0' to FIVE-BYTES
add 1 to ZERO giving IX-1
add 1 to ZERO giving IX-2
add 3 to ZERO giving IX-3
perform until IX-3 = 1
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
move THREE-BYTES(IX-1:1) to BTS-PASS-BITS
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to EIGHT-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
move POSITIVE-BIT-VALUE to BTS-PASS-BYTES
move EIGHT-BYTES(1:4) to BTS-PASS-BYTES(5:4)
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BITS to FIVE-BYTES(IX-2:1)
add 1 to IX-2
move EIGHT-BYTES(5:4) to BTS-PASS-BYTES(5:4)
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BITS to FIVE-BYTES(IX-2:1)
subtract 1 from IX-3
add 1 to IX-2
add 1 to IX-1
end-perform
perform COBOL-UNPACK-UNITS.
* Display the results...
perform COBOL-UNPACK-POST-EFFICIENT
perform COBOL-UNPACK-POST-DIFFICULT
move 'Finished the COBOL-UNPACK Routine...'
to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*---------------------------------------------------------------*
COBOL-UNPACK-POST-DIFFICULT.
move THREE-BYTES(1:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-H1L
move BTS-PASS-BYTES(5:4) to PACK-03-H1R
move THREE-BYTES(2:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-H2L
move BTS-PASS-BYTES(5:4) to PACK-03-H2R
move THREE-BYTES(3:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-H3L
move BTS-PASS-BYTES(5:4) to PACK-03-H3R
move THE-HARD-WAY to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*---------------------------------------------------------------*
COBOL-UNPACK-POST-EFFICIENT.
move THREE-BYTES(1:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-E1L
move BTS-PASS-BYTES(5:4) to PACK-03-E1R
move THREE-BYTES(2:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-E2L
move BTS-PASS-BYTES(5:4) to PACK-03-E2R
move THREE-BYTES(3:1) to BTS-PASS-BITS
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES(1:4) to PACK-03-E3L
move BTS-PASS-BYTES(5:4) to PACK-03-E3R
move THE-EASY-WAY to MESSAGE-TEXT
perform Z-POST-MESSAGE
exit.
*---------------------------------------------------------------*
COBOL-UNPACK-UNITS.
move REQUEST-4-EXPAND to BTS-PASS-REQUEST
move THREE-BYTES(IX-1:1) to BTS-PASS-BITS
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BYTES to EIGHT-BYTES
move REQUEST-4-COMPRESS to BTS-PASS-REQUEST
if PACK-03 < 0
move NEGATIVE-BIT-VALUE to BTS-PASS-BYTES
else
move POSITIVE-BIT-VALUE to BTS-PASS-BYTES
end-if
move EIGHT-BYTES(1:4) to BTS-PASS-BYTES(5:4)
* move EIGHT-BYTES(5:4) to BTS-PASS-BYTES(1:4)
call 'ASM4BITS' using BTS-PASS-AREA
move BTS-PASS-BITS to FIVE-BYTES(IX-2:1)
exit.
*****************************************************************
DISPLAY-BYTES.
move 'Binary value is ' to MESSAGE-TEXT(1:16)
move BTS-PASS-BYTES(1:4) to MESSAGE-TEXT(17:4)
move '-' to MESSAGE-TEXT(21:1)
move BTS-PASS-BYTES(5:4) to MESSAGE-TEXT(22:4)
perform Z-POST-MESSAGE
exit.
*****************************************************************
* The following Z-Routines perform administrative tasks *
* for this program. *
*****************************************************************
*****************************************************************
Z-POST-COPYRIGHT.
display SIM-TITLE
display SIM-COPYRIGHT
exit.
*****************************************************************
Z-POST-MESSAGE.
display MESSAGE-BUFFER
move SPACES to MESSAGE-TEXT
exit.
*****************************************************************
Z-THANK-YOU.
display SIM-THANKS-01
display SIM-THANKS-02
exit.
*****************************************************************
* This example is provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
This program (ASM4BITS.CBL) was originally written in the early 1970's time frame. It was used in a production environment for a few years but in 1986 it was replaced by a callable COBOL program. Today, this program is only used by SimoTime for teaching, learning and demonstration purposes. Today, the program still compiles and executes on the Mainframe in a ZOS environment and has been tested on the PC with Mainframe Express from Micro Focus. Using the "Animation" capability of Mainframe Express is a good way to view the execution of the program at the source code level and examine the results of each line of code as it is executed.
ASM4BITS CSECT
***********************************************************************
* This program is provided by: *
* SimoTime Enterprises, LLC *
* (C) Copyright 1987-2010 All Rights Reserved *
* *
* Web Site URL: http://www.simotime.com *
* e-mail: helpdesk@simotime.com *
***********************************************************************
* *
* Created: 1974/06/01, Simmons *
* Changed: 1974/06/01, Simmons, no changes to date... *
* *
***********************************************************************
* *
* This program will run on an IBM Mainframe using MVS or a PC using *
* Micro Focus Mainframe Express, version 2.5 or later (MFE) with *
* the Assembler Option. *
* *
* This program provides an example of an assembler sub-routine that *
* provides an eight-byte string of zeroes or ones to coincide with *
* the bits in a single byte. *
* *
* Using the Micro Focus Animation You can immediately see the results *
* of each instruction execution. This is a very effective way to *
* become familiar with how these techniques work. *
* *
***********************************************************************
*
* This program is a callable routine that performs two functions:
*
* EXPAND - translate the bits of a one-byte field to bytes
* in an eight-byte field.
* COMPRESS - translate the bytes of an eight-byte field into
* bits in a one-byte field.
*
* ------------------------------------------------------------------- *
* The EXPAND function will do the following.
*
* The call interface is as follows.
* move 'EXPAND ' to BTS-PASS-REQUEST
* call 'ASM4BITS' using BTS-PASS-AREA
*
* Input: BTS-PASS-BITS, a one byte field (8-bits)
* OUTPUT: BTS-PASS-BYTES, an eight byte field
*
* For each bit that is on in the BTS-PASS-BITS field set the
* corresponding byte in the BTS-PASS-BYTES field to a value
* of one.
*
* For each bit that is off in the BTS-PASS-BITS field set the
* corresponding byte in the BTS-PASS-
* BYTES field to a value of zero.
*
* Example: if BTS-PASS-BITS = x'55'
* then BTS-PASS-BYTES will be '01010101'
*
* ------------------------------------------------------------------- *
* The COMPRESS function will do the following:
*
* The call interface is as follows.
* move 'COMPRESS' to BTS-PASS-REQUEST
* call 'ASM4BITS' using BTS-PASS-AREA
*
* Input: BTS-PASS-BYTES, an eight byte field
* Output: BTS-PASS-BITS, a one byte field (8-bits)
*
* For each byte that is a one in the BTS-PASS-BYTES field the
* corresponding bit in the BTS-PASS-BITS field is set to ON (1)
*
* For each byte that is zero in the BTS-PASS-BYTES field the
* corresponding bit in the BTS-PASS-BITS field is set to OFF (0).
*
* Example: if BTS-PASS-BYTES = '01010101'
* then BTS-PASS-BITS set to x'55'
*
***********************************************************************
* ASM4BITS, Expand or compress between bits and bytes. *
***********************************************************************
*
R0 EQU 0
R1 EQU 1
R2 EQU 2
R3 EQU 3
R4 EQU 4
R5 EQU 5
R6 EQU 6
R7 EQU 7
R8 EQU 8
R9 EQU 9
R10 EQU 10
R11 EQU 11
R12 EQU 12
R13 EQU 13
R14 EQU 14
R15 EQU 15
*
* ------------------------------------------------------------------- *
SAVE (14,12)
BALR 12,0
USING *,12
*
ST R1,SAVER1 * Save Register 1
L R8,0(,R1) * Get address of PASS AREA
L R6,0(,R1)
LA R6,12(,R6) * Get address of Single Byte
L R7,0(,R1)
LA R7,13(,R7) * Get address of eight bytes
*
CLC 0(8,R8),REQEXP * Is this an EXPAND request...
BE EXPAND
*
CLC 0(8,R8),REQCOM * Is this a COMPRESS request...
BE COMPRESS
*
B EORNOK
*
* ------------------------------------------------------------------- *
* Based on the bit within a one-byte field being set to "OFF" or "ON"
* move a "0" or "1" value to the corresponding byte within an
* eight-byte string.
*
EXPAND EQU *
MVC WRK04+3(1),0(R6) * Save the one-byte
MVC 0(8,R7),ZEROES * Init eight-byte string to zeroes
LA R5,128
LA R3,8 * Load 8 into REG-3 for loop count
BCTLOOP1 EQU *
L R4,WRK04
NR R4,R5 * Logical and of bit position
BZ *+8
MVI 0(,R7),C'1' * Move one if bit on
LA R7,1(,R7) * Incr to Next Byte String Address
SRL R5,X'01' * Shift right 1-bit position
BCT R3,BCTLOOP1 * Loop until REG-3 goes to ZERO
B RETURN
*
* ------------------------------------------------------------------- *
* Based on a "0" or "1" value of an individual byte within an
* eight-byte string set the corresponding bit within a one-byte field
* to "OFF" or "ON".
*
COMPRESS EQU *
MVI 0(,R6),X'00' * Init the one-byte to low-value
LA R5,128
LA R3,8 * Load 8 into REG-3 for loop count
SR R4,R4
BCTLOOP2 EQU *
CLI 0(,R7),C'1' * Is eight-byte position a one
BNE *+6
OR R4,R5 * Set bit if value is one
LA R7,1(,R7) * Incr to Next Byte String Address
SRL R5,X'01' * Shift right 1-bit position
BCT R3,BCTLOOP2 * Loop until REG-3 goes to ZERO
ST R4,WRK04 * Store the one-byte value
MVC 0(1,R6),WRK04+3 * Move one-byte to PASS AREA
B RETURN
*
* ------------------------------------------------------------------- *
* Success, return to the calling program with a ZERO Return Code.
*
RETURN EQU *
SR 15,15 * Set return code to ZERO
RETURN (14,12),RC=(15)
*
* ------------------------------------------------------------------- *
* ABENDing, return to the calling program with a non-ZERO Return Code.
*
EORNOK EQU *
LA R15,16 * Set return code to 16 or x'10'
RETURN (14,12),RC=(15)
*
* ------------------------------------------------------------------- *
* Data buffers used within the routine.
*
REQEXP DC CL8'EXPAND '
REQCOM DC CL8'COMPRESS'
SAVER1 DC 4XL1'00'
WRK04 DC 4XL1'00'
ZEROES EQU *
ZERO DC CL1'0'
DC 7CL1'0'
END
This program provides very little visual information when it is executed on the mainframe. The real value of this program is observed when it is animated using Mainframe Express provided by Micro Focus. It is possible to watch the actual execution of each individual instruction and to immediately see the results.
This copy file (PASSBITS.CPY) provides a convenient method for defining the data areas (or fields) that are used to pass information between the demo program and the bit and byte conversion routine.
*****************************************************************
* Data Structure used for calling SIMOBITS. *
*****************************************************************
* Copyright (C) 1987-2010 SimoTime Enterprises *
* All Rights Reserved *
*****************************************************************
* Provided by SimoTime Enterprises *
* Our e-mail address is: helpdesk@simotime.com *
* Also, visit our Web Site at http://www.simotime.com *
*****************************************************************
01 BTS-PASS-AREA.
05 BTS-PASS-REQUEST PIC X(8).
05 BTS-PASS-RESULT PIC S9(9) COMP.
05 BTS-PASS-RECORD.
10 BTS-PASS-BITS PIC X.
10 BTS-PASS-BYTES.
15 BTS-PASS-BYTE-01 PIC X.
15 BTS-PASS-BYTE-02 PIC X.
15 BTS-PASS-BYTE-03 PIC X.
15 BTS-PASS-BYTE-04 PIC X.
15 BTS-PASS-BYTE-05 PIC X.
15 BTS-PASS-BYTE-06 PIC X.
15 BTS-PASS-BYTE-07 PIC X.
15 BTS-PASS-BYTE-08 PIC X.
The purpose of this document is to assist as a tutorial for new programmers or as a quick reference for experienced programmers. In the world of programming there are many ways to solve a problem. This suite of programs is provided as a COBOL ad Assembler programming example of one of the possible solutions to the problem of determining and changing the bits within a byte.This example uses an approach of converting the bit information in a single byte to or from an eight-byte field of COBOL accessible zeroes and ones.
Permission to use, copy, modify and distribute this software, documentation or training material for any purpose requires a fee to be paid to Simotime Enterprises. Once the fee is received by SimoTime the latest version of the software, documentation or training material will be delivered and a license will be granted for use within an enterprise, provided the SimoTime copyright notice appear on all copies of the software. The SimoTime name or Logo may not be used in any advertising or publicity pertaining to the use of the software without the written permission of SimoTime Enterprises.
SimoTime Enterprises makes no warranty or representations about the suitability of the software, documentation or learning material for any purpose. It is provided "AS IS" without any express or implied warranty, including the implied warranties of merchantability, fitness for a particular purpose and non-infringement. SimoTime Enterprises shall not be liable for any direct, indirect, special or consequential damages resulting from the loss of use, data or projects, whether in an action of contract or tort, arising out of or in connection with the use or performance of this software, documentation or training material.
If you have any questions, suggestions or comments please call or send an e-mail to: helpdesk@simotime.com
You may download this example at http://www.simotime.com/sim4dzip.htm#AsmBitManipulation as a Z-Pack. The Z-Packs provide individual programming examples, documentation and test data files in a single package. The Z-Packs are usually in zip format to reduce the amount of time to download.
Please view the complete list of SimoTime Z-Pack Examples at http://www.simotime.com/sim4dzip.htm .
Note: You must be attached to the Internet to download a Z-Pack or view the list.
It is possible to do bit manipulation in COBOL. An example that uses a callable COBOL program instead of a callable Assembler routine is available. Refer to COBOL does Bit Manipulation example for more information.
Check out The COBOL Connection for more examples of mainframe COBOL coding techniques and sample code.
Check out The SimoTime Library for a wide range of topics for Programmers, Project Managers and Software Developers.
To review all the information available on this site start at The SimoTime Home Page .
If you have any questions, suggestions or comments please call or send an e-mail to: helpdesk@simotime.com.
Founded in 1987, SimoTime Enterprises is a privately owned company. We specialize in the creation and deployment of business applications using new or existing technologies and services. We have a team of individuals that understand the broad range of technologies being used in today's environments. This includes the smallest thin client using the Internet and the very large mainframe systems. There is more to making the Internet work for your company's business than just having a nice looking WEB site. It is about combining the latest technologies and existing technologies with practical business experience. It's about the business of doing business and looking good in the process. Quite often, to reach larger markets or provide a higher level of service to existing customers it requires the newer Internet technologies to work in a complementary manner with existing corporate mainframe systems. Whether you want to use the Internet to expand into new market segments or as a delivery vehicle for existing business functions simply give us a call or check the web site at http://www.simotime.com.
| Return-to-Top |
| Copyright © 1987-2010 SimoTime Enterprises All Rights Reserved |
| When technology complements business |
| http://www.simotime.com |
| Version 06.11.08 |