2017-10-19 129 views
2

我有我的彙編代碼的兩個問題。根據指導原則,我必須使用浮點運算完成所有這些。就這樣說,似乎我沒有從中得到正確的答案,我不知道什麼是錯的。函數如下:y = 3x^3 + 2.7x^2-74x + 6.3。我必須給X,它是假設進入這個函數,並輸出Y.浮點不輸出正確的答案

該代碼也假設結束時,我鍵入N,但它不斷給我一個浮點錯誤。

編輯:我想出了我的問題與功能,但是每當我鍵入N它不會跳下來,並結束我的代碼。

input  REAL8 ?   ; user input 
result  REAL8 ?   ; result of calculation 
three  REAL8 3.0  ; constant 
twoSeven REAL8 2.7  ; constant 
seventyFour REAL8 74.0  ; constant 
sixThree REAL8 6.3  ; constant 


prompt BYTE "Enter an Integer or n to quit",0dh,0ah,0 
again BYTE "Would you like to go again? Y/N?", 0dh, 0ah, 0 
no  BYTE "N",0dh,0ah,0 
rprompt BYTE "The result of the calculation is ",0 

.code 
main PROC 
result_loop: 
finit     ; initialize the floating point stack 
mov edx,OFFSET prompt ; address of message 
call WriteString  ; write prompt 
call ReadFloat   ; read the input value 
fst input    ; save input of user 


fmul three    ; multiplies by three 
fadd twoSeven   ; Adds by 2.7 
fmul input    ; multiplies by the input 
fsub seventyFour  ; subtracts 74 
fmul input    ; multiplies by input 
fadd sixThree   ; adds by three 

fst result    ; puts value in area 
mov edx,OFFSET rprompt ; address of message 
call WriteString  ; write prompt 
call WriteFloat   ; writes the result 
call CrLf    ; prints new line 

mov edx, OFFSET again 
call WriteString 
Call ReadString 

cmp edx, 'n'   ; compares the input to n 
je end_if    ; jumps if its equal to n 
jmp result_loop   ; jumps back to the top 

end_if:     ; end statment 
call WaitMsg   ; 
exit     ; 
main ENDP 
END main 
+0

'FCOMI輸入,「n''是沒有任何意義。你可能想讀取一個字符串,將它與'「n」'進行比較,如果不相等,則將其轉換爲浮點數。 – Jester

+0

@jester我對代碼進行了小小的調整,增加了input2,並嘗試設置n和輸入之間的比較,但是我仍然很短。 – Unleaver

+0

''n''不是一個數字,你意識到,對吧?你不能把它看作是一個浮點數。 – Jester

回答

1
Call ReadString 
cmp edx, 'n'   ; compares the input to n 
je end_if    ; jumps if its equal to n 
jmp result_loop   ; jumps back to the top 
end_if:     ; end statment 

ReadString不工作,你認爲它的工作方式。

您需要將它傳遞給EDX指向緩衝區的指針,該緩衝區可以存儲輸入。您還需要在ECX中告訴您可以允許此緩衝區包含多少個字符。

當ReadString返回時,您將獲得有效輸入的字符數EAX

因此定義一個緩衝區並設置參數。

那麼你的代碼變成:

mov edx, offset InBuffer 
mov ecx, 1 
Call ReadString 
test eax, eax 
jz result_loop  ; Jump back if no input given! 
mov al, [edx]  ; Get the only character 
or al, 32   ; Make LCase to accept 'n' as well as 'N' 
cmp al, 'n'   ; compares the input to n 
jne result_loop  ; jumps back if its not equal to n 
end_if:    ; end statment 
相關問題