home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / prog / realasm1 / st2int.asm < prev   
Encoding:
Assembly Source File  |  1993-07-18  |  2.6 KB  |  72 lines

  1. .286
  2. ;================================================
  3. ; invoke st2int, string
  4. ;
  5. ; Convert an ascii string to an INTEGER
  6. ; where string is '+(or -)integers',0
  7. ;
  8. ; Returns AX = integer.
  9. ;         AX = 0, if word overflow
  10. ;------------------------------------------------
  11. cseg          segment word public 'code'
  12.               assume  cs:cseg,ss:cseg
  13.               assume  ds:cseg,es:cseg
  14.  
  15.               include math.inc
  16.  
  17. st2int        proc    near uses bx cx dx si di, string:NPB
  18.  
  19.               mov     si, string                ;
  20.               xor     cx, cx                    ;
  21.  
  22.               .WHILE (1)                        ; goto string end
  23.                  .BREAK .IF (byte ptr [si] == 0);
  24.                  inc     si                     ;
  25.                  inc     cx                     ;
  26.               .ENDW                             ;
  27.               dec     si                        ;
  28.               std                               ; reverse direction
  29.  
  30.               .WHILE (cx)                       ; find numerals
  31.                  lodsb                          ;
  32.                  inc     si                     ;
  33.                  .BREAK .IF ((al >= '0') && (al <= '9'))
  34.                  dec     si                     ;
  35.                  dec     cx                     ;
  36.               .ENDW                             ;
  37.  
  38.               mov     di, 1                     ;
  39.               xor     bx, bx                    ;
  40.               .WHILE (cx)                       ;
  41.                  lodsb
  42.                  .BREAK .IF ((al <= '0') || (al >= '9'))
  43.                  xor     ah, ah                 ;
  44.                  sub     al, '0'                ; AX = binary
  45.                  mul     di                     ; AX *= DI
  46.                  add     bx, ax                 ; add to result
  47.  
  48.                  .IF (CARRY?)                   ; if overflow
  49.                     xor     ax, ax              ; AX = 0
  50.                     jmp     exit                ;
  51.                  .ENDIF                         ;
  52.  
  53.                  mov     ax, di                 ; next power of 10
  54.                  mov     dx, 10                 ;
  55.                  mul     dx                     ;
  56.                  mov     di, ax                 ;
  57.  
  58.                  dec     cx                     ;
  59.               .ENDW                             ;
  60.  
  61.               .IF (al == '-')                   ;
  62.                  neg     bx                     ;
  63.               .ENDIF                            ;
  64.  
  65.               mov     ax, bx
  66. exit:
  67.               ret
  68. st2int        endp
  69.  
  70. cseg          ends
  71.               end
  72.