2014-09-25 44 views
0

試圖擴大我的編程技能。完成python,C++,java和一點點cobol,但是大會讓我感到困惑。試圖製作一個程序,用單獨的對話框提示並輸入四個等級,並計算等級的總和和平均值(總和/ 4)。代碼:添加四個數字並在裝配中找到平均值

.586 
.MODEL FLAT 

INCLUDE io.h   ; header file for input/output 

.STACK 4096 

.DATA 
number1 DWORD ? 
number2 DWORD ? 
number3 DWORD ? 
number4 DWORD ? 
four DWORD 4 
prompt1 BYTE "Enter first number", 0 
prompt2 BYTE "Enter second number", 0 
prompt3 BYTE "Enter third number",0 
prompt4 BYTE "Enter fourth number",0 
string BYTE 40 DUP (?) 
resultLbl BYTE "The sum is", 0 
resultAvg BYTE "The average is",0 
sum  BYTE 11 DUP (?), 0 
avg  BYTE 11 DUP (?), 0 

.CODE 
_MainProc PROC 
     input prompt1, string, 40  ; read ASCII characters 
     atod string   ; convert to integer 
     mov  number1, eax ; store in memory 

     input prompt2, string, 40  ; repeat for second number 
     atod string 
     mov  number2, eax 

     input prompt3, string, 40 
     atod string 
     mov  number3, eax 

     input prompt4, string, 40 
     atod string 
     mov  number4, eax 

     mov  eax, number1 ; first number to EAX 
     add  eax, number2 ; add second number to EAX 
     add  eax, number3 ; add third number to EAX 
     add  eax, number4 ; add fourth number to EAX 
     mov  ax, eax   ; copy eax's value into ax (does this take eax's value away from eax?) 
     mov  al, four  ; put's 4 into al 
     idiv al,    ; divides sum by ax(4) 
     dota avg, ax   ; convert to ASCII characters 
     dtoa sum, eax  ; convert to ASCII characters 
     output resultLbl, sum   ; output label and sum 
     output resultAvg, avg    ; output label and avg 

     mov  eax, 0 ; exit with return code 0 
     ret 
_MainProc ENDP 
END        ; end of source code 

不知道我在哪裏錯了。它編譯和運行,但只要求2個數字,然後返回這兩個數字的總和。任何和所有的幫助,非常感謝

回答

0

不能說是什麼導致只有2個號碼被請求,只有總和輸出沒有你的io.h。那個是從哪裏來的?代碼是有道理的。

ax寄存器只有16位。如果您的宏期望eax中的數字,則表示您已損壞。

你可能想要一個32位的分紅和結果。最簡單的方法就是堅持使用32位指令:

cdq    ; sign extends eax into the edx register 
idiv four   ; divide edx:eax by four. Result is in eax. 

向右移兩個地方更簡單!這也除以4.

sar ax, 2 
+0

那麼,我會把'CDQ'和'IDIV四'?我會把'mov al,four'和'idiv al,'替換成'cdq'和'idiv four'嗎?我認爲這就是你在 – Bob 2014-09-26 01:44:36

+0

@ user38254得到的是的。刪除3條說明。插入班次或部門。但是這不會解決你的I/O問題。 – Gene 2014-09-26 02:57:21

相關問題