2013-03-02 47 views
0

大家好我是新來的彙編語言,我不知道如何創建一個程序來讀取3個16位整數a,b ,然後讓它計算出判別式。 (b^2-4ac) 任何人都可以幫助我嗎? 到目前爲止,我開始嘗試讓程序與a和c相乘。如何用匯編語言計算判別式(b^2 - 4ac)

.data 
Prompt BYTE  "Enter a number ?" , 0 
Message BYTE  "The discriminant is: ", 0 
b  SDWORD ? 
a  SDWORD ? 
cc  SDWORD ? 
discriminant SDWORD ? 
.code 
main  PROC 
mov edx, OFFSET Prompt   ; EDX must have the string's offset 
    call WriteString  ; Call the procedure to write a string 
    call ReadInt   ; Call the procedure to read an integer 
    mov a, eax   ; The integer is read into AL, AX or EAX 

    mov edx, OFFSET Prompt ; Read another integer 
    call WriteString 
    call ReadInt 
    mov cc, eax 

mov eax, a       ; AL AX or EAX must have the 
        ; multiplicand 
    cdq    ; Clear the EDX register 
    imul cc   ; One operand - the multiplier 
    mov Product, eax   ; The product is in AL, AX or EAX 
+0

嗨我其實也不知道C的任何東西。我開始與大會 – Andy 2013-03-02 04:19:19

+0

[哦,好的...](http://tinyurl.com/7zlwvql) – 2013-03-02 04:23:45

回答

1

你說你正在使用16位輸入,所以A,B和C都有望成爲16位整數。這會得到一個32位的簽名結果。有了這個,我們可以做到:

; Get b^2 into EBX 
movsx eax, [WORD b] ; Sign extend b to 32bit 
imul eax   ; Multiply 
mov ebx, eax  ; Put the result into ebx 

; Get 4ac into EAX 
movsx eax, [WORD a] ; Sign extend a to 32bit 
shl eax, 2   ; Multiply by 4 
movsx ecx, [WORD c] ; Sign extend c to 32bit 
imul ecx   ; EDX:EAX = 4 * a * c 

; Subtract, the result is in EBX 
sub ebx, eax 

這是使用32位操作數,因爲你的例子。您可以使用16位操作數進行等價處理,但如果您使用32位結果,則必須從DX:AX轉換爲32位。請注意,根據您使用的彙編程序,[WORD b]的語法可能會更改。我看到一些使用[WORD PTR b]或僅僅WORD b或類似的。

+0

謝謝你的回答!然而,我不熟悉shl eax中的「shl」,2;乘以4你可以向我解釋那是什麼,爲什麼我需要它在程序中? – Andy 2013-03-02 04:31:04

+0

'SHL'表示左移。它基本上佔用一個值的所有位,並將它們移動到左邊,但是多次指定並在末尾放入0。在這種情況下,我將數值向左移兩次。使用4bit導致它很短,這會將1111(即-1)更改爲1100(即-4)或1110(即-2)至1000(即-8)。 'SHL'在所有x86上都是非常快的操作,而'IMUL'或'MUL'則不是。 – 2013-03-02 04:33:25

+0

@andy查看二進制數字是如何工作的,並且清楚地將所有位左移一位將乘以2.考慮正常的十進制數(基數10),向左移一位乘以10,二進制是基數2因此向左移動1乘以2. – doug65536 2013-03-02 04:35:49