home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TRYHEAP.ZIP / HTOL.ASM < prev    next >
Assembly Source File  |  1987-12-11  |  2KB  |  84 lines

  1.     name    HTOL
  2.     page    55,132
  3.     title    HTOL - Hex ASCII to long integer
  4.  
  5. ;
  6. ; HTOL.ASM - convert hexadecimal ASCII string 
  7. ;            to long (32-bit) integer.
  8. ;
  9. ; (C) 1987 Ziff Davis, by Ray Duncan
  10. ; Call with:    DS:SI = address of string
  11. ;
  12. ; Returns:    DX:AX = result (high word in DX)
  13. ;        DS:SI = address+1 of terminator 
  14. ;
  15. ;               other registers preserved
  16. ;
  17. ; Like the C library 'atol', this routine gives no 
  18. ; warning in the event of overflow, and terminates 
  19. ; on the first unconvertable character.
  20. ;
  21.  
  22. _TEXT    segment    word public 'CODE'
  23.  
  24.     assume    cs:_TEXT
  25.  
  26.     public    htol            ; make HTOL available
  27.                     ; to the Linker
  28.  
  29. htol    proc    near                 ; Convert hex ASCII string
  30.                     ; to 32-bit binary integer.
  31.  
  32.     push    cx            ; save register
  33.         xor     cx,cx               ; set forming answer to zero
  34.         xor     dx,dx
  35.  
  36. htol1:    lodsb                       ; get next char., make sure
  37.                     ; it is '0'-'9' or 'A'-'F'
  38.  
  39.         cmp     al,'0'
  40.         jb      htol3                ; exit if char < '0'
  41.  
  42.     cmp    al,'9'
  43.     jbe    htol2            ; proceed if char '0'-'9'
  44.  
  45.     or    al,20h            ; fold char. to lower case.
  46.  
  47.     cmp    al,'f'
  48.     ja    htol3            ; exit if > 'F' or 'f'
  49.  
  50.     cmp    al,'a'
  51.     jb    htol3            ; exit if < 'A' or 'a'
  52.  
  53.     add    al,9            ; else add fudge factor
  54.                     ; for digits 'A'-'F'
  55.  
  56. htol2:                    ; add this digit to
  57.                     ; forming answer...
  58.  
  59.     shl     cx,1            ; first shift current answer 
  60.         rcl     dx,1                ; left by 4 bits...
  61.         shl     cx,1
  62.         rcl     dx,1            
  63.         shl     cx,1
  64.         rcl     dx,1            
  65.         shl     cx,1
  66.         rcl     dx,1            
  67.         and     ax,0fh              ; isolate binary value 0-A
  68.                                     ; from ASCII character code,
  69.         or     cx,ax               ; add to the forming answer.
  70.  
  71.         jmp     htol1                ; get next character
  72.  
  73. htol3:    mov    ax,cx            ; return DX:AX = value
  74.     pop    cx            ; restore register
  75.         ret                         ; back to caller
  76.  
  77. htol    endp
  78.  
  79. _TEXT    ends
  80.  
  81.     end
  82.  
  83.