flat assembler
Message board for the users of flat assembler.
Index
> Main > Floating Point Numbers: convert ascii to floatingpoint, ... |
Author |
|
pelaillo 24 Jul 2004, 16:02
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 |
|||
24 Jul 2004, 16:02 |
|
tango 25 Jul 2004, 08:19
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-) E.g.: variable dt 1.414 give "Extra characters on line" error. No problem with: variable dq 1.414 |
|||
25 Jul 2004, 08:19 |
|
Tomasz Grysztar 25 Jul 2004, 20:07
Sorry, my mistake - it's fixed now.
|
|||
25 Jul 2004, 20:07 |
|
Worr 26 Jul 2004, 12:29
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. |
|||
26 Jul 2004, 12:29 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.