2012-03-28 8 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

答えて

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