rea*_*l_G 1 x86 assembly fixed-point
我是 Assembly 的新手,正在学习定点算法。
AX 是一个 16 位寄存器-
MUL Pi ; Multiplies EAX with Pi and stores result in EAX
DIV 256 ; Divides EAX by 256 which equals the necessary right- shift for the 8,8 format
Run Code Online (Sandbox Code Playgroud)
但我不认为它是这样工作的。
一个MUL用字指令操作数的大小相乘AX与操作数,并将其结果DX:AX。如果两个操作数都是 8.8 格式,则结果将是 16.16 格式,整数部分在 中DX,小数部分在 中AX。将其转换为8.8格式,你把内容DL和AH这样的:
MUL Pi ; multiple by Pi, leaving the integral part in DX and the fraction in AX
MOV AL, AH ; move the fractional part into place
MOV AH, DL ; move the integral part into place
Run Code Online (Sandbox Code Playgroud)
一切都应该很好。请注意,四舍五入可能略有偏差。您可以像这样更正:
MUL Pi ; multiple by Pi, leaving the integral part in DX and the fraction in AX
ADD AX, 0080h ; apply rounding to AH
ADC DX, 0 ; apply carry if any
MOV AL, AH ; move the fractional part into place
MOV AH, DL ; move the integral part into place
Run Code Online (Sandbox Code Playgroud)
这根据原则正确舍入结果
round(x) = ?x + 0.5?
Run Code Online (Sandbox Code Playgroud)