2012-03-28 47 views
1

我的輸出存在一些問題......我想我的陣列有一些問題。還有一種新的組裝。該任務是設計一個使用對話框提示用戶輸入數字的彙編程序。這些數字將被存儲在一個數組中。將會有一個輸出消息顯示以下內容:輸入的數字總和,輸入了多少個數字(不包括-9999來結束程序),數字的平均值以及數組數量大於或等於等於平均值​​。所有幫助表示讚賞!以下是我有:裝配中的陣列

.DATA 
numArray DWORD ? 
numElts  DWORD 100 
num   DWORD ? 
exitNum  DWORD -9999 
prompt  BYTE "Enter a number", 0 
string  BYTE 40 DUP (?) 
resultLbl BYTE "Results", 0 
sum   BYTE 11 DUP(?), " is the sum.", 0dh, 0ah 
;numEntered BYTE 11 DUP(?), " numbers were entered." 
avg   BYTE 11 DUP(?), " is the average." 
count  BYTE 11 DUP(?), " is the number of entries that are >= the  average." 

.CODE 
_MainProc PROC 
      mov  eax, 0      ; sum := 0 
      lea  ebx, numArray    ; get address of  nbrArray 

LOOP1:  input prompt, string, 40   ; read ASCII characters 
      atod string      ; convert to integer 
      mov  num, eax     ; store in memory 
     mov  ecx, numElts    ; count := nbrElts 
     cmp  exitNum, eax 
     je  QUIT      ; quit if -9999 
     add  eax, [ebx]     ; add number to sum 
     add  ebx, 4      ; get address of next array elt 
     add  ecx, 1      ; add one for count 
     loop LOOP1      ; repeat nbrElts times 

     cdq         ; extend sum to quadword 
     idiv numElts      ; calculate average 
     dtoa avg, ebx     ; convert to ASCII characters 
     dtoa count, ecx 
     dtoa sum, eax 

QUIT:    
     output resultLbl, sum, avg, count 
     ret 

_MainProc ENDP 
END            ; end of source code 
+0

是'atod'自定義函數嗎?通常會導致'atod'轉換爲'double',而不是整數,因此您需要'atol'。另外,描述你的問題會有很大的幫助。 – Necrolis 2012-03-28 06:14:00

回答

1

您還使用EAX來存儲您的總和,但它會通過任何呼叫(具體atod)被感染,你在陣列中也沒有疼痛的價值。 使您的代碼有點更流暢(分成幾部分,以使其更簡單):

 mov  ecx, 100     ; loop count (size of array) 
     lea  ebx, numElts    ; get address of the array 

LOOP1: 
     input prompt, string, 40   ; read ASCII characters 
     push ecx       ; save the loop count incase 
     atod string      ; convert to integer 
     pop  ecx       ; restore the loop count 
     mov  [ebx], eax     ; store in the array    
     cmp  exitNum, eax 
     je  NEXT      ; quit if -9999 
     add  ebx, 4      ; get address of next array elt 
     loop LOOP1      ; repeat nbrElts times 

從這裏我們可以做平均,總和等(我已經做了只有相加平均,剩下的就是由你決定)。

NEXT: 
mov ecx,100   ;loop count 
lea ebx,numElts  ;array address 
mov edx,0    ;sum 

LOOP2: 
mov eax,[ebx]     ;get the num 
cmp eax,exitNum    ;check for the sentinel 
je DONE 
add edx,eax     ;increase the sum 
add ebx,4      ;next array element 
loop LOOP2 

DONE: 
mov eax,100 
sub eax,ecx     ;get the number of entries processed 
mov ecx,eax 
mov eax,edx     ;get ready to divide 
push edx      ;save the sum 
mov edx,0 
idiv ecx      ;sum/count 
pop edx      ;restore the sum 
;from here edx has your sum, eax has your average