16-bit sum of data

Purpose:

To calculate the sum of a series of numbers. The length of the series given as a number of words (i.e. 16 bit quantities) is in the variable length at location $6000. The starting address of the series is contained in the long-word variable start at location $6002. The sum is to be stored in the variable total at location $6006. It is to be assumed that the sum is a 16-bit number so that you can ignore carries.

Note: the specification above says that the starting address is in the variable start at location $6002. This is, of course, short for saying that the 32-bit value is spread over the locations with addresses $6002, $6003, $6004 and $6005.

Sample Test Data:

input:

length: (6000) = 0003
(6002) = 00005000
(5000) = 2040
(5002) = 1c22
(5004) = 0242)

output:

total: (6006) = (5000) + (5002) + (5004)
= 2040 + 1c22 + 0242 = 3ea4

 

Source Code:

data equ $6000      
program equ $4000      
           
  org data      
length ds.w 1 ;number of data elements    
start ds.l 1 ;address of data elements    
  ds.w 1 ;sum of data elements    
           
       
  org program      
  movea.l start, a0 ;initialise pointer    
  moveq #0, d0 ;initialise sum to 0    
  move.w length, d1 ;initialise element count    
loop add.w (a0)+,d0 ;sum next element    
  subq.w #1, d1 ;update element count    
  bne loop ;if count not zero goto loop    
           
  move.w d0,total ;store sum    
           
           
           

Back to 68000 program contents page