以补码存储
十进制&二进制转换,表格法
如,123D -> 01111011B
单精度浮点存储
符号位(1 Sign):0正,1负;
指数位(8 Exponent):需要加偏置值Bias,127 + 指数值;
尾数部分(23 Mantissa):弃小数点前那个恒1并在右端以0补全位数;
如,1234.625F -> 100 1101 0010.101 -> 1.00 1101 0010 101 * 2^10
Sign 0
Exponent 10,127+10 -> 1000 1001
Mantissa 0011 0100 1010 1000 0000 000
0 1000 1001 0011 0100 1010 1000 0000 000 -> 449A5400
内存中小尾存储,00 54 9A 44
双精度浮点存储
符号位(1 Sign):0正,1负;
指数位(11 Exponent):需要加偏置值Bias,1023 + 指数值;
尾数部分(52 Mantissa):弃小数点前那个恒1并在右端以0补全位数;
如,1234.625 -> 100 1101 0010.101 -> 1.00 1101 0010 101 * 2^10
Sign 0
Exponent 10,1023+10 -> 100 0000 1001
Mantissa 0011 0100 1010 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000
0 100 0000 1001 0011 0100 1010 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 -> 40934A8000000000
内存中小尾存储,00 00 00 00 80 4A 93 40
浮点指令
.386
.model flat, stdcall ;32 bit memory model
option casemap :none ;case sensitive
.code
FloatCase proc
mov ebp, esp
sub esp, 5ch
push ebx
push esi
push edi
lea edi, [ebp - 5ch]
mov ecx, 17h
mov eax, 0cccccccch
rep stos dword ptr [edi]
mov dword ptr [ebp - 4], 43000000h //128.0
mov dword ptr [ebp - 8], 40800000h //4.0
mov dword ptr [ebp - 0ch], 40000000h //2.0
mov dword ptr [ebp - 10h], 42000000h //32.0
mov dword ptr [ebp - 14h], 41000000h //8.0
mov dword ptr [ebp - 18h], 0
mov dword ptr [ebp - 1ch], 42b00000h //88.0
fld dword ptr [ebp - 4] //st0 = 128.0
fmul dword ptr [ebp - 8] //st0 = 128.0 * 4.0 = 512.0
fdiv dword ptr [ebp - 0ch] //st0 = 512.0 / 2.0 = 256.0
fst dword ptr [ebp - 4] //[ebp - 4] = st0 = 256.0
fdiv dword ptr [ebp - 4] //st0 = 256.0 / 256.0 = 1.0
fmul dword ptr [ebp - 10h] //st0 = 1.0 * 32.0 = 32.0
fdiv dword ptr [ebp - 14h] //st0 = 32.0 / 8.0 = 4.0
fst dword ptr [ebp - 10h] //[ebp - 10h] = st0 = 4.0
fadd dword ptr [ebp - 1ch] //st0 = 4.0 + 88.0 = 92.0
fst dword ptr [ebp - 10h] //[ebp - 10h] = st0 = 92.0
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret
FloatCase endp
start:
invoke FloatCase
ret
end start
计算出浮点值:
43000000h -> 0100 0011 0000 0000 0000 0000 0000 0000
符号:0正数
指数:1000 0110,128+6-127 -> 7
1.0 * 10^7 -> 1000 0000 -> 128.0f
40800000h -> 0100 0000 1000 0000 0000 0000 0000 0000
符号:0正数
指数:1000 0001,128+1-127 -> 2
1.0 * 10^2 -> 100 -> 4.0f
40000000h -> 0100 0000 0000 0000 0000 0000 0000 0000
符号:0正数
指数:1000 0000,128-127 -> 1
1.0 * 10^1 -> 10 -> 2.0f
42000000h -> 0100 0010 0000 0000 0000 0000 0000 0000
符号:0正数
指数:1000 0100,128+4-127 -> 5
1.0 * 10^5 -> 100000 -> 32.0f
41000000h -> 0100 0001 0000 0000 0000 0000 0000 0000
符号:0正数
指数:1000 0010,128+2-127 -> 3
1.0 * 10^3 -> 1000 -> 8.0f
42b00000h -> 0100 0010 1011 0000 0000 0000 0000 0000
符号:0正数
指数:1000 0101,128+5-127 -> 6
1.011 * 10^6 -> 1011000 -> 88.0f
st0 = 92.0