如何在汇编中div后显示11.1?

发布于 2024-10-17 00:57:58 字数 356 浏览 3 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍白女子 2024-10-24 00:57:58

如果您想将两个整数相除并打印小数结果,那么您可以像小学中用手除法那样进行操作。初始除法将为您提供结果的整数部分,然后将余数部分重复乘以 10 并除以除数,直到它为零或您已达到获得分数的预期精度

32/5 为例 上面

  • 整数部分:

    32/5 = 6 → 打印出“6”。
    
  • 小数部分:

    余数:2,乘以10 → 2*10 = 20
    20/5 = 4 → 打印出“4”
    余数:0,到此为止
    

更复杂的除法也可以像上面那样进行,只需在每一步之后将余数乘以10即可。如果结果是无限的,那么当你获得足够的精度时停止。例如:25/11 = 2.27272727...

  25/11 = 2 R 3 → 2.
3*10/11 = 2 R 8 → 2
8*10/11 = 7 R 3 → 7
3*10/11 = 2 R 8 → 2
8*10/11 = 7 R 3 → 7
and so on...

当然可以通过乘以 10N 一次获得 N 个小数位来更快地完成


或者使用浮点值并进行浮点除法像其他人所说的 FDIV 。之后,有多种方法可以将浮点结果转换为字符串,但是获得正确舍入的结果将非常困难,因此最好仅使用为此目的而制作的库。

然而,对于非常简单的演示,也可以使用上述方法:

  • 拆分结果的整数部分并打印它,然后打印
  • 将整数部分乘以10,整数部分将是下一个小数位
  • 删除整数部分并重复上述步骤,直到达到所需的精度

粗略的例子是这样的,忽略由于二进制浮点属性引起的误差

11.157
Int part: 11    → print 11.
Fractional part:
0.157*10 = 1.57 → print 1
 0.57*10 = 5.7  → print 5
  0.7*10 = 7    → print 7

更详细的信息可以在 将浮点数转为字符串

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 above

  • Integer part:

    32/5 = 6 → Print out "6."
    
  • Fractional part:

    Remainder: 2, multiply it by 10 → 2*10 = 20
    20/5 = 4 → Print out "4"
    Remainder: 0, stop here
    

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...

  25/11 = 2 R 3 → 2.
3*10/11 = 2 R 8 → 2
8*10/11 = 7 R 3 → 7
3*10/11 = 2 R 8 → 2
8*10/11 = 7 R 3 → 7
and so on...

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:

  • Split the integer part of the result and print it followed by .
  • Multiply the integer part by 10, the integer part will be the next fractional digit
  • Remove the the integer part and repeat the above step until you reach the desired precision

A rough example is like this, disregarding errors due to binary floating-point properties

11.157
Int part: 11    → print 11.
Fractional part:
0.157*10 = 1.57 → print 1
 0.57*10 = 5.7  → print 5
  0.7*10 = 7    → print 7

More detailed information can be found in Turn float into string

彼岸花ソ最美的依靠 2024-10-24 00:57:58

DIV 是整数除法指令。如果您想要浮点除法,那么您需要查看FDIV

DIV is an integer divide instruction. If you want floating point division then you'll need to look at FDIV.

紙鸢 2024-10-24 00:57:58

如前所述,DIV 执行整数除法。由于它是整数除法,因此所得的商和余数可在单独的寄存器对中使用,确切的寄存器取决于除数的大小。

1 字节除数

AX - Dividend
AH - Remainder
AL - Quotient

2 字节除数

DX:AX - Dividend
DX - Remainder
AX - Quotient

4 字节除数

EDX:EAX - Dividend
EDX - Remainder
EAX - Quotient

由于您使用的是 1 字节除数“BL”,余数将位于 AH 寄存器中。在 32/5 的情况下,AL 寄存器将包含值 6AH 寄存器将包含值 < code>2 这意味着 2/5 的剩余部分是 0.4,为您提供答案 6.4

如果您决定使用 FPU 操作,则需要使用 FLD 指令将两个值 325 加载到 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

AX - Dividend
AH - Remainder
AL - Quotient

2 Byte Divisor

DX:AX - Dividend
DX - Remainder
AX - Quotient

4 Byte Divisor

EDX:EAX - Dividend
EDX - Remainder
EAX - Quotient

Since you are using a 1 byte divisor 'BL' the remainder will be in the AH register. In your case of 32/5 the AL register will contain the value 6 and the AH register the value 2 which means there is a remainder of 2/5 which is 0.4 giving you your answer of 6.4.

If you decide you use FPU operations you will need to load the two values 32 and 5 onto the FPU stack using the FLD instruction, once for each value, after which you call FDIV 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文