home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / ASCBCD.AZM / ASCBCD.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  2.8 KB  |  92 lines

  1. ;----------------------------------------------------------------
  2. ;         This is a module in the ASMLIB library. 
  3. ;
  4. ; Convert the ascii characters -> by DE into a BCD number in HL.
  5. ; On exit DE -> the byte that caused the end of the conversion and 
  6. ; A also contains the byte. This routine is extremely useful for
  7. ; converting numbers in console buffers into numbers in HL.
  8. ; Note that if the first character that is read is a minus sign
  9. ; then the accumulator will return the minus character else it
  10. ; will return a zero flag to indicate that the number is positive.
  11. ;
  12. ;            Written        R.C.H.        16/9/83
  13. ;            Last Update    R.C.H.        16/9/83
  14. ;----------------------------------------------------------------
  15. ;
  16.     name    'ascbcd'
  17. ;
  18.     public    ascbcd
  19.     extrn    caps
  20.     maclib    z80
  21. ;
  22. minus    equ    '-'
  23. ;
  24. ascbcd:
  25.     xra    a
  26.     sta    fneg            ; Initialize the sign
  27.     sta    chr            ; Save in the last character
  28.     lxi    h,00            ; Clear result to initialize
  29. ascbcd2:    ; loop here to read memory characters.
  30.     ldax    d            ; Get a character
  31.     cpi    ' '            ; Spacer ??
  32.     jrz    ascbcd4            ; Ignore it then.
  33.     cpi    '-'            ; Negative number ??
  34.     jrz    do$neg            ; Handle it
  35.     call    caps            ; Make upper case
  36.     sta    chr            ; Save it. This is a flag too.
  37.     sui    '0'
  38.     cpi    10            ; Convert to a BCD single digit.
  39.     jrc    ascbcd3            ; Note that A..F is illegal since BCD.
  40.     sui    'A' - '0' - 10
  41.     cpi    10            ; Check if in range 0..9
  42.     jrnc    bcd$fin            ; Load the neg flag and return
  43. ;
  44. ascbcd3:    ; Here we multiply the result by 10 then mask in the digit
  45.     push    b
  46.     mov    b,h
  47.     mov    c,l            ; Take a copy of the original
  48.     dad    h            ; * 2
  49.     dad    h            ; * 4
  50.     dad    b            ; * 5 ( added the original)
  51.     dad    h            ; * 10 total
  52.     pop    b            ; Restore
  53.     ora    l            ; Mask in the NEW digit
  54.     mov    l,a            ; Put into result
  55. ascbcd4:    ; Bump pointer and continue
  56.     inx    d            ; Point to next character
  57.     jr    ascbcd2
  58. ;
  59. ;         ---- Handle the negative number ----
  60. ; Note that the minus sign must be the first non blank character read
  61. ; If a character has been read that is not a blank then we return 
  62. ; to the user after checking the neg byte which will be set if a previous
  63. ; negative sign was read.
  64. ;
  65. do$neg:
  66.     lda    chr            ; CHAR <> 0 until <> ' ' read.
  67.     ora    a            ; Has a character been read ?
  68.     jrnz    bcd$fin            ; Finish
  69.     mvi    a,'-'
  70.     sta    fneg            ; Save the negative flag.
  71.     sta    chr            ; Save in the last character byte
  72.     jr    ascbcd4            ; Continue on.
  73. ;
  74. ;        ---- End of the conversion ----
  75. ; This is the end of the program. Here the NEG byte is read into the 
  76. ; accumulator and orr'ed to itself to return a 00 = positive else
  77. ; <> 0 for a negative number. Note the DE -> terminating character.
  78. ;
  79. bcd$fin:
  80.     lda    fneg            ; Get the flag
  81.     ora    a            ; Or to itself
  82.     ret
  83. ;
  84.     dseg
  85. fneg    db    00            ; This is the negative flag
  86. chr    db    00            ; Last character read.
  87. ;
  88. ;
  89.     end
  90.  
  91.  
  92.