News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

When integers are read in

Started by Tiffany, October 12, 2005, 02:20:00 PM

Previous topic - Next topic

Tiffany

Hey guys. Not gonna lie. This is a homework question but the program is done already. My question is this: The program asks for integers and exits when a zero is entered like it is supposed to. When it exits it gives the count on how many numbers were entered excluding zero and the average. Now I can get my count to work but my average always comes to zero. However, I can get the average to work if I do a running average after every interger. Is there anyway to keep up with the average to display at the end if the program is not using an array to store the numbers? I have attached my code so you guys could see how I've set it up. Is there anyway to display the average at the end without the use of an array? If so I'm not asking for code just advice or bring my attention to something I'm missing. Thanks for taking a look.

.386
.MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

INCLUDE io.h                    ; file for input/output

cr         EQU    0dh           ; carriage return character
Lf         EQU    0ah           ; line feed character

.STACK     4096                 ; 4096-byte stack is reserved

.DATA                           ; storage for data reserved

intro            BYTE   cr, Lf, "Input positive or negative integers one at a time.", cr, Lf
                  BYTE   "The program will display how many numbers were put in and", cr, Lf
                  BYTE   "their average. Enter a zero (0) when you do not have anymore", cr, Lf
                  BYTE   "numbers to enter.", cr, Lf, Lf, 0
prompt        BYTE   "Enter your number: ", 0
number        BYTE   16 DUP (?)
sum             DWORD  ?
avgLabel       BYTE   "Average is:",0
countLabel    BYTE   cr,Lf,"# Count is:",0
value            BYTE   11 DUP (?),cr,Lf, 0


.CODE                               ; beginning of main program code
_start:

           output intro             ; initial directions
           mov    sum, 0            ; initializes sum at zero
           mov    ebx, 0            ; initializes count at zero


notZero:   output prompt
               input  number, 16
               atod   number

           cmp    eax, 0           
           jz     done
   
           add    sum, eax      ; adds integer to sum
           inc    ebx              ; increments the count
           mov    eax, sum     ; gets the sum
           cdq                      ; extends the sum to 64 bits
           idiv   ebx               ; finds average   sum/count
           jmp    notZero
done:

           dtoa   value, ebx        ; converts the count to ASCII character
           output countLabel        ; displays the count label
           output value             ; displays count
             
           dtoa   value, eax        ; converts the average to ASCII character
           output avgLabel         ; displays the label for average
           output value             ; displays the average   
           jmp quit
         
   
       
         

quit:           INVOKE   ExitProcess, 0  ; exits with return code 0

PUBLIC _start                       ; make entry point public

END                                    ; end of code


P1

The theory here is to keep the weighted average.  And you only need two variables to do so.

weighted count (wc),weighted average (wa), new number(nn)

((wc * wa) + nn) / wc + 1 = updated weighted average and also increment wc after the calculation.

Regards,  P1  :8)

rags

I see 2 possible problems in the code:

1)eax bieng trashed in the count loop by the calls to the output, input, and conversion routines.

2)ebx bieng trashed in the count loop by the calls to the output, input, and conversion routines.

the values in these register should be preserved before making calls to other functions, and restored
afterwards, because the functions could be using these registers, and not preserving them.
Thus whatever you had in them would be changed after the returning from the call.


Rags
God made Man, but the monkey applied the glue -DEVO

Tiffany

Thanks guys. I got it. I just designated another label and moved eax into it. Just started this stuff about a month ago. Thanks again for your help