AX上的MUL操作与(8.8)格式的Pi(31415.)在Assembly中如何工作



我是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

但我不认为它是这样工作的。

具有字大小操作数的MUL指令将AX与该操作数相乘,并将结果放入DX:AX。如果两个操作数都是8.8格式,则结果将是16.16格式,整数部分在DX中,小数部分在AX中。要将其转换为8.8格式,可以将DLAH的内容如下所示:
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

一切都会好起来的。请注意,舍入可能有点偏离。你可以这样纠正:

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

这根据原理正确地对结果进行四舍五入

round(x) = ⌊x + 0.5⌋

相关内容