Hi all,
here is a ftoa procedure , from fasm forum , adapted by me.
demo
====
/*************************************
* Sphinx C-- *
* *
* ftoa demo From FASM forum *
* *
* by Emil_halim *
* *
*************************************/
#pragma option w32c //create Windows console EXE.
#pragma option OS //speed optimization
#jumptomain NONE //just jump to main function
#includepath "D:\Ext_c--\winlib"
#include <windows.h>
#include <MSVCRT.H-->
#pragma option ia //allow inserte asm instructions
#pragma option LST
^^
; #########################################################################
;
;
; #########################################################################
.data
itoa_digits db "0123456789ABCDEF", 0
ftoa_ten dd 10
.code
;===============================================================================
; integer to ASCII conversion procedure
;
; input:
; number - number to convert Smile
; radix - base system like 10 for decimal, 16 for hexadecimal, etc.
; result_buffer - pointer to memory where output will be saved
; signed - bool value, if number is signed (-2147483646...2147483647)
; or unsigned (0...4294967295)
;
; output:
; length of converted string
;
; possible errors:
; radix exceeds 16
;
; coded by Reverend
;===============================================================================
itoa Proc number:DWORD, radix:DWORD, result_buffer:DWORD, sgned:DWORD uses edi esi ebx
local temp_buffer[33]:char
mov esi, radix
lea edi, [#temp_buffer+32]
mov ebx, #itoa_digits
cmp esi, 16
ja error
std
xor al, al
stosb
mov eax, number
cmp sgned, TRUE
jnz @F
test eax, 80000000h
jz @F
neg eax
@@:
xor edx, edx
idiv esi
xchg eax, edx
xlatb
stosb
xchg eax, edx
test eax, eax
jnz @B
lea esi, [edi+1]
mov edi, result_buffer
cld
cmp sgned, TRUE
jnz @F
test number, 80000000h
jz @F
mov al, "-"
stosb
@@:
lodsb
stosb
test al, al
jnz @B
sub edi, result_buffer
lea eax, [edi-1]
stc
@ theend:
cld
ret
@ error:
clc
jmp theend
itoa Endp
;===============================================================================
; float to ASCII conversion procedure
;
; input:
; buffer - pointer to memory where output will be saved
; precision - number of digits after dot
;
; output:
; no immediate output
;
; notes:
; separate integer and mantisa part with dot '.'
; so GOOD 123.456
; WRONG 123,456
;
; coded by Reverend // HTB + RAG
;===============================================================================
ftoa Proc buffer:dword, prec:dword uses eax edi ecx
local status_original:word
local status_changed:word
local integer:dword
local mantisa:dword
local sgned:dword
; --------------------------------
; set correct precision
; --------------------------------
mov eax, prec
cmp eax, 51
jb @F
mov eax, 51
@@:
mov prec, eax
; -----------------------------------------------
; change control wortd of fpu to prevent rounding
; -----------------------------------------------
fnstcw status_original
mov ax, status_original
or ax, 0000110000000000b
mov status_changed, ax
fldcw status_changed
; --------------------------------
; check if sgned
; --------------------------------
xor eax, eax
fst sgned
test sgned, 80000000h
setnz al
mov sgned, eax
; ----------------------------------
; cut integer and mantisa separately
; ----------------------------------
fld st(0)
fld st(0) ; st0 = x, st1 = x
frndint
fist integer ; st0 = x, st1 = x
fabs
fsubp st(1), st(0) ; st0 = mantisa(x)
; --------------------------------
; save integer part in buffer
; --------------------------------
mov edi, buffer
push sgned
push edi
push 10
push integer
call itoa
add edi, eax
mov al, "."
stosb
; --------------------------------
; save mantisa part in buffer
; --------------------------------
mov ecx, prec
dec ecx
@ lop:
fimul DSDWORD[#ftoa_ten]
fld st(0)
frndint
fist mantisa
fsubp st(1), st(0)
push 0
push edi
push 10
push mantisa
call itoa
add edi, eax
ftst
fnstsw ax
test ax, 0100000000000000b
jz @F
test ax, 0000010100000000b
jz fin
@@:
loop lop
fldcw status_original
fimul DSDWORD[#ftoa_ten]
fist mantisa
push 0
push edi
push 10
push mantisa
call itoa
# AL = '\0';
stosb
; --------------------------------
; restore previous values
; --------------------------------
@ fin:
fstp st(0)
stc
ftoa Endp
; #########################################################################
^^
void main()
{
//buffer that holds string
char ss[64];
// test postive value
ST = 55.44;
ftoa(#ss,4);
puts(#ss);
// test nigative value
ST = -55.44;
ftoa(#ss,4);
puts(#ss);
MessageBox(0,"","",0);
}
Enjoy coding with c--,Masm,Basic.