flat assembler
Message board for the users of flat assembler.

Index > Main > Floating Point Numbers: convert ascii to floatingpoint, ...

Author
Thread Post new topic Reply to topic
Worr



Joined: 23 Jul 2004
Posts: 5
Location: Ohio, USofA
Worr
I have recently gone from HLA to FASM, and decided to work with the FPU, but have run into some problems. I was able to place a floating point value into an 80-bit variable when I declared the variable, but how would I be able to otherwise declare a floating point number. I have made a Float2Str procedure and am going to make a Str2Float procedure also, which I could probably use in order to set a floating point value, although it would probably be very unefficient. Also, a friend of mine downloaded the latest version of FASM and was unable to set the value of a 80-bit variable upon declaring it, and instead gave him a "Extra charactars on line" error if I remeber if I correctly. Are you supposed to be able to set the value that way, or not?
Post 24 Jul 2004, 15:03
View user's profile Send private message Visit poster's website AIM Address Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo
Hope it helps you.
Code:
; -------------------------------------
;  AsciiToFp    st0 <-- esi
;    by Tim Roberts
;    last modified by pelaillo
;      December 30 2002
;  Improvements:
;    Fasm syntax
;    Zero when empty string
;    Discard after second decimal point
;    Small optimizations
; -------------------------------------

  str_tofp:
 push ebx esi edi
    xor eax,eax
 mov [.sign],eax
     mov [.expsign],eax
  mov [.decimal],eax
  movzx eax,byte [esi]
        cmp al,'-'
        je .minus_sign
      cmp al,'+'
        je .plus_sign
       jmp .sign_ok
  .minus_sign:
  inc [.sign]
  .plus_sign:
    inc esi
     movzx eax,byte [esi]
  .sign_ok:
     xor ebx,ebx
 fldz
        xor ecx,ecx
  .mantissa_loop:
        cmp eax,'E'
       je .exponent
        cmp eax,'e'
       je .exponent
        cmp eax,'.'
       je .dec_point
       xor eax,'0'
       cmp eax,9
   ja .digits_ok
       mov [.temp],eax
     fmul [.ten]
 fiadd   word [.temp]
        inc ebx
     jmp .no_point
  .dec_point:
  cmp     [.decimal],0
        jne     .digits_ok
  mov [.decimal],ebx
  .no_point:
      inc esi
     movzx eax,byte [esi]
        jmp .mantissa_loop
  .exponent:
      inc esi
     movzx eax,byte [esi]
        cmp al,'-'
        je  .minus_exponent_sign
    cmp al,'+'
        je  .plus_exponent_sign
     jmp .exponent_loop
  .minus_exponent_sign:
   inc [.expsign]
  .plus_exponent_sign:
        inc esi
     movzx eax,byte [esi]
  .exponent_loop:
       xor eax,'0'
       cmp eax,9
   ja  .digits_ok
      lea     ecx,[ecx*4+ecx]
     lea     ecx,[ecx*2+eax]
     inc esi
     movzx eax,byte [esi]
        jmp .exponent_loop
  .digits_ok:
     cmp [.expsign],0
    je  .exponent_ready
 neg ecx
  .exponent_ready:
   mov eax,[.decimal]
  or  eax,eax
 je  .no_decimals
    sub ebx,eax
 sub ecx,ebx
  .no_decimals:
  or ecx,ecx
  je .no_exponent
     mov eax,ecx
;       call    fexp10
  .fexp10:
;    mov     ecx,eax
    cmp eax,0
    jge .fexp_sign_ok
    neg eax
  .fexp_sign_ok:
    fld1
    mov dl,al
    and edx,0Fh
    je .bigger_exp
    lea edx,dword [edx+edx*4]
    fld tword [edx*2+.ten_1-10]
    fmulp st1,st
  .bigger_exp:
    mov dl,al
    shr dl,4
    and edx,0Fh
    je .still_bigger
    lea edx,dword [edx+edx*4]
    fld tword [edx*2+.ten_16-10]
    fmulp st1,st
  .still_bigger:
    mov dl,ah
    and edx,1Fh
    je .exponent_sign
    lea edx,dword [edx+edx*4]
    fld tword [edx*2+.ten_256-10]
    fmulp st1,st
  .exponent_sign:
    cmp ecx,0
    jge .positive_exponent
    fdivp st1,st
    jmp .no_exponent
  .positive_exponent:
    fmulp st1,st
  .no_exponent:
        cmp [.sign],0
       je .ready
   fchs
  .ready:
       pop edi esi ebx
     mov edi,esi
 call fp_tohex
       ret
  align 4
  .sign dd 0
  .expsign dd 0
  .decimal dd 0
  .temp dd 0
  .ten dq 10.0
  .ten_1 dt 1.0e1
        dt 1.0e2
    dt 1.0e3
    dt 1.0e4
    dt 1.0e5
    dt 1.0e6
    dt 1.0e7
    dt 1.0e8
    dt 1.0e9
    dt 1.0e10
   dt 1.0e11
   dt 1.0e12
   dt 1.0e13
   dt 1.0e14
   dt 1.0e15
  .ten_16 dt 1.0e16
     dt 1.0e32
   dt 1.0e48
   dt 1.0e64
   dt 1.0e80
   dt 1.0e96
   dt 1.0e112
          dt 1.0e128
          dt 1.0e144
          dt 1.0e160
          dt 1.0e176
          dt 1.0e192
          dt 1.0e208
          dt 1.0e224
          dt 1.0e240
  .ten_256 dt 1.0e256
          dt 1.0e512
          dt 1.0e768
          dt 1.0e1024
         dt 1.0e1280
         dt 1.0e1536
         dt 1.0e1792
         dt 1.0e2048
         dt 1.0e2304
         dt 1.0e2560
         dt 1.0e2816
         dt 1.0e3072
         dt 1.0e3328
         dt 1.0e3584
         dt 1.0e4096
         dt 1.0e4352
         dt 1.0e4608
         dt 1.0e4864

; Float point to hex:
; edi <-- st0

fp_tohex:
    push esi edi
        mov esi,.fpnum
      fstp tword [esi]
    mov     ecx,9
  .conv_loop:
  mov     al,[esi+ecx]
        shr     al,4
        call .HexOut
        mov     al,[esi+ecx]
        and     al,0Fh
      call .HexOut
        dec     ecx
 jns     .conv_loop
  mov     byte [edi],0
        pop edi esi
 ret
  .HexOut:
       cmp al,9
    jbe .is_num
 add al,'A'-10
     jmp .ok_num
  .is_num:
       add     al,'0'
  .ok_num:
  stosb
       ret
  .fpnum dt 0.0

    
Post 24 Jul 2004, 16:02
View user's profile Send private message Yahoo Messenger Reply with quote
tango



Joined: 05 Apr 2004
Posts: 5
Location: Italy
tango
Worr wrote:
(..snip..) Also, a friend of mine downloaded the latest version of FASM and was unable to set the value of a 80-bit variable upon declaring it, and instead gave him a "Extra charactars on line" error if I remeber if I correctly. Are you supposed to be able to set the value that way, or not?


Same problem here.. (with last version of FASM -22 jul-) Sad
E.g.:

variable dt 1.414

give "Extra characters on line" error.

No problem with:

variable dq 1.414
Question
Post 25 Jul 2004, 08:19
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7755
Location: Kraków, Poland
Tomasz Grysztar
Sorry, my mistake - it's fixed now.
Post 25 Jul 2004, 20:07
View user's profile Send private message Visit poster's website Reply with quote
Worr



Joined: 23 Jul 2004
Posts: 5
Location: Ohio, USofA
Worr
Thanks for fixing that.

Although your function won't work with the way I have my numbers in strings, it did give me an idea as to how to finish mine, so it is of great help, although I'm gonna get some more sleep for now; woke up early to golfing but it's raining. Crying or Very sad
Post 26 Jul 2004, 12:29
View user's profile Send private message Visit poster's website AIM Address Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.