0

我这里有我的代码,但我不知道如何声明、输入和输出浮点型数字。在 c 中,%f 用于浮点数。我在我的代码中尝试过,但它不起作用。我也试过 REAL 8。这是我的代码。我知道 %d 是整数,但是当我使用 %f 时,输出是 0.000000 我无法输出确切的答案。

.386
.model flat, c
.stack 100h


printf      PROTO arg1:Ptr byte, printlist:VARARG
scanf       PROTO arg2:Ptr byte, inputlist:VARARG

.data
in1fmt      byte "%d" , 0
msg1fmt     byte 0Ah, "%s" , 0
msg2fmt     byte 0Ah, 0Ah, "%s" , 0
msg3fmt     byte 0Ah,"%s%d" , 0Ah, 0Ah, 0
msg4fmt     byte 0Ah,"%s%d" , 0Ah, 0Ah, 0Ah, 0Ah, 0
msg5fmt     byte 0Ah, "%s%d" , 0Ah, 0Ah,  0
msg1        byte "Enter any number: " , 0
msg2        byte "Enter another number: " , 0
msg3        byte "The sum is: " , 0
msg4        byte "The product is: " , 0
msg5        byte "The largest is: ", 0

num1        sdword ?
num2        sdword ?
sum         sdword ?
product     sdword ?
largest     sdword ?

.code
main        proc
INVOKE printf, ADDR msg1fmt, ADDR msg1
INVOKE scanf, ADDR in1fmt, ADDR num1
INVOKE printf, ADDR msg2fmt, ADDR msg2
INVOKE scanf, ADDR in1fmt, ADDR num2

mov eax, 0
mov eax, num1
add eax, num2
mov sum, eax
mov eax, 0
mov eax, num1
imul num2
mov product, eax
mov eax, 0
add eax, num1
.if eax > num2
mov eax, num1
mov largest, eax
.else
mov eax, 0
add eax, num2
mov largest, eax
.endif

INVOKE printf, ADDR msg3fmt, ADDR msg3, sum
INVOKE printf, ADDR msg4fmt, ADDR msg4, product
INVOKE printf, ADDR msg5fmt, ADDR msg5, largest

ret
main        endp
end
4

1 回答 1

0

唯一真正的绊脚石是 'printf' 期望堆栈上的双精度(REAL8)即使对于"%f".

.686
.model flat, c

INCLUDELIB MSVCRT.LIB

; If MSVCRT.LIB doesn't exist try the first or both of the following libs
; includelib libcmt.lib
; includelib legacy_stdio_definitions.lib

printf     PROTO arg1:Ptr byte, printlist:VARARG
scanf      PROTO arg2:Ptr byte, inputlist:VARARG

.data
    in1fmt      byte "%f" , 0
    msg1fmt     byte 0Ah, "%s" , 0
    msg2fmt     byte 0Ah, 0Ah, "%s" , 0
    msg3fmt     byte 0Ah,"%s%f" , 0Ah, 0Ah, 0
    msg4fmt     byte 0Ah,"%s%f" , 0Ah, 0Ah, 0Ah, 0Ah, 0
    msg5fmt     byte 0Ah, "%s%f" , 0Ah, 0Ah,  0
    msg1        byte "Enter any number: " , 0
    msg2        byte "Enter another number: " , 0
    msg3        byte "The sum is: " , 0
    msg4        byte "The product is: " , 0
    msg5        byte "The largest is: ", 0

    num1        REAL4 ?
    num2        REAL4 ?
    sum         REAL8 ?         ; printf needs a double as argument
    product     REAL8 ?         ; printf needs a double as argument
    largest     REAL8 ?         ; printf needs a double as argument

.code
main proc
    INVOKE printf, ADDR msg1fmt, ADDR msg1
    INVOKE scanf, ADDR in1fmt, ADDR num1
    INVOKE printf, ADDR msg2fmt, ADDR msg2
    INVOKE scanf, ADDR in1fmt, ADDR num2

    fld num1
    fadd num2
    fstp sum

    fld num1
    fmul num2
    fstp product

    fld num1
    fld num2
    fcomi st,st(1)
    jnbe @F             ; Jump if ZF=0 & CF=0 - ST(0) > ST(1)
    fxch
    @@:
    fstp largest
    fstp st(0)              ; Just pop ST(0)

    INVOKE printf, ADDR msg3fmt, ADDR msg3, sum
    INVOKE printf, ADDR msg4fmt, ADDR msg4, product
    INVOKE printf, ADDR msg5fmt, ADDR msg5, largest

    xor eax, eax            ; Exitcode: 0
    ret
main endp
end
于 2020-02-08T21:17:53.137 回答