home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ENTERPRS / CPM / UTILS / S / ZCNFG21.LZH / CFGLIB.LZH / RADBIN.Z80 < prev    next >
Text File  |  1992-11-03  |  2KB  |  46 lines

  1.     ext    mpy16,sdelm
  2.     public    rten1,radbin
  3.  
  4. ; RTEN1 performs decimal conversion of the string at HL.
  5.  
  6. rten1:    ld    de,10        ; Decimal radix base
  7.                 ; Fall through to generalized
  8.                 ; ..radix conversion routine
  9.  
  10. ; This routine converts the string pointed to by HL using the radix
  11. ; passed in DE.  If the conversion is successful, the value is returned in BC.
  12. ; HL points to the character that terminated the number, and A contains that
  13. ; character.  If an invalid character is encountered, the routine returns with
  14. ; the carry flag set, and HL points to the offending character.
  15.  
  16. radbin:    ld    bc,0        ; Initialize result
  17. rdx1:    or    a        ; Make sure carry is reset
  18.     call    sdelm        ; Test for delimiter (returns Z if delimiter)
  19.     ret    z        ; Return if delimiter encountered
  20.  
  21.     sub    '0'        ; See if less than '0'
  22.     ret    c        ; Return with carry set if so
  23.     cp    10        ; See if in range '0'..'9'
  24.     jr    c,rdx2        ; Branch if it is valid
  25.     cp    'A'-'0'        ; Bad character if < 'A'
  26.     ret    c        ; ..so we return with carry set
  27.     sub    7        ; Convert to range 10..15
  28. rdx2:
  29.     cp    e        ; Compare to radix in E
  30.     ccf            ; Carry should be set; this will clear it
  31.     ret    c        ; If carry now set, we have an error
  32.  
  33.     inc    hl        ; Point to next character
  34.     push    bc        ; Push the result we are forming onto the stack
  35.     ex    (sp),hl        ; Now HL=result, (sp)=source pointer
  36.     call    mpy16        ; HLBC = previous$result * radix
  37.     ld    h,0        ; Discard high 16 bits and
  38.     ld    l,a        ; ..move current digit into HL
  39.     add    hl,bc        ; Form new result
  40.     ld    c,l        ; Move it into BC
  41.     ld    b,h
  42.     pop    hl        ; Get string pointer back
  43.     jr    rdx1        ; Loop until delimiter
  44.  
  45.     end
  46.