2017-10-18 85 views
0

簡單的裝配程序可以吐出兩個用戶輸入數字中較大的一個。我無法正確輸出輸出。例如,如果我輸入45和55,最大值將是55,但是當我嘗試反向55和45(答案應該仍然是55)時,我得到45.這看起來似乎只能輸出第二個輸入值存儲在EAX。任何幫助是極大的讚賞。來自輸入的裝配最大值

.586 
.MODEL FLAT 

INCLUDE io.h 

.STACK 4096 

.DATA 
value1 DWORD ? 
value2 DWORD ? 

prompt1 BYTE "Enter the first number", 0 
prompt2 BYTE "Enter the second number", 0 
string BYTE 40 DUP (?) 

resultLbl BYTE "The maximum value you entered was:", 0 

.CODE 

_MainProc PROC  
input prompt1, string, 40  ;get user input value1 
atod string      ;convert input (ASCII) to integer 
mov ebx, eax 

input prompt2, string, 40 ; repeat for value2 
atod string 
mov value2, eax 

cmp eax, ebx    ;compare user inputs 

jg greater_than  ;jump conditional if value 1 is greater then value 2 

    greater_than: ;condition if greater than ouput 
     dtoa value1, eax     ;decimal to ASC for output of 
integer value stored at ebx 
     output resultLbl, value1   ;output value 1 
      jmp exit      

    less_than: ;condition if less than ouput 
     dtoa value1, eax 
     output resultLbl, value2   ;else output value 2  
      jmp exit 

    exit: ;quit jump    ;end if/else conditional 

    mov eax, 0   ;clear memory 
    mov ebx, 0 
    ret 
_MainProc ENDP 
END 
+0

調試。函數調用中很可能會覆蓋ebx。存儲在內存中,而不是一個寄存器 –

+0

@SamiKuhmonen沒有寄存器被'input'影響,根據以下鏈接:http://flylib.com/books/en/2.265.1.27/1/ 從鏈接引用: 輸入宏只更改指定目標處的內存。它不會更改任何寄存器內容,包括標誌寄存器。 –

回答

1

問題是與指令的順序。即使操作數1小於操作數2,唯一的區別是jg greater_than不會導致顯式跳轉到greater_than標籤。然而,第二天指令集打印操作數1,也就是說,執行的控制將轉向「內」 greater_than標籤。爲什麼?這是因爲下一條指令dtoa value1, eax恰好在jg greater_than之後。那麼,你現在看到了這個問題嗎?你不會阻止操作數1的打印操作,如果1小於2.操作所有代碼正在做的是,確保爲確保您的程序打印value1如果操作數1> 2的操作數

你的代碼是等效下面的C代碼:

if (op1>op2)goto gt; 
gt: 
printf("%d",op1); 
exit(0); 

lt: 
printf("%d",op2); 
exit(0); 

當你的目的是要做到以下幾點:

if(op1>op2){ 
printf("%d",op1); 
exit(0); 
} 

printf("%d",op2); 
exit(0); 

爲了解決這個問題,你就必須改變指令的順序,如:

jg greater_than  ;jump conditional if value 1 is greater then value 2 

    less_than: ;condition if less than ouput 
     dtoa value1, ebx 
     output resultLbl, value1   ;else output value 2  
      jmp exit 

    greater_than: ;condition if greater than ouput 
     dtoa value1, eax     ;decimal to ASC for output of integer value stored at eax 
     output resultLbl, value1   ;output value 1 
      jmp exit 

上面的代碼確保less_than標籤內的代碼在操作數1>操作數2時通過顯式跳轉到greater_than標籤而被阻止執行。當操作數1 < =操作數2時,不進行顯式跳轉,並執行序列中的下一條指令。在這種情況下,內部的less_than instrcutions被執行時操作數1 = <操作數2

簡而言之,條件跳轉導致僅當條件被滿足,否則執行該序列中的下一指令的跳轉。您有責任按照正確的順序放置說明。它們不像高級條件結構,如if ... else。

更新:請注意,我的整個解決方案中只使用value1

+0

感謝您的幫助,我有一些C++的經驗,所以我理解的邏輯,它似乎我絆倒在裝配的實現。 –

+0

我怎麼能把這個檢查3個數字? –

1

如上所述,如果您在此指令之後立即使用「jg greater_than」跳轉,即使EAX < = EBX您執行的應該是針對EAX> EBX條件執行的相同指令,您從不執行跳轉到less_than。標籤「less_than」可以被刪除。你應該寫:

CMP EAX,EBX 
    JG greater_than 

;less_than 
    dtoa value1, eax 
output resultLbl, value2 
    jmp exit 

greater_than: 
integer value stored at ebx 
output resultLbl, value1 

exit: 

但我有一個新的解決方案。這是一個優化的MAX(A,B)功能:

; INPUT: EAX, EBX 
; OUTPUT: EAX <- The maximum value between EAX, EBX 

    CMP EAX,EBX 
CMOVL EAX,EBX 
0

輸入了3個值。這看起來好像會起作用嗎?

.586 
.MODEL FLAT 

INCLUDE io.h 

.STACK 4096 

.DATA 
value1 DWORD ? 
value2 DWORD ? 
value3 DWORD ? 

titleLbl BYTE "MAXIMUM NUMBERS",0 
formula BYTE "Taking any THREE random user input numbers, this program can 
determine which of those numbers is greater.",0 

prompt1 BYTE "Enter the first number",0 
prompt2 BYTE "Enter the second number",0 
prompt3 BYTE "Enter the third number",0 
string BYTE 40 DUP (?) 

resultLbl BYTE "The maximum value you entered was:", 0 
max_value BYTE 11 DUP (?), 0 

.CODE 

_MainProc PROC 
output titleLbl, formula 

input prompt1, string, 40  ;get user input value1 
atod string      ;convert input (ASCII) to integer 
mov ebx, eax 

input prompt2, string, 40 ; repeat for value2 
atod string 
mov ecx, eax 

input prompt3, string, 40 ; repeat for value2 
atod string 
mov value3, eax 

cmp ebx, ecx    ;compare user inputs 

jg greater_than  ;jump conditional if value 2 is greater then value 3 

less_than: ;condition if less than ouput 
    cmp eax, ecx 

    jg greater_than_again ;jump conditional if 2 is greater than 1 

    less_than_again: 
     dtoa max_value, ecx 
     output resultLbl, max_value   ;else output value 2  
     jmp exit 

    greater_than_again: 
    dtoa max_value, eax     ;decimal to ASC for output of 
integer value stored at eax 
    output resultLbl, max_value   ;output value 1 
     jmp exit 

greater_than: ;condition if greater than ouput 
    cmp eax, ebx 

    jg greater_than_again ;jump conditional if 2 is greater than 1 

    less_than_again2: 
     dtoa max_value, ebx 
     output resultLbl, max_value   ;else output value 2  
     jmp exit 

    greater_than_again2: 
    dtoa max_value, eax     ;decimal to ASC for output of 
integer value stored at eax 
    output resultLbl, max_value   ;output value 1 
     jmp exit 

    exit: ;quit jump    ;end if/else conditional 

    mov eax, 0   ;clear memory 
    mov ebx, 0 
    mov ecx, 0 
    ret 
_MainProc ENDP 
END 
+0

這應該是對這個問題的正確答案嗎?你發佈它作爲答案。 –

+0

它爲我工作,也許有更好的辦法? –

+0

我想這可能是一個新的問題,因爲你說:「這是否看起來像它的工作?」。這不是你通常如何開始答案。如果你已經測試過它並且工作,那很好。 –

相關問題