News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Extended VAL

Started by Ficko, December 18, 2009, 05:41:26 PM

Previous topic - Next topic

Ficko

I had to come up with a VAL proc that can handle decimal and scientific notation addition to uderstand the US decimal point and EU coma separation and signs.

I thought it may can be interesting and any bug reports are appreciated. :wink

Input can be like: "1","+1","-1","1.1","1,1","3,14e-003"

The result is a Real8 in eax:edx pair.


.686p
.model flat
; ===========================================================================
.code
; =============== S U B R O U T I N E =======================================
align 16
VAL proc near public
  push esi
  mov  esi, [esp+8]     ;ESI = Input string
  xor  eax, eax
  xor  edx, edx
  xor  ecx, ecx
  push ecx
  push 10
  fild word ptr [esp]
  fldz
@@:  lodsb
   cmp  al, ' '
  jbe  @B
  cmp  al, '0'
  .if (SIGN?)
   .if (al == '-')
    inc dl       ;DL (bit 0) = Sign for the number
   .elseif (al != '+')
    jmp @F   
   .endif
   inc esi
  .endif
  dec esi
C0:  lodsb
@@:  .if (al != 0)
   sub  al, '0'
   .if (SIGN?)
    .if (al == '.' - '0') || (al == ',' - '0')
     .if (dh != 0)
      jmp Error
     .endif
     inc dh      ;DH = DECPOINT
     jmp C0
    .endif
    .if (al == '-' - '0') && !(ah & 2)
     or  dl, 2     ;DL (bit 1) = Sign for the exponent
     mov ah, 2     ;AH (bit 1) = EXPONENT + Sign for the exponent found
    .elseif (al == '+' - '0') && !(ah & 2)
     mov ah, 2
    .else
     jmp Error
    .endif
    jmp C0
   .elseif (al <= '9' - '0')
    .if (ah == 2)
     imul ecx,ecx,10    ;ECX = Number after EXPONENT
     add cl, al
     jmp C0
    .elseif (ah != 0)
     jmp Error
    .endif
    .if (dh != 0)
     dec dword ptr [esp+4]  ;[esp-4] EXPONENT count (after decpoint)
    .endif
    mov  [esp], al
    fmul st(0), st(1)
    fild  word ptr [esp]
    faddp st(1), st(0)   
    jmp C0
   .endif
   .if (al == 'e' - '0') || (al == 'E' - '0')
    .if (ah == 0)
     mov ah, 1     ;AH (bit 0) = EXPONENT found
     jmp C0
    .endif
   .endif   
  .endif
Error: .if (dl & 2)
   neg ecx
  .endif
  add ecx, [esp+4]
  .if (!ZERO?)
   .if (sdword ptr ecx < 0)
    neg ecx
    .repeat
     fdiv st(0), st(1)
    .untilcxz
   .else
    .repeat
     fmul st(0), st(1)
    .untilcxz
   .endif
  .endif
  .if (dl & 1)
   fchs
  .endif
@@:  fstp qword ptr [esp]
  pop eax
  pop edx
  ffreep st(0)
  pop esi
  retn 4
VAL endp
end