]> git.sur5r.net Git - cc65/blob - libsrc/runtime/imul8x8r16.s
cfg/atari-xex.cfg: fix typo in comment
[cc65] / libsrc / runtime / imul8x8r16.s
1 ;
2 ; 2010-11-02, Ullrich von Bassewitz
3 ; 2014-09-10, Greg King
4 ;
5 ; CC65 runtime: 8x8 => 16 signed multiplication
6 ;
7
8         .export         imul8x8r16, imul8x8r16m
9         .importzp       ptr1, ptr3, tmp1
10
11         .macpack        generic
12
13 ;---------------------------------------------------------------------------
14 ; 8x8 => 16 signed multiplication routine.
15 ;
16 ;   multiplicand  multiplier   product
17 ;   LHS             RHS        result
18 ; -------------------------------------------------------------
19 ;   .A (ptr3-low)   ptr1-low    .XA
20 ;
21
22 imul8x8r16:
23         sta     ptr3
24
25 imul8x8r16m:
26         ldx     #>$0000
27         bit     ptr3
28         bpl     @L7
29         dex
30 @L7:    stx     ptr3+1          ; Extend sign of Left-Hand Side
31         ldy     #<$0000         ; Clear .XY accumulator
32         ldx     #>$0000
33         lda     ptr1
34         bmi     NegMult
35         bpl     @L2             ; Branch always
36
37 @L0:    tya                     ; Add current multiplicand
38         add     ptr3
39         tay
40         txa
41         adc     ptr3+1
42         tax
43
44 @L1:    asl     ptr3
45         rol     ptr3+1
46 @L2:    lsr     ptr1            ; Get next bit of Right-Hand Side into carry
47         bcs     @L0
48         bnz     @L1             ; Loop if more one-bits in multiplier
49
50         tya                     ; Put result into cc65's accumulator
51         rts
52
53 ; The multiplier is negative.
54 ; Therefore, make it positive; and, subtract when multiplying.
55 NegMult:
56         eor     #%11111111
57         sta     ptr1
58         inc     ptr1
59         bnz     @L2             ; Branch always
60
61 @L0:    tya                     ; Subtract current multiplicand
62         sub     ptr3
63         tay
64         txa
65         sbc     ptr3+1
66         tax
67
68 @L1:    asl     ptr3
69         rol     ptr3+1
70 @L2:    lsr     ptr1            ; Get next bit of Right-Hand Side into carry
71         bcs     @L0
72         bnz     @L1             ; Loop if more one-bits in multiplier
73
74         tya                     ; Put result into cc65's accumulator
75         rts