]> git.sur5r.net Git - cc65/blob - libsrc/runtime/mul8.s
Changes due to code review.
[cc65] / libsrc / runtime / mul8.s
1 ;
2 ; Ullrich von Bassewitz, 2009-08-17
3 ;
4 ; CC65 runtime: multiplication for ints. Short versions.
5 ;
6
7         .export         tosumula0, tosmula0
8         .export         mul8x16, mul8x16a
9         .import         popptr1
10         .importzp       ptr1, ptr4
11
12
13 ;---------------------------------------------------------------------------
14 ; 8x16 routine with external entry points used by the 16x16 routine in mul.s
15
16 tosmula0:
17 tosumula0:
18         sta     ptr4
19 mul8x16:jsr     popptr1         ; Get left operand (Y=0 by popptr1)
20
21         tya                     ; Clear byte 1
22         ldy     #8              ; Number of bits
23         ldx     ptr1+1          ; check if lhs is 8 bit only
24         beq     mul8x8          ; Do 8x8 multiplication if high byte zero
25 mul8x16a:
26         sta     ptr4+1          ; Clear byte 2
27
28         lsr     ptr4            ; Get first bit into carry
29 @L0:    bcc     @L1
30
31         clc
32         adc     ptr1
33         tax
34         lda     ptr1+1          ; hi byte of left op
35         adc     ptr4+1
36         sta     ptr4+1
37         txa
38
39 @L1:    ror     ptr4+1
40         ror     a
41         ror     ptr4
42         dey
43         bne     @L0
44         tax
45         lda     ptr4            ; Load the result
46         rts
47
48 ;---------------------------------------------------------------------------
49 ; 8x8 multiplication routine
50
51 mul8x8:
52         lsr     ptr4            ; Get first bit into carry
53 @L0:    bcc     @L1
54         clc
55         adc     ptr1
56 @L1:    ror
57         ror     ptr4
58         dey
59         bne     @L0
60         tax
61         lda     ptr4            ; Load the result
62         rts                     ; Done
63