title: MASM Adding up the sum of elements in an array
body: <p>I'm just trying to write a simple program that takes in x amount of integers from a user, have the user enter them, then use a subroutine to calculate the sum of the array and print out the sum.</p>
  
  <p>I want to pass the address of the array in EAX and the number of array elements into EBX.</p>
  
  <pre><code>;Lab 4
  
  title Template Lab4 Adder
  
  INCLUDE Irvine32.inc ;32bit assembler
  
  .data
  theansweris DWORD ?
  welcometoadder BYTE "Welcome to adder",0dh,0ah,0
  howmanynumbers BYTE "How many numbers would you like to add?:",0dh,0ah,0
  enteranumber BYTE "Enter a Number:",0dh,0ah,0
  youentered BYTE "You entered:",0dh,0ah,0
  thesumis BYTE "The sum of the numbers is:",0dh,0ah,0
  space BYTE " ",0dh,0ah,0
  readStr    DB  50 DUP(?) ;allocates 50 BYTES WITH ?
  var1 DWORD ?
  testz DWORD 0
  sum         BYTE    11 DUP(?), " is the sum.", 0dh, 0ah
  numArray    DWORD   ?
  numElts     DWORD   100
  num         DWORD   ?
  resultLbl   BYTE    "Results", 0
  
  .code
  main PROC
      call Clrscr
      mov edx, OFFSET welcometoadder
      call WriteString
      ;mov     ecx, 100                   ; loop count (size of array)
      ;lea     ebx, numElts               ; get address of the array
      mov edx, OFFSET howmanynumbers
      call WriteString
  
      mov edx,OFFSET enteranumber
      call WriteString
      call ReadInt
      mov edx,OFFSET youentered
      call WriteString
      mov ebx,eax
      mov edx,ebx
      call WriteInt
      mov edx, OFFSET space
      call WriteString
      ;mov ebx,edx
      mov ecx,ebx
      lea ebx,NumElts
  
  LOOP1:
          mov edx, OFFSET enteranumber
          call WriteString
          mov edx, OFFSET space
          call WriteString
          call ReadInt
          mov     [ebx], eax                  ; store in the array            
          add     ebx, 4                      ; get address of next array elt
          cmp ecx,0
          je LOOP2
          loop    LOOP1                       ; repeat nbrElts times
  LOOP2:
  call DumpRegs
  ;mov eax,[ebx]                  ;get the num
  ;add edx,eax                    ;increase the sum
  ;add ebx,4                      ;next array element
  ;loop LOOP2
  
  ;    CALL ADDER
  
  ;ADDER PROC
  ;    add bx, cx
  ;    add bx, cx
  ;    add ax, bx
      ;mov edx, OFFSET thesumis
      ;call WriteString
  ;    RET
  ;ADDER ENDP
  
      exit
      main ENDP
  
  END main
  </code></pre>