home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / ASM / ALIB30B / CONV24.ASM < prev    next >
Assembly Source File  |  1994-10-31  |  3KB  |  135 lines

  1.     page    66,132
  2. ;******************************** CONV24.ASM *********************************
  3.  
  4. LIBSEG           segment byte public "LIB"
  5.         assume cs:LIBSEG , ds:nothing
  6.  
  7. ;----------------------------------------------------------------------------
  8. .xlist
  9.     include  mac.inc
  10.     include  common.inc
  11. .list
  12. comment 
  13. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -( CONVERT )
  14. DEC_STRZ_TO_DWORD - convert numeric asciiz string to double word value
  15. ;
  16. ;inputs:  ds:si = string ptr (string ends with zero)
  17. ;
  18. ;output:  if no carry, dx,ax = number (ax is low word)
  19. ;         if carry, error occured
  20. ;* * * * * * * * * * * * * *
  21. 
  22. ;-------------------------
  23. str_sign       db    0        ;0=positive 1=neg
  24. ;-------------------------
  25.     public    DEC_STRZ_TO_DWORD
  26. DEC_STRZ_TO_DWORD    PROC    FAR
  27.     mov    cx,07fffh
  28. DEC_STRZ_TO_DWORD    ENDP    
  29. ;
  30. comment 
  31. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -( CONVERT )
  32. DEC_STR_TO_DWORD - convert string to double word
  33. ;
  34. ;inputs:  ds:si = string ptr
  35. ;            cx = string length
  36. ;            
  37. ;output:  if no carry, dx,ax = number (ax is low word)
  38. ;         if carry, error occured
  39. ;* * * * * * * * * * * * * *
  40. 
  41.     public    DEC_STR_TO_DWORD
  42. DEC_STR_TO_DWORD    PROC    FAR
  43.     apush    cx,si,di
  44.     mov    cs:str_sign,0
  45.     XOR    DX,DX          
  46.     XOR    DI,DI
  47.     jcxz    sl_illegal
  48. ;
  49. ; remove any spaces or tabs on front end
  50. ;    
  51. sl_lp1:    LODSB
  52.     dec    cx
  53.     cmp    al,' '
  54.     jne    sl_ck_sign            ;jmp if not space
  55.     jcxz    sl_illegal
  56.     jmp    sl_lp1
  57.     
  58. sl_ck_sign:
  59.         CMP     AL,2Bh    ; '+'   
  60.     JZ    sl_next_chr          
  61.         CMP     AL,2Dh    ; '-'   
  62.     JNZ    sl_end_ck      
  63.     mov    cs:str_sign,1            ;set negative number flag
  64. sl_next_chr:
  65.     LODSB
  66. ;
  67. ; check for end of string by looking for a non numeric character
  68. ;    
  69. sl_end_ck:
  70.     cmp    al,0dh
  71.     je    sl_end_fnd
  72.     cmp    al,' '
  73.     je    sl_end_fnd
  74.     cmp    al,09h
  75.     je    sl_end_fnd
  76.     cmp    al,0
  77.     je    sl_end_fnd
  78.         CMP     AL,30h    ; '0'   
  79.     JB    sl_illegal          
  80.         CMP     AL,39h    ; '9'   
  81.     JA    sl_illegal          
  82. ;
  83. ; convert char. to decimal
  84. ;    
  85.     AND    AX,000Fh
  86.     XCHG    AX,DI
  87. ;
  88. ; dx:ax = sum so far,  di=addition value  operation = dx:ax * 10 + di -> sum
  89. ;
  90.     shl    ax,1            ;sum
  91.     rcl    dx,1            ;  * 2 -> sum
  92.     apush    dx,ax
  93.  
  94.     shl    ax,1
  95.     rcl    dx,1            ;dx,ax = sum * 4
  96.  
  97.     shl    ax,1
  98.     rcl    dx,1            ;dx,ax = sum * 8
  99.  
  100.     pop    bx
  101.     add    ax,bx            ;low(sum*8) + low(sum*2)
  102.     pop    bx
  103.     adc    dx,bx            ;hi(sum*8) + hi(sum*2) = sum*10
  104.  
  105.     XCHG    AX,DI
  106.     
  107.     ADD    DI,AX          
  108.     ADC    DX,+00          
  109.     JS    sl_illegal            ;jmp if overflow
  110.     DEC    CX                ;terminate loop when cx-> -1
  111.     jns    sl_next_chr            ;loop till done
  112.     
  113. sl_end_fnd:
  114.     MOV    AX,DI          
  115.     cmp    cs:str_sign,0
  116.     je    sl_exit2        ;jmp if positive (carry clear)
  117. ;
  118. ; make number negative
  119. ;
  120.     not    dx
  121.     neg    ax
  122.     sbb    dx,-1
  123. sl_exit2:    
  124.     clc    
  125.     jmp    sl_exit              
  126. sl_illegal:
  127.     stc
  128. sl_exit:
  129.     APOP    DI,SI,CX
  130.     RETF              
  131. DEC_STR_TO_DWORD    ENDP
  132.  
  133. LIBSEG    ENDS
  134.     end
  135.