Announcement

Collapse
See more
See less

8 digit packed BCD to binary

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 8 digit packed BCD to binary

    converts up to 8 digits of packed BCD to a binary number ASM BCD2BIN.zip BCD2Bin.bas

  • #2
    One small caution, there are multiple data formats which may fairly be called "BCD." [one decimal digit per four-bit half-byte]. See:
    http://www.talsystems.com/tsihome_ht...ads/C2IEEE.htm
    .. for some expansion on "BCD" decimal types.

    FWIW, the output of the program is a MSGBOX with this on it:

    ' MSGBOX OUT:
    '---------------------------
    'PowerBASIC
    '---------------------------
    ' 5.02989868393566E-7, 87654321
    '---------------------------
    'OK
    '---------------------------

    But the way the code is written, it will not handle the overpunched signs you will read about in the link.

    It would also get the same result if you said
    Code:
      x  =  &h87654321
    ..
    instead of
    Code:
    x = nBCD2Bin(&h87654321, n)
    I think to be a true "BCD to BIN" converter the input needs to be a character string. That is, your input would be a four byte string, with a hex pair representation of .. .

    Code:
    87 65 43 21
    (which is "WA+[unprintable => NAK or CTRL+U)"


    . and not a simple assignment of the value 0x87654321 to a PB long integer; and the corresponding argument to the called function would be a STRING instead of a LONG.

    By the way, the BCD representation (no sign overpunch) of the value 0x87654321 (decimal 2271560481) would be the five character string with the hex pair presentation of:

    Code:
    22 71 56 04 81
    I have code to do all this (including the overpunches) , but it's "only" in BASIC which as I understand it would be of minimal interest in this forum.

    I am not qualified to comment on the quality of the assembly language code. I am qualified to comment on the utility of the function as written.

    MCM
    Last edited by Michael Mattias; 14 Feb 2017, 02:53 PM.
    Michael Mattias
    Tal Systems Inc.
    Racine WI USA
    mmattias@talsystems.com
    http://www.talsystems.com

    Comment


    • #3
      Thanks for your comments Michael. This is a BCD digit (0 to 9) in a 4 bit cell as you stated. Each byte contains two BCD digits, not in ASCII format. The comment about x = &h87654321 giving the same result as
      nBCD2Bin(&h87654321, n) is not correct. Each digit can have a value of 0 to 9, not 0 to F. There is a comment in the code about positive numbers only. This was written to convert some data from a data acquisition card that was in this format. BTW, your inputs on the forum have been most helpful over the years. Thanks to you and Gary and many others! Thanks to Vivian for maintaining and allowing PowerBasic to live on. FC

      Comment


      • #4
        Ok, I see what I did incorrectly, I was playing with the code and didn't actually run the 'original' when I got that number. Duh.

        When PB supported a BCD data type (in PB/DOS, never ported to Windows) I was doing a lot with IBM mainframe COBOL and the then leading edge plan of using this thing called File Transfer Protocol to actually exchange real live data files over the Internet, I got into this BCD in a big way.. and the PB "BCD" data type was very handy. Alas, no such datatype was offered in PB/Windows. (Well, I guess it didn't matter all that much because much of the data were sign-overpunched anyway.. except when it wasn't).

        Not only was all the mainframe data stored BCD (COBOL "S9(7)V9(2) COMP." ("COMP" is BCD on IBM M/F) and none of the input was, but we also had some bit twidlling to do because the HARDWARE 'ASCII to EBCDIC' converter (that's right, it was done in hardware as part of the FTP process) in use at the client site had some, er, "interesting" thoughts about character equivalents.

        I ended up helping a lot of the other consultants I encountered; that was the origin of the document on my website (linked above).. as I was getting tired of explaining the same thing over and over again.

        I also created a "parse the COBOL source and convert the COBOL-created data" programming library - which never sold worth diddly squat (but it really worked well!)

        MCM
        Michael Mattias
        Tal Systems Inc.
        Racine WI USA
        mmattias@talsystems.com
        http://www.talsystems.com

        Comment


        • #5
          Have you looked at the FBLD instruction, I think it can convert up to 18 BCD digits to a real using the FPU

          Comment


          • #6
            Originally posted by John Petty View Post
            Have you looked at the FBLD instruction, I think it can convert up to 18 BCD digits to a real using the FPU
            Thanks John, I was not familiar with this instruction, nor can I figure out how to use it properly. Any suggestions?
            FFC

            Comment


            • #7
              An example to get you started:
              Code:
              'PBCC6 program
              #COMPILE EXE
              #DIM ALL
              
              FUNCTION PBMAIN
              #REGISTER NONE
              
              LOCAL BinaryNumber AS QUAD
              LOCAL BCDnumber AS QUAD
              LOCAL FullBCDversion AS EXT    'a space to store the BCD number as it's 10 bytes long, just like an EXT
              
              LOCAL Result() AS QUAD
              DIM Result(0) AT VARPTR(FullBCDversion)    'overlap Result(0) with the last few bytes of the BCD number so they can be accessed easily
              
              
              'convert Binary to BCD
              BinaryNumber = 123456789012345
              
              !fild BinaryNumber       'get the binary number
              !fbstp FullBCDversion    'convert to BCD and store
              
              PRINT
              PRINT "Binary Number =";BinaryNumber
              PRINT "   BCD number = ";HEX$(Result(0))
              
              'convert BCD to binary
              Result(0)= &h0877665544332211
              
              !fbld FullBCDversion    'load the 18 digit BCD number and convert to an EXT
              !fistp BinaryNumber     'save the converted number as an integer (QUAD)
              
              PRINT
              PRINT "Binary Number =";BinaryNumber
              PRINT "   BCD number = ";HEX$(Result(0))
              
              
              WAITKEY$
              
              END FUNCTION

              Comment

              Working...
              X