如何在汇编中div后显示11.1?
如何在div
后面显示11.1?例如32/5=6.4,我只能显示6,那么如何显示6.4呢?
OUT23:
MOV BL,CNT ;CNT is the total number of even number
DIV BL
MOV DL,AL
MOV BH,AL
MOV AH,09H
LEA DX,MSG23
INT 21H
MOV DL,BH ;print out the average, only the integer part able to display
MOV AH,02
ADD DL,30H
INT 21H
How to display 11.1 after the div
? E.g 32/5=6.4, I am only able to display 6 so how to display 6.4?
OUT23:
MOV BL,CNT ;CNT is the total number of even number
DIV BL
MOV DL,AL
MOV BH,AL
MOV AH,09H
LEA DX,MSG23
INT 21H
MOV DL,BH ;print out the average, only the integer part able to display
MOV AH,02
ADD DL,30H
INT 21H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想将两个整数相除并打印小数结果,那么您可以像小学中用手除法那样进行操作。初始除法将为您提供结果的整数部分,然后将余数部分重复乘以 10 并除以除数,直到它为零或您已达到获得分数的预期精度
以
32/5 为例
上面整数部分:
小数部分:
更复杂的除法也可以像上面那样进行,只需在每一步之后将余数乘以10即可。如果结果是无限的,那么当你获得足够的精度时停止。例如:25/11 = 2.27272727...
当然可以通过乘以 10N 一次获得 N 个小数位来更快地完成
或者使用浮点值并进行浮点除法像其他人所说的
FDIV
。之后,有多种方法可以将浮点结果转换为字符串,但是获得正确舍入的结果将非常困难,因此最好仅使用为此目的而制作的库。然而,对于非常简单的演示,也可以使用上述方法:
。
粗略的例子是这样的,忽略由于二进制浮点属性引起的误差
更详细的信息可以在 将浮点数转为字符串
If you want to divide two integers and print the fractional result then you can do it like when you divide by hand in elementary school. The initial division will give you the integer part of the result, then just repeatedly multiply the remainder part by 10 and divide by the divisor until it's zero or you have reached the intended precision to get the fraction
Take the example
32/5
aboveInteger part:
Fractional part:
More complex divisions can also be done like above, just multiply the remainder by 10 after each step. If the result is infinite then stop when you get enough precision. For example: 25/11 = 2.27272727...
Of course it can be done even faster by multiplying 10N to get N fractional digits at once
Alternatively use floating-point values and do a floating-point division with
FDIV
like others said. After that there are numerous ways to convert the float result into string, but it'll be exceedingly difficult to get a correctly rounded result, so it's better to just use libraries made for that purpose.However for a very simple demonstration then again the above method can also be used:
.
A rough example is like this, disregarding errors due to binary floating-point properties
More detailed information can be found in Turn float into string
DIV
是整数除法指令。如果您想要浮点除法,那么您需要查看FDIV
。DIV
is an integer divide instruction. If you want floating point division then you'll need to look atFDIV
.如前所述,
DIV
执行整数除法。由于它是整数除法,因此所得的商和余数可在单独的寄存器对中使用,确切的寄存器取决于除数的大小。1 字节除数
2 字节除数
4 字节除数
由于您使用的是 1 字节除数“BL”,余数将位于 AH 寄存器中。在
32/5
的情况下,AL
寄存器将包含值6
,AH
寄存器将包含值 < code>2 这意味着2/5
的剩余部分是0.4
,为您提供答案6.4
。如果您决定使用 FPU 操作,则需要使用
FLD
指令将两个值32
和5
加载到 FPU 堆栈上,一次用于每个值,然后调用 FDIV,它将从 FPU 堆栈中弹出两个值,将它们相除并将结果推送到 FPU 堆栈上。然后可以使用 FSTP 指令将结果从堆栈移至内存地址。获得结果后,您需要将其从二进制 IEEE 浮点表示形式转换为可以在屏幕上显示的字符串表示形式。
As already stated
DIV
performs an integer division. Since it is an integer division, the resulting quotient and remainder are available in separate register pairs, the exact registers depends on the size of the divisor.1 Byte Divisor
2 Byte Divisor
4 Byte Divisor
Since you are using a 1 byte divisor 'BL' the remainder will be in the AH register. In your case of
32/5
theAL
register will contain the value6
and theAH
register the value2
which means there is a remainder of2/5
which is0.4
giving you your answer of6.4
.If you decide you use FPU operations you will need to load the two values
32
and5
onto the FPU stack using theFLD
instruction, once for each value, after which you callFDIV
which will pop the two values off the FPU stack, divide them and push the result onto the FPU stack.The result can then be move off the stack to a memory address using the
FSTP
instruction. Once you have the result you will need to convert that from the binary IEEE floating-point representation to a string representation which can be displayed on the screen.