;******************************************************
;
         TITL    "65816/65802 CROSS-16 EXAMPLE PROGRAM"
         PAGE     56
         CPU     "65816.TBL"     ; CPU TABLE
         HOF     "MOT16"         ; HEX FORMAT

; THE 65816 TABLE IS WRITTEN WITH THE PROCESSOR IN
; NATIVE MODE WITH M = 0.  THEREFORE, MOST OF THE
; OPERANDS IN THE IMMEDIATE ADDRESSING INSTRUCTIONS
; ARE TWO BYTES LONG.

;******************************************************
;
; THIS IS AN EXAMPLE PROGRAM FOR THE 65816 PROCESSOR.
; IT IS TAKEN FROM PAGE 355 OF THE BOOK:
;
;        65816/65802 ASSEMBLY LANGUAGE PROGRAMMING
;        BY MICHAEL FISCHER
;        ISBN 0-07-881235-6
;
;        PUBLISHED BY: OSBORNE McGRAW-HILL
;                      2600 TENTH STREET
;                      BERKELEY, CA, USA
;                      94710
;
; AND AFTER ONLY FORMAT MODIFICATIONS FOR CROSS-32,
; IT IS PASSED ON AS AN EXAMPLE, FREE OF CHARGE,
; BY UNIVERSAL CROSS-ASSEMBLERS.

;Program to multily two multiple precision unsigned
;values in 16 bit and binary mode.  Maximum multiplier
;and multiplicand size is 4096 words.  Maximum product
;size is 8192 words.
           ORG   002000H
Mplier:    EQU   003000H      ;Multiplier Low first
Mpcand:    EQU   003100H      ;Multiplicand Low first
Product:   EQU   003200H
Size:      EQU   000300H      ;# of words in values
Pindex:    EQU   000302H
Numbits:   EQU   000304H
           CLC
           XCE                ;Native mode
           CLD                ;Binary mode
           REP   #30H         ;16 bit mode
           ASL   Size         ;Convert to bytes
           BEQ   Done         ;If n #s, no change
           LDA   Size
;Double number of bytes in multplier
           ASL   A
           TAX                ;Store as product index
           ASL   A
           ASL   A
           STA   Numbits      ;Bytes * 8 (2^3) = bits
           DEC   Size
           DEC   Size         ;Adjust as 16 bit index
           DEX
           DEX                ;Adjust as 16 bit index
           STX   Pindex
;Zero out product just to be sure
Clrprdct:  STZ   Product,X
           DEX
           DEX
           BNE   Clrprdct
           STZ   Product
Multloop:  LDX   Size
;Bet least significant bit of multiplier
MPShift:   ROR   Mplier,X
           DEX
           DEX
           BNE   MPShift
           ROR   Mplier
           BCC   NoAdd        ;If zero, get again
;Add multiplicand to product
           CLC
           LDX   Size
           LDY   Pindex
AddMpcnd:  LDA   Product,Y
           ADC   Mpcand, X
           STA   Product,Y
           DEY
           DEY
           DEX
           DEX
           BNE   AddMpcnd
           LDA   Product, Y
           ADC   Mpcand,X
           STA   Product, Y
NoAdd:     LDX   Pindex       ;Shift product right
           CLC
Prodshft:  ROR   Product,X
           DEX
           DEX
           BNE   Prodshft
           ROR   Product
           DEC   Numbits
           BNE   Multloop
Done:      SEC
           XCE
           RTS

           END
