title: calculating students avg using loop
body: <p>This program should calculate the avg grades of students. 
  I'm using a loop for the user to input 5 grades.
  I tried to keep the div part outside the loop, but it did not work.
  I had to put it inside the loop although logically doesn't make since and it always prints 1.</p>
  
  <p>here is the code:</p>
  
  <pre><code>include irvine32.inc
  
  ; Write a MASM program that calculates the avg grade of a student(Five grades are inputted by the user).
  
  .data
  
  mesg byte "     *********This program calculates average grades of students *********", 0dh, 0ah, 0
  mesg1 byte "Enter 5 grades: ", 0dh, 0ah, 0
  mesg2 byte "The average grade is: ", 0dh, 0ah, 0
  
  grade1 dword ?
  grade2 dword ?
  grade3 dword ?
  grade4 dword ?
  grade5 dword ?
  
  
  .code
  main proc
  
  mov eax, 0
  
  mov edx, OFFSET mesg
  call writestring
  
  mov edx, OFFSET mesg1
  call writestring
  
  mov ecx, 5
  
  myLoop :
  
  call readint
  mov grade1, eax
  call readint
  mov grade2, eax
  call readint
  mov grade3, eax
  call readint
  mov grade4, eax
  call readint
  mov grade5, eax
  
  add eax, grade1
  add eax, grade2
  add eax, grade3
  add eax, grade4
  mov grade5, eax
  
  call writeint
  
  call dumpregs
  
      LOOP myLoop
  
  mov edx, OFFSET mesg2
  call writestring
  
  mov eax, grade5
  mov cl, 5
  sub edx, edx
  div cl
  
  call writeint
  call dumpregs
  
  
  exit
  main endp
  end main
  </code></pre>
  
  <hr>
  
  <p>calculating avg, with the <code>div</code> inside the loop:</p>
  
  <pre><code>include irvine32.inc
  
  ; Write a MASM program that calculates the avg grade of a student(Five grades are inputted by the user).
  
  .data
  
  mesg byte "     *********This program calculates average grades of students *********", 0dh, 0ah, 0
  mesg1 byte "Enter 5 grades: ", 0dh, 0ah, 0
  mesg2 byte "The average grade is: ", 0dh, 0ah, 0
  
  grade1 dword ?
  grade2 dword ?
  grade3 dword ?
  grade4 dword ?
  grade5 dword ?
  
  
  .code
  main proc
  
      mov eax, 0
  
      mov edx, OFFSET mesg
      call writestring
  
      mov edx, OFFSET mesg1
      call writestring
  
      mov ecx, 5
  
  myLoop:
      call readint
      mov grade1, eax
      call readint
      mov grade2, eax
      call readint
      mov grade3, eax
      call readint
      mov grade4, eax
      call readint
      mov grade5, eax
  
      add eax, grade1
      add eax, grade2
      add eax, grade3
      add eax, grade4
      mov grade5, eax
  
      call writeint
  
      call dumpregs
  
  
      mov edx, OFFSET mesg2
      call writestring
  
      mov eax, grade5
      mov ebx, 5
      sub edx, edx
      div ebx
  
      call writeint
      call dumpregs
  
      LOOP myLoop
  
  
      exit
  main endp
  end main
  </code></pre>