我找到了将float转换为int的代码。
int ftoi(float flt)
{
int i;
_asm
{
mov eax,flt; //loaded mem to acc
rcl eax,1; //left shift acc to remove the sign
mov ebx,eax; //save the acc
mov edx,4278190080; //clear reg edx;
and eax,edx; //and acc to retrieve the exponent
shr eax,24;
sub eax,7fh; //subtract 7fh(127) to get the actual power
mov edx,eax; //save acc val power
mov eax,ebx; //retrieve from ebx
rcl eax,8; //trim the left 8 bits that contain the power
mov ebx,eax; //store
mov ecx, 1fh; //subtract 17 h
sub ecx,edx;
mov edx,00000000h;
cmp ecx,0;
je loop2;
shr eax,1;
or eax,80000000h;
loop1:
shr eax,1; //shift (total bits - power bits);
sub ecx,1;
add edx,1;
cmp ecx,0;
ja loop1;
loop2:
mov i, eax;
//check sign +/-
sign:
mov eax,flt;
and eax,80000000h;
cmp eax,80000000h;
je putsign;
}
return i;
putsign:
return -i;
}
您是否认为可以编辑此代码以将32 Int转换为float?
如果可以的话,你可以给我一些建议怎么做?
非常感谢你提出的所有建议和答案。
答案 0 :(得分:0)
我的建议:
#include <stdio.h>
float itof (int i)
{
float flt = 0.0;
_asm
{
mov eax, i
bt eax, 31
jnc plus
rcr [flt], 1
neg eax
plus:
mov edx, 0x7F // Bias
bsr ebx, eax
mov ecx, 23
cmp ebx, ecx
jbe fit
// too big:
sub bl, cl
mov cl, bl
shr eax, cl
setc cl // to round or not to round
add eax, ecx
add edx, ebx
mov cl, 23
fit:
bsr ebx, eax
btr eax, ebx
sub cl, bl
shl eax, cl
or [flt], eax
add ebx, edx
shl ebx, 23
or [flt], ebx
}
return flt;
}
int main ( void )
{
float flt;
int i;
i = 12345678;
// i = -0x8888888; // different results, both are right
flt = (float)i;
printf("%i %f\n",i,flt);
flt = itof (i);
printf("%i %f\n",i,flt);
return 0;
}