assembly language, messed up output

perkeyone

something clever
Reaction score
71
x86 assembly i mean
ive tried several things, but was unable to get this program to display the average correctly but failed
the program uses some sort of c++ framework that my professor provided for us.
http://frank.mtsu.edu/~rdetmer/CSCI3160/windows32.zip
the only file from the project that i edited is "example.asm"
heres my code
oh and my teacher requires comments on every line, which i guess saves me the time of trying to explain what is going on to you guys

Code:
;Program prompts for 4 grades and a final exam grade, then displays the sum and average.
;Calvin Perkey
;10/7/09
.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA
prompt1 BYTE    "Grade 1?",0
prompt2 BYTE    "Grade 2?",0
prompt3 BYTE    "Grade 3?",0
prompt4 BYTE    "Grade 4?",0
prompt5 BYTE    "Final?",0
stravg  BYTE    11 DUP (?)
strsum  BYTE    11 DUP (?)
sumtext BYTE    " is the sum", 13, 10
avgtext BYTE    " is the average",0
lable1  BYTE    "For these grades:",0
sum     DWORD   ?
n100    DWORD   100
n6      WORD    6

.CODE
_MainProc       PROC
        input   prompt1, strsum, 40    ;input grade 1
        atod    strsum                 ;convert to integer
        mov     sum, eax               ;copy grade 1 to sum
        input   prompt2, strsum, 40    ;input grade 2
        atod    strsum                 ;convert to integer
        add     sum, eax               ;add grade 2 to sum
        input   prompt3, strsum, 40    ;input grade 3
        atod    strsum                 ;convert to integer
        add     sum, eax               ;add grade 3 to sum
        input   prompt4, strsum, 40    ;input grade 4
        atod    strsum                 ;convert to integer
        add     sum, eax               ;add grade 4 to sum
        input   prompt5, strsum, 40    ;input grade 5
        atod    strsum                 ;convert to integer
        add     sum, eax               ;add final grade to sum
        add     sum, eax               ;add final grade to sum again
        dtoa    strsum, sum            ;convert to ascii
        mov     eax, sum               ;copy sum to eax
        mul     n100                   ;multiply the sum by 100 to get 1 decimal place of accuracy
        cdq                            ;prepare for division
        div     n6                     ;divide by 6 to get the average
        add     eax, 5                 ;add 5 so that it will round up
        dtoa    stravg, eax            ;convert to ascii
        mov     bl, stravg[9]          ;copy the tenths place
        mov     stravg[10], bl         ;overwrite the hundredths place with teh tenths place
        mov     stravg[9], 2eh         ;overwrite the tenths place with a decimal
        output  lable1, strsum[8], strsum[9], strsum[10], sumtext, stravg[6], stravg[7], stravg[8], stravg[9], stravg[10], avgtext         ;output results
        
        ;This is so frusterating, I can't get it to display the average even though I can see the value in memory!
        ;I can get it to display by itself but not in the same box with the other strings... I don't get it.
        ;Does it have something to do with the line feed character?
        
        ret
_MainProc ENDP
END                                    ; end of source code

the output i am trying to achieve will look like this...
"X is the sum
Y is the average"

but for some reason i am only getting this
"X is the sum
is the average"
 

perkeyone

something clever
Reaction score
71
ill try it one sec...

EDIT: nope, if i put a null there the output shows
"X is the sum"
thanks for trying though
i added a lil more description as to how i need to output to be formatted.
 

The Helper

Necromancy Power over 9000
Staff member
Reaction score
1,697
You are never loading anything into avgtext. It is going to always be blank unless you put something in there. Do a search on your code for avgtext and you will see you are never loading it.
 

perkeyone

something clever
Reaction score
71
today i found out what the problem was

i was using the output macro incorrectly
it outputs only one string terminated by a null
and i was supposed to group all my strings in the order that i wanted them to print. so my error was the order that i declared variables... by far the lamest error ive heard of...

so in order to fix it i had to change
Code:
stravg  BYTE    11 DUP (?)
strsum  BYTE    11 DUP (?)
sumtext BYTE    " is the sum", 13, 10
avgtext BYTE    " is the average",0
;etc
        output  lable1, strsum[8], strsum[9], strsum[10], sumtext, stravg[6], stravg[7], stravg[8], stravg[9], stravg[10], avgtext         ;output results
to
Code:
strsum  BYTE    11 DUP (?), " is the sum", 13, 10
stravg  BYTE    11 DUP (?), " is the average",0
;etc
        output  lable1, strsum       ;output results

@ the helper
actually the average text was showing up in the prompt, but the stravg wasnt. (both were in the memory though) but i figured out that it was because i was using the output macro incorrectly, the partially correct output that i had was pure luck.

@ both of you
thanks for trying to help me out
i really ought to do my homework sooner
luckily i got the changes made and submitted to the online dropbox for the assignment, so i should get full credit i think
 

perkeyone

something clever
Reaction score
71
wow ty
i was going to say +rep to you guys for helping
but phyrex1an's is disabled and yours is astronomical
haha
 

kicker10BOG

New Member
Reaction score
0
Hello, I have the same professor you had.... He's changed the assignment a little and now we have to round the average to the nearest tenth..... And now it's win64.

Code:
; OLAB3 assembly language program -- Finds and displays the sum and average of 4 final grades
; Author:  Fake Name
; Date:    2/2010

INCLUDE io.h            ; header file for input/output

.DATA               ; reserve storage for data
grade1    DWORD   ?
grade2    DWORD   ?
grade3    DWORD   ?
grade4    DWORD   ?
final     DWORD   ?
prompt1   BYTE    "Grade 1?", 0
prompt2   BYTE    "Grade 2?", 0
prompt3   BYTE    "Grade 3?", 0
prompt4   BYTE    "Grade 4?", 0
promptF   BYTE    "Final?", 0
string    BYTE    40 DUP (?)
resultLbl BYTE    "For these grades: ", 0
divisor   DWORD   6
sum       BYTE    11 DUP(?), " is the sum.", 0dh, 0ah
avg       BYTE    11 DUP(?), '.'
rem       BYTE    11 DUP(?), " is the average.", 0

.CODE
MainProc  PROC
          sub     rsp, 120              ; reserve stack space for MainProc

          input   prompt1, string, 40   ; read ASCII characters
          atod    string                ; convert to integer
          mov     grade1, eax           ; store in memory

          input   prompt2, string, 40   ; repeat for second grade
          atod    string
          mov     grade2, eax

          input   prompt3, string, 40   ; repeat for third grade
          atod    string
          mov     grade3, eax

          input   prompt4, string, 40   ; repeat for fourth grade
          atod    string
          mov     grade4, eax
          
          input   promptF, string, 40   ; repeat for final
          atod    string
          mov     final, eax

          mov     eax, grade1     ; first grade to EAX
          add     eax, grade2     ; add second grade
          add     eax, grade3     ; add third grade
          add     eax, grade4     ; add fourth grade
          add     eax, final      ; add the final grade twice
          add     eax, final
          dtoa    sum, eax        ; convert sum to ASCII characters

          sub     edx, edx        ; prepare for division
          div     divisor         ; divide the sum by 6
          dtoa    avg, eax        ; convert the quotient to ASCII characters
          dtoa    rem, edx        ; convert the remainder to ASCII characters
          output  resultLbl, sum  ; output label and sum

          add     rsp, 120        ; restore stack
          mov     rax, 0          ; value to return (0)
          ret
MainProc ENDP
END

The output I get is: (Suppose X, Y, and Z are integer values AND '-' is a space.)
X is the sum.
y.-------Z is the average.

As you can see, the problem is that the tenth value is too far from the decimal point. If I try changing it from BYTE 11 DUP(?) to a different number of "DUP(?)"s it erases some of the text after it, resulting in:

X is the sum.
y.-------Zverage.

Can anyone please give me a little advice?
__
Thanks
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top