Average of Numbers Program



High Level Assembler(HLASM) for MVS & VM & VSE

Average of Numbers Program

Postby Blkr777 » Fri May 19, 2023 4:51 pm

Hi, can you please help me to find what is wrong with this code, I am trying to find average of numbers


AVERAGE CSECT            
         STM  14,12,12(13)
         BALR 12,0        
         USING *,12        
         ST   13,SAVEA+4  
         LA   13,SAVEA    
 *************************
         L   1,=F'6'      
         LR  6,1          
         LA  2,ARRAY      
         SR  4,4          
 LABEL   DS   0H          
         L   3,0(4,2)      
         A   4,=F'4'      
         A   3,SUM        
         ST  3,SUM        
        BCT 1,LABEL            
        D   6,SUM              
        CVD 7,DW              
        UNPK MSG+2(4),SUM      
        OI  MSG+5,X'F0'        
        LA  5,MSG              
        WTO TEXT=(5),ROUTCDE=11
***********************        
        L    13,SAVEA+4        
        LM   14,12,12(13)      
        SR   15,15            
        BR   14    
********************            
SAVEA   DS   18F              
ARRAY   DC   F'5'              
        DC   F'12'            
        DC   F'9'              
        DC   F'23'            
        DC   F'11'      
        DC   F'99'      
SUM     DC   F'0'        
MSG     DC   AL2(4)      
        DS   CL4        
DW      DS   D          
        DS   0F          
        LTORG ,          
        END      


Iam getting output as +0009 in the log
Blkr777
 
Posts: 2
Joined: Fri May 19, 2023 4:37 pm
Has thanked: 2 times
Been thanked: 0 time

Re: Average of Numbers Program

Postby sergeyken » Fri May 19, 2023 6:36 pm

The instruction
         . . . . . . .
        D   6,SUM  
        . . . . . . .

will divide the undefined(!) huge long binary fixed from (R6,R7) by the value of SUM
F'00000006,????????' / SUM

The value of (R6,R7) is something greater than 25,769,803,776 (=X'600000000'), up to 30,064,771,071 (=X'6FFFFFFFF')
The value of SUM is F'159'
The result of division is unpredictable, R6=remainder, R7=quotient
The result is unpredictable...
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Average of Numbers Program

Postby sergeyken » Fri May 19, 2023 7:03 pm

Rewrite it as follows

AVERAGE CSECT            
         STM  14,12,12(13)
         BALR 12,0        
         USING *,12        
         ST   13,SAVEA+4  
         LA   13,SAVEA    
 *************************   
         LA  2,ARRAY          = start of array    
         XR  3,3              clean up the accumulator    
 LOOP   DS   0H          
         A   3,0(2)           add new element to accumulator 
         LA   2,L'ARRAY(2)    = next element address
         C    2,=A(SUM)       compare to end of array
         JL   LOOP            loop while not end        
        ST  3,SUM             keep the total (not needed so far...)
        XR  2,2               prepare to divide               
        D   2,SIZE            find the average as integer              
        CVD 3,DW              convert quotient to decimal             
        UNPK TEXT,DW          unpack decimal to zoned    
        OI  TEXT+L'
TEXT-1,X'F0'  convert zoned to character              
        WTO TEXT=MSG,ROUTCDE=11  send message
***********************        
        L    13,SAVEA+4        
        LM   14,12,12(13)      
        SR   15,15            
        BR   14    
********************            
SAVEA   DS   18F              
ARRAY   DC   F'5'              
        DC   F'12'            
        DC   F'9'              
        DC   F'23'            
        DC   F'11'      
        DC   F'99'      
SUM     DC   F'0'    
SIZE    DC   A((SUM-ARRAY)/L'ARRAY)          number of array elements
DW      DS   PL8      
MSG     DC   Y(L'
TEXT)      
TEXT    DS   CL4                   
        LTORG ,          
        END      


It is highly recommended not gorget about comments!!!
Javas and Pythons come and go, but JCL and SORT stay forever.

These users thanked the author sergeyken for the post:
Blkr777 (Sun May 21, 2023 9:05 pm)
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Average of Numbers Program

Postby Blkr777 » Sun May 21, 2023 9:04 pm

Hi sergeyken, thanks for the reply, got the issue now
Blkr777
 
Posts: 2
Joined: Fri May 19, 2023 4:37 pm
Has thanked: 2 times
Been thanked: 0 time

Re: Average of Numbers Program

Postby sergeyken » Mon May 22, 2023 12:52 am

Blkr777 wrote:Hi sergeyken, thanks for the reply, got the issue now

Besides of the issue with misunderstanding the Divide instruction, I highly recommend you to review the whole style of coding, and reasonable usage of data, plus detailed understanding of different data formats specific. Partially I tried to show this in my re-written sample of code.

ALWAYS ADD COMMENTS TO ANY CODE YOU WRITE!
It shall also help yourself to understand your own code, after a very short period of time… :mrgreen:
Javas and Pythons come and go, but JCL and SORT stay forever.

These users thanked the author sergeyken for the post:
Blkr777 (Mon May 22, 2023 8:43 pm)
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times


Return to Assembler

 


  • Related topics
    Replies
    Views
    Last post