; mysc.altervista.org
; Problem n. 1   
; how to manage array in 8086 programming Source code ?
; author: Wilfried Mbouenda Mbogne 
DIM1   EQU 9  
DIM2   EQU 10  
       .MODEL small
       .STACK
       .DATA
A   DB  1, 2, 3, 4, 5, 6, 7, 8, 10, 11
B   DB  DIM1  DUP(?)     
M   DW  (DIM1 * DIM1) DUP (?)
MINA DB ?
MINB DB ? 
C DB ?
MAX DW ?
TEMP DW ?   
TEMP2 DW ?
       .CODE
       .STARTUP 
;1.  Compute the sum for each pair of consecutive values, putting the result in a 9 
;elements array (Bj = Aj + Aj+1).
       MOV AL, 0
	   MOV CX, DIM1
       MOV DI, 0
lab1:  MOV AL,A[DI]
	   ADD AL,A[DI+1]
	   MOV B[DI],AL
	   INC DI
       DEC CX
       CMP CX,0
       JNZ lab1

    
; 2  Find min value of A and B
;small = element[0]    
;for each element in array, starting from 1 (not 0):
;    if (element < small)
;        small = element 
;A
     MOV DI, 0
     MOV AL,A[DI]
     MOV CX, DIM1
     MOV DI, 1  
LAB3:CMP A[DI],AL
     JAE PASS
     MOV AL,A[DI]
PASS:INC DI
     DEC CX
     CMP CX,0             
     JNZ LAB3
     MOV MINA,AL    
     ;B
     
     MOV DI, 0
     MOV AL,B[DI]
     MOV CX, DIM1-1
     MOV DI, 1  
LAB4:CMP B[DI],AL
     JAE PASS1
     MOV AL,B[DI]
PASS1:INC DI
     DEC CX
     CMP CX,0             
     JNZ LAB4
     MOV MINB,AL   
     
;3 
;Compute  all  possible  products  among  first  9  values  
;of  first  array  and  all  9 values of second array, 
;putting results in a matrix of 9x9 values (words)
; for each element1 in array A, starting from 0:
;	for each element2 in array B, starting from 0:
;		M[i][j] = element1*element2
;
       MOV AX, 0
	   MOV CX, DIM1
       MOV DI, 0
lab5:  MOV C, DIM1
       MOV BX, 0
lab6:  MOV AX, 0
       MOV AL, A[DI]
       MUL B[BX]         
;M[J,K] = M[2 * (J * NUM_COLS + K)] Index is multiply by 2 because the matrix size element is 2 byte (1 word)
       MOV TEMP,AX
       XOR AX,AX
       MOV AX,DI
       MOV TEMP2,BX
       MOV BX,DIM1
       MUL BX
       MOV BX,TEMP2
       ADD AX,BX
       MOV TEMP2,BX
       MOV BX,2
       MUL BX
       MOV BX,TEMP2  
       MOV TEMP2,DI
       MOV SI,AX
       MOV AX,TEMP
       MOV M[SI],AX 
       MOV AX, 0
       INC BX
       DEC C
       CMP C,0             
       JNZ LAB6
       MOV AX, 0
       INC DI
       DEC CX
       CMP CX,0             
       JNZ LAB5 
;4
;Find the maximum value among values of the so computed matrix
;max = matrix[0][0];
;        for each integer i from 0 to dim
;            for each integer j from 0 to dim
;                if (max < matrix[i][j]) {
;                    max = matrix[i][j];
                 
      
      MOV AX,M[0]
      MOV CX,DIM1  
      MOV DI,0
lab7: MOV C,DIM1
      MOV BX, 0 
lab8: ;M[J,K] = M[2 * (J * NUM_COLS + K)]
       MOV TEMP,AX
       XOR AX,AX
       MOV AX,DI
       MOV TEMP2,BX
       MOV BX,DIM1
       MUL BX
       MOV BX,TEMP2
       ADD AX,BX
       MOV TEMP2,BX
       MOV BX,2
       MUL BX
       MOV BX,TEMP2  
       MOV TEMP2,DI
       MOV SI,AX
       MOV AX,TEMP
      CMP AX,M[SI]
      JAE PASS3
      MOV AX,M[SI]
PASS3:INC BX
      DEC C
      CMP C,0             
      JNZ LAB8 
      INC DI
      DEC CX
      CMP CX,0             
      JNZ LAB7
      MOV MAX,AX
   .EXIT
   END 