锥体体积未返回正确值

时间:2017-09-20 23:18:50

标签: math assembly x86 irvine32

我的代码正确装配,但它没有返回圆锥的体积。我尝试了其他一些方法让它给我一个圆锥的体积,它并没有给我正确的答案。有什么我想念的吗?

编辑:我修改了我的代码以输出一个数字,但它没有给我正确的数字。

这是我用于此代码的公式: V =(22)(r)(r)(h)/ 21

diviser         DWORD       21
height          WORD        0
radius          BYTE        0                                           ;*** Declare an unsigned, integer variable for the value
product         WORD        0
askRadius       BYTE        "What is the radius?", 0ah, 0dh, 0              ;*** Declare the prompt and message parts
askHeight       BYTE        "What is the height? ", 0
volumeOutput    BYTE        "The volume of the cone is:  ", 0   
lineBreak       BYTE        " ",0dh, 0ah, 0

.code
main PROC

mov ebx, diviser                         ;Initializes the diviser

mov edx,OFFSET askRadius                 ;Asks for Radius
call WriteString
call ReadDec
mov radius, al                           ;moves radius into al (8-bit)
mul radius                               ;Multiplies radius * radius(16-bit)

mov edx,OFFSET askHeight                 ;asks for Height
call writestring
call ReadDec
mov height, dx                           ;moves height into AX
mov WORD PTR product, ax                 ;convert 16-bit into 32-bit
mov WORD PTR product+2, dx               ;converts 16-bit into 32-bit
mul eax                                  ;multiplies by 22
mov edx, 22                              
mul edx
div ebx                                  ;divides by 21

mov edx, OFFSET volumeOutput             
call WriteString
call WriteDec
call WaitMsg
exit

主要ENDP 结束主要

1 个答案:

答案 0 :(得分:2)

这样的事情应该会更好。请注意,您应该选择将计算扩展到64位的位置。

askRadius       BYTE        "What is the radius?", 0ah, 0dh, 0              ;*** Declare the prompt and message parts
askHeight       BYTE        "What is the height? ", 0
volumeOutput    BYTE        "The volume of the cone is:  ", 0   
lineBreak       BYTE        " ",0dh, 0ah, 0

.code
main PROC

mov edx,OFFSET askRadius                 ;Asks for Radius
call WriteString
call ReadDec
imul eax, eax                            ; radius * radius
mov ecx, eax                             ; save for later

mov edx,OFFSET askHeight                 ;asks for Height
call writestring
call ReadDec
imul eax, eax, 22
mul ecx                                  ;multiplies by radius * radius
mov ecx, 21
div ecx                                  ;divides by 21

mov edx, OFFSET volumeOutput             
call WriteString
call WriteDec
call WaitMsg
exit

main ENDP
END main