]> git.sur5r.net Git - cc65/blob - libsrc/runtime/mul.s
goto.c warning fix for implicit truncation
[cc65] / libsrc / runtime / mul.s
1 ;
2 ; Ullrich von Bassewitz, 2009-08-17
3 ;
4 ; CC65 runtime: multiplication for ints
5 ;
6
7         .export         tosumulax, tosmulax
8         .import         mul8x16, mul8x16a       ; in mul8.s
9         .import         popptr1
10         .importzp       tmp1, ptr1, ptr4
11
12
13 ;---------------------------------------------------------------------------
14 ; 16x16 multiplication routine
15
16 tosmulax:
17 tosumulax:
18         sta     ptr4
19         txa                     ; High byte zero
20         beq     @L3             ; Do 8x16 multiplication if high byte zero
21         stx     ptr4+1          ; Save right operand
22         jsr     popptr1         ; Get left operand (Y=0, X untouched by popptr1)
23
24 ; Do ptr4:ptr4+1 * ptr1:ptr1+1 --> AX
25
26         tya                     ; A = 0        
27         ldy     ptr1+1          ; check if lhs is 8 bit only
28         beq     @L4             ; -> we can do 8x16 after swap
29         sta     tmp1
30         ldy     #16             ; Number of bits
31
32         lsr     ptr4+1
33         ror     ptr4            ; Get first bit into carry
34 @L0:    bcc     @L1
35
36         clc
37         adc     ptr1
38         tax
39         lda     ptr1+1          ; Hi byte of left op         
40         adc     tmp1
41         sta     tmp1
42         txa
43
44 @L1:    ror     tmp1
45         ror     a
46         ror     ptr4+1
47         ror     ptr4
48         dey
49         bne     @L0
50
51         lda     ptr4            ; Load the result
52         ldx     ptr4+1
53         rts                     ; Done
54
55 ; High byte of rhs is zero, jump to the 8x16 routine instead
56
57 @L3:    jmp     mul8x16
58
59 ; If the high byte of lhs is zero, swap the operands in ptr1/4 and
60 ; use the 8x16 routine. On entry, A and Y are zero and X has the value
61 ; of ptr4+1
62
63 @L4:    stx     ptr1+1          ; Store hi-byte from ptr4
64         ldy     ptr1            ; Save right operand (8 bit)
65         ldx     ptr4            ; Copy left 16 bit operand to right
66         stx     ptr1
67         sty     ptr4            ; Copy low 8 bit of right op to left
68         ldy     #8
69         jmp     mul8x16a        ; There, ptr4+1 will be also cleared
70