]> git.sur5r.net Git - cc65/blob - libsrc/runtime/umul16x16r32.s
9ecd1596ebb2f558e1f4d03a8120d44325c1e309
[cc65] / libsrc / runtime / umul16x16r32.s
1 ;
2 ; Ullrich von Bassewitz, 2010-11-03
3 ;
4 ; CC65 runtime: 16x16 => 32 unsigned multiplication
5 ;
6
7         .export         umul16x16r32, umul16x16r32m
8         .export         umul16x16r16, umul16x16r16m
9
10         .include        "zeropage.inc"
11
12
13 ;---------------------------------------------------------------------------
14 ; 16x16 => 32 unsigned multiplication routine. Because the overhead for a
15 ; 16x16 => 16 unsigned multiplication routine is small, we will tag it with 
16 ; the matching labels, as well.
17 ;
18 ;  routine         LHS         RHS        result          result also in
19 ; -----------------------------------------------------------------------
20 ;  umul16x16r32    ax          ptr1       ax:sreg          ptr1:sreg
21 ;  umul16x16r32m   ptr3        ptr1       ax:sreg          ptr1:sreg
22 ;  umul16x16r16    ax          ptr1       ax               ptr1
23 ;  umul16x16r16m   ptr3        ptr1       ax               ptr1
24 ;
25 ; ptr3 is left intact by the routine.
26 ;
27
28 umul16x16r32:
29 umul16x16r16:
30         sta     ptr3
31         stx     ptr3+1
32
33 umul16x16r32m:
34 umul16x16r16m:
35         lda     #0
36         sta     sreg+1
37         ldy     #16             ; Number of bits
38
39         lsr     ptr1+1
40         ror     ptr1            ; Get first bit into carry
41 @L0:    bcc     @L1
42
43         clc
44         adc     ptr3
45         pha
46         lda     ptr3+1
47         adc     sreg+1
48         sta     sreg+1
49         pla
50
51 @L1:    ror     sreg+1
52         ror     a
53         ror     ptr1+1
54         ror     ptr1
55         dey
56         bne     @L0
57
58         sta     sreg            ; Save byte 3
59         lda     ptr1            ; Load the result
60         ldx     ptr1+1
61         rts                     ; Done
62
63