home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / bp_6_93 / bonus / winer / sumarray.asm < prev    next >
Assembly Source File  |  1992-05-12  |  1KB  |  48 lines

  1. ;SUMARRAY.ASM, adds the elements in a long integer array
  2.  
  3. ;Copyright (c) 1991 Ethan Winer
  4.  
  5. ;Syntax: CALL SumArray(SEG Array&(1), NumElements%, Sum&)
  6.  
  7. .Model Medium, Basic
  8. .Code
  9.  
  10. SumArray Proc Uses SI, Array:DWord, NumEls:Word, Sum:Word
  11.  
  12.   Push DS          ;save DS manually so we can restore it later
  13.                    ;  within this routine
  14.   Xor  AX,AX       ;clear AX and DX which will accumulate
  15.   Mov  DX,AX       ; the total
  16.  
  17.   Mov  BX,NumEls   ;get the address for NumElements%
  18.   Mov  CX,[BX]     ;assign NumElements% to CX
  19.   Lds  SI,Array    ;load the address of the first element
  20.   Jcxz Exit        ;exit if NumElements = 0
  21.  
  22. Do:
  23.   Add  AX,[SI]     ;add the value of the low word
  24.   Adc  DX,[SI+2]   ;and then add the high word
  25.   Add  SI,4        ;point to the next array element
  26.  
  27.   Or   SI,SI       ;are we beyond a 32k boundary?
  28.   Jns  More        ;no, continue
  29.  
  30.   Sub  SI,8000h    ;yes, subtract 32k from the address
  31.   Mov  BX,DS       ;copy DS into BX
  32.   Add  BX,800h     ;adjust the segment to compensate
  33.   Mov  DS,BX       ;copy BX back into DS
  34.  
  35. More:
  36.   Loop Do          ;loop until done
  37.  
  38. Exit:
  39.   Pop  DS          ;restore DS and gain access to Sum&
  40.   Mov  BX,Sum      ;get the DGROUP address for Sum&
  41.   Mov  [BX],AX     ;assign the low word
  42.   Mov  [BX+2],DX   ;and then the high word
  43.  
  44.   Ret              ;return to BASIC
  45.  
  46. SumArray Endp
  47. End
  48.