Search390.com Developer Tip August 22, 2001 ======================================================== SPONSORED BY: Storage Decisions 2001 ======================================================== Storage Decisions 2001 brings together the top storage industry analysts and expert technologists to give you specific how-to advice to set storage strategy, make key decisions and manage storage effectively. This exclusive conference is FREE to those who qualify. Apply today at www.StorageDecisions2001.com. ======================================================== ============================================ search390 TIPS CONTEST ============================================ Have a great tip of your own that you'd like to share with the rest of us? Then send it in. Our tip of the month contest has been extended through the end of August, so there is still time to take home the fabulous T-Rex remote control dinotronic dinosaur. To submit your tip today, go to http://search390.techtarget.com/tipsSubmit/1,289485,sid10,00.html ============================================ TODAY'S DEVELOPER TIP: Practicing safe HEX By Jim Keohane I trust you have all seen Mark Fleming's Tip "Hex Conversion in COBOL" 7/25 which coincidentally showed a COBOL subroutine similar in function to one I suggested in my "COBOL ... Hexadecimally Challenged" tip. I'm curious as to how many noticed the neat trick embodied in Mark's version where he uses packed and zoned decimal conversion for fields WS-ONUM-DISPLAY and WS-ONUM-DECIMAL. Many folks would be concerned about the dreaded S0C7 ABEND's due to invalid numerics. No need to worry. Mark's code clearly practiced safe HEX. Assembler programmers have used this trick for decades. I posted an assembler solution on ASM-L list a few weeks ago where someone had asked for an easy way to convert storage to displayable hex characters. Assuming the storage to be displayed in hex was four bytes long at label FOUR and that you wanted the 8 hex characters at LABEL EIGHT then this assembler code works: UNPK EIGHT(9),FOUR(5) TR EIGHT,=16C'0123456789ABCDEF' . . . FOUR DS CL4,C EIGHT DS CL8,C Note the spare byte following FOUR and EIGHT so that FOUR and EIGHT can be used as if FIVE and NINE by TR instruction. In COBOL this is similar to: 05 FIVE PIC 9(9) COMP-3. 05 FOUR REDEFINES FIVE PIC XXXX. 05 NINE PIC 9(9). 05 EIGHT REDEFINES NINE PIC X(8). . . . MOVE 'TEN5' TO FOUR. MOVE FIVE TO NINE. <- Mark's MAGIC!!! INSPECT EIGHT CONVERTING X'FAFBFCFDFEFF' TO 'ABCDEF'. DISPLAY 'HEX = <' EIGHT '>.'. Result: HEX = <E3C5D5F5>. The UNPK..TR (or MOVE..INSPECT/CONVERTING) works because UNPK does two things. First, the rightmost byte of source is moved to rightmost byte of destination AFTER that byte's hex nibbles are reversed. We don't care about this UNPK feature and therefore have an extra trailing byte that is ignored. Next UNPK move remaining NIBBLES in source, from right to left, into remaining BYTES in destination, from right to left, preceding each by X'F'. See COBOL example above. It moves 'TEN5' (X'E3C5D5F5') to FOUR. The fifth byte in FIVE can be anything. The UNPK (or MOVE) swaps nibbles in fifth byte of FIVE and puts it into ninth byte of NINE. We don't care about it. The remaining NIBBLEs in FOUR, right to left (5,F,5,D,5,C,3,E), are moved into bytes of EIGHT, right to left, preceded by X'F'. So EIGHT is now X'FEF3FCF5FDF5FFF5'. The INSPECT/CONVERTING changes that to X'C5F3C3F5C4F5C6F5' or displayable 'E3C5D5F5'. How come no S0C7 ABEND, you ask? Because UNPK is not doing math! It is strictly moving data according to the two rules mentioned earlier plus some other rules not involved in this case. Below is a variation of the subroutine based on my original approach but further simplified to only do the same hex conversion as is done in Mark's example. It requires 2 standard tables but the logic only requires 2 or 3 COBOL VERBs! My version has code for both calling program, which does DISPLAYs of input and results, and of subroutine. Subroutine has a PERFORM of a MOVE and a single INSPECT CONVERTING. Note also the FROM-HERE and TO-THERE input parameters of subroutine are described as being one and two million bytes, respectively. In actuality, it's the calling program that sets the actual size. The subroutine's LINKAGE SECTION can describe almost any length as long as the actual logic never references more than is actually passed by caller. Note that this version, though superficially similar to Mark's, uses the rightmost byte where nibbles have been swapped. The "MOVE FROM-S9 (I) ... TO-PACKED (I)" does the nibble-swapping. The original two nibbles in each byte of FROM-HERE becomes the left nibbles of every two bytes in TO-THERE. E.g. X'2C' becomes X'2CC2'. Conversion via LEFT-NIBBLES results in '2C' or X'F2C3'. This is not a "my version is better than his" argument. There are obvious tradeoffs comparing an approach with simplified logic that is table driven or another approach that skips use of a table but the logic is somewhat more involved. To be precise, Mark's example did involve a table. The INSPECT ... CONVERTING X'FAFBFCFDFEFF' TO 'ABCDEF' either builds a 256 byte TR table dynamically or, if compiler is smart enough, has that same table built during compile. You just don't see it. - Jim p.s. Here's COBOL calling program HORRIBLE*, subroutine SAFEHEX and the output: ID DIVISION. PROGRAM-ID. HORRIBLE. DATA DIVISION. WORKING-STORAGE SECTION. 77 LEN PIC S9(9) COMP VALUE +4. 77 FOUR PIC X(4) VALUE 'TEN5'. 77 EIGHT PIC X(8) VALUE ALL '*'. PROCEDURE DIVISION. CALL 'SAFEHEX' USING LEN, FOUR, EIGHT. DISPLAY 'FOUR=<' FOUR '>'. DISPLAY 'EIGHT=<' EIGHT '>'. GOBACK. END PROGRAM HORRIBLE. ID DIVISION. PROGRAM-ID. SAFEHEX. DATA DIVISION. WORKING-STORAGE SECTION. 77 I PIC S9(9) COMP. 01 ALL-256-VALUES. 05 PIC X(16) VALUE X'000102030405060708090A0B0C0D0E0F'. 05 PIC X(16) VALUE X'101112131415161718191A1B1C1D1E1F'. 05 PIC X(16) VALUE X'202122232425262728292A2B2C2D2E2F'. 05 PIC X(16) VALUE X'303132333435363738393A3B3C3D3E3F'. 05 PIC X(16) VALUE X'404142434445464748494A4B4C4D4E4F'. 05 PIC X(16) VALUE X'505152535455565758595A5B5C5D5E5F'. 05 PIC X(16) VALUE X'606162636465666768696A6B6C6D6E6F'. 05 PIC X(16) VALUE X'707172737475767778797A7B7C7D7E7F'. 05 PIC X(16) VALUE X'808182838485868788898A8B8C8D8E8F'. 05 PIC X(16) VALUE X'909192939495969798999A9B9C9D9E9F'. 05 PIC X(16) VALUE X'A0A1A2A3A4A5A6A7A8A9AAABACADAEAF'. 05 PIC X(16) VALUE X'B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF'. 05 PIC X(16) VALUE X'C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF'. 05 PIC X(16) VALUE X'D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF'. 05 PIC X(16) VALUE X'E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF'. 05 PIC X(16) VALUE X'F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF'. 01 ALL-256 REDEFINES ALL-256-VALUES PIC X(256). 01 LEFT-NIBBLES-VALUES. 05 PIC X(16) VALUE '0000000000000000'. 05 PIC X(16) VALUE '1111111111111111'. 05 PIC X(16) VALUE '2222222222222222'. 05 PIC X(16) VALUE '3333333333333333'. 05 PIC X(16) VALUE '4444444444444444'. 05 PIC X(16) VALUE '5555555555555555'. 05 PIC X(16) VALUE '6666666666666666'. 05 PIC X(16) VALUE '7777777777777777'. 05 PIC X(16) VALUE '8888888888888888'. 05 PIC X(16) VALUE '9999999999999999'. 05 PIC X(16) VALUE 'AAAAAAAAAAAAAAAA'. 05 PIC X(16) VALUE 'BBBBBBBBBBBBBBBB'. 05 PIC X(16) VALUE 'CCCCCCCCCCCCCCCC'. 05 PIC X(16) VALUE 'DDDDDDDDDDDDDDDD'. 05 PIC X(16) VALUE 'EEEEEEEEEEEEEEEE'. 05 PIC X(16) VALUE 'FFFFFFFFFFFFFFFF'. 01 LEFT-NIBBLES REDEFINES LEFT-NIBBLES-VALUES PIC X(256). LINKAGE SECTION. 77 HOW-MANY PIC 9(9) COMP. 01 FROM-HERE. 05 FROM-S9 PIC S9 OCCURS 1000000. 01 TO-THERE. 05 OCCURS 1000000. 10 TO-AS-IS PIC S9. 10 TO-PACKED PIC S9 COMP-3. PROCEDURE DIVISION USING HOW-MANY, FROM-HERE, TO-THERE. PERFORM VARYING I FROM 1 BY 1 UNTIL I GREATER THAN HOW-MANY MOVE FROM-S9 (I) TO TO-AS-IS (I), TO-PACKED (I) END-PERFORM. INSPECT TO-THERE (1:HOW-MANY + HOW-MANY) CONVERTING ALL-256 TO LEFT-NIBBLES. GOBACK. END PROGRAM SAFEHEX. ******************************** FOUR=<TEN5> EIGHT=< E3C5D5F5> ******************************** * - HORRIBLE. I had once toyed with the idea of writing a book titled "Horrible Things You Can Do In COBOL." Requests for ideas elicited very few responses so I dropped the project. If anyone has ideas please email me with subject "HORRIBLE COBOL." ------------------------------------------------------ About the author: Jim Keohane ([EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>)is president of a New York consulting company Multi-Platforms, Inc. His company specializes in commercial software development/consulting with emphasis on cross-platform and performance issues. Did you like this tip? Send us an email to: [EMAIL PROTECTED] to let us know your thoughts. Also, Jim Keohane is dying for your feedback (he really isn't dying, so don't worry). But, he did post this in the Developer forum recently: "...I need some feedback on past Search390 tips and, more importantly, suggestions for future tips. I'm posting this here because the tips that involve actual code examples seem to get the best response. Please email [EMAIL PROTECTED] with Subject "Tips" or "Feedback." Please email me or Jim your feedback, or post your comments here: http://search390.discussions.techtarget.com/WebX?[EMAIL PROTECTED]^[email protected] ======================================================= Web Enabling Tips: Any systems manager or developer implementing an e-business strategy would benefit from these hints. Provided by 390 industry experts, these tips will help you successfully meet the next generation of e-business challenges. To try out this free e-mail service for yourself, just update your user profile at http://search390.techtarget.com/register/1,,sid10,00.html and be sure you select the "Web-enabling" checkbox. ======================================================= Share the knowledge! ======================================================= Have you got some code secrets to share with your colleagues? Don't miss out on the opportunity to help someone else or to post your own questions. Check out our search390 Developer Discussion Forum at http://search390.discussions.techtarget.com/WebX?50@@.ee83ff7 Also check out our other search390 forums on operating systems, e-business, and sound off (aka miscellaneous!). There's sure to be something there to spark your interest. ======================================================== Disclaimer: Our tips exchange is a forum for you to share technical advice and expertise with your peers and to learn from other IT professionals. Techtarget.com provides the infrastructure to facilitate this sharing of information. However, we can't guarantee the accuracy and validity of the material submitted. You agree that your use of the ask the expert services and your reliance on any questions, answers, information or other materials received through the web site will be at your own risk. ======================================================== ======================================================== If you would like to sponsor this or any TechTarget newsletter, please contact Gabrielle DeRussy at [EMAIL PROTECTED] ======================================================== If you no longer wish to receive this newsletter simply reply to this message with "REMOVE" in the subject line. Or, visit http://search390.techtarget.com/register and adjust your subscriptions accordingly. If you choose to unsubscribe using our automated processing, you must send the "REMOVE" request from the email account to which this newsletter was delivered. Please allow 24 hours for your "REMOVE" request to be processed.
