APUT82
         PLA
         STA   RETADR
         PLA
         STA   RETADR+1
         PLA
         STA   :YIDX      
         PLA
         STA   :XIDX      
         PLA
         STA   ADDR4      
         PLA
         STA   ADDR4+1
         PLA
         STA   ADDR3      
         PLA   
         STA   ADDR3+1
         LDY   #0
         LDA   (ADDR4),Y
         STA   :XLEN
         LDY   #1
         LDA   (ADDR4),Y
         STA   :YLEN
         LDY   #2
         LDA   (ADDR4),Y
         STA   :ESIZE
         LDA   ERRCTRL
         CMP   #1
         BEQ   :CHKERR
         JMP   :ERREND
:CHKERR
         LDA   :XIDX           
         CMP   :XLEN
         BCS   :XOVF
         JMP   :ERRCONT1
:XOVF
         _ERR  #:E_SID;#:E_XOVF;#:E_DUMP;#:XIDX;#17
:ERRCONT1
         LDA   :YIDX           
         CMP   :YLEN
         BCS   :YOVF
         JMP   :ERREND
:YOVF
         _ERR  #:E_SID;#:E_YOVF;#:E_DUMP;#:XIDX;#17
:ERREND
         LDA   #0
         TAY
         STY   SCRATCH
         BEQ   :ENTLP
:DOADD
         CLC
         ADC   :YIDX
         TAX
         TYA
         ADC   SCRATCH
         TAY
         TXA
:LP
         ASL   :YIDX
         ROL   SCRATCH
:ENTLP
         LSR   :YLEN
         BCS   :DOADD
         BNE   :LP
         STX   :PBAK
         STY   :PBAK+1
         CLC
         LDA   :XIDX
         STA   :MLIER
         LDA   #0
         STA   :MLIER+1
         LDA   :ESIZE
         STA   :MCAND
         LDA   #0
         STA   :MCAND+1
         LDA   #0
         STA   :PROD
         STA   :PROD+1
         STA   :PROD+2
         STA   :PROD+3
         LDX   #$10
:SHIFTR  LSR   :MLIER+1
         ROR   :MLIER
         BCC   :ROTR
         LDA   :PROD+2
         CLC
         ADC   :MCAND
         STA   :PROD+2
         LDA   :PROD+3
         ADC   :MCAND+1
:ROTR
         ROR
         STA   :PROD+3
         ROR   :PROD+2
         ROR   :PROD+1
         ROR   :PROD
         DEX
         BNE   :SHIFTR
         LDA   :PBAK
         CLC
         ADC   :PROD
         STA   :PROD
         LDA   :PBAK+1
         ADC   :PROD+1
         STA   :PROD+1
         LDA   :PROD
         CLC
         ADC   #3         
         STA   :PROD
         BCC   :NOCAR
         INC   :PROD+1
:NOCAR
         CLC
         LDA   :PROD
         ADC   ADDR4
         STA   :PROD
         LDA   :PROD+1
         ADC   ADDR4+1
         STA   :PROD+1
         LDY   :PROD
         LDX   :PROD+1
         STY   ADDR2
         STX   ADDR2+1
         LDY   #0
         LDX   #0
:CLP
         LDA   (ADDR3),Y
         STA   (ADDR2),Y
         INY
         CPY   :ESIZE
         BNE   :CLP
         LDA   RETADR+1
         PHA
         LDA   RETADR
         PHA
         LDX   ADDR2+1
         LDY   ADDR2
         LDA   :ESIZE
         RTS
:ESIZE   DS    1
:XIDX    DS    1
:YIDX    DS    1
:MCAND   DS    2
:MLIER   DS    2
:PROD    DS    4
:XLEN    DS    1
:YLEN    DS    1
:PBAK    DS    2
:E_SID   ASC   "APUT82 (PUT82 MACRO)",00
:E_DUMP  ASC   ":XIDX(1) :YIDX(1) :MCAND(2) :MLIER(2)"
         ASC   ":PROD(4) :XLEN(1) :YLEN(1) PBAK(2)",00
:E_XOVF  ASC   "OUT OF BOUNDS! X > MAXIMUM.",00
:E_YOVF  ASC   "OUT OF BOUNDS! Y > MAX.",00
