* support/regression/tests/bug1057979.c:
[fw/sdcc] / device / lib / z80 / mul.s
1         .area   _CODE
2
3 ; This multiplication routine is similar to the one
4 ; from Rodnay Zaks, "Programming the Z80".
5
6 __muluchar_rrx_s::
7         ld      hl, #2
8         add     hl, sp
9         ld      e, (hl)
10         inc     hl
11         ld      h, (hl)
12         ld      l, #0
13         ld      d, l
14         ld      b, #8
15 muluchar_rrx_s_loop:
16         add     hl, hl
17         jr      nc, muluchar_rrx_s_noadd
18         add     hl, de
19 muluchar_rrx_s_noadd:
20         djnz    muluchar_rrx_s_loop
21         ret
22
23 ;; Originally from GBDK by Pascal Felber.
24
25 __mulschar_rrx_s::
26         ld      hl,#2
27         add     hl,sp
28
29         ld      e,(hl)
30         inc     hl
31         ld      l,(hl)
32
33         ;; Fall through
34 __mulschar_rrx_hds::
35         ;; Need to sign extend before going in.
36         ld      c,l
37
38         ld      a,l
39         rla
40         sbc     a,a
41         ld      b,a
42
43         ld      a,e
44         rla
45         sbc     a,a
46         ld      d,a
47
48         jp      .mul16
49
50 __mulint_rrx_s::
51         ld      hl,#2
52         add     hl,sp
53
54         ld      e,(hl)
55         inc     hl
56         ld      d,(hl)
57         inc     hl
58         ld      a,(hl)
59         inc     hl
60         ld      h,(hl)
61         ld      l,a
62
63         ;; Fall through
64
65 __muluchar_rrx_hds::
66 __mulint_rrx_hds::
67         ;; Parameters:
68         ;;      HL, DE (left, right irrelivent)
69         ld      b,h
70         ld      c,l
71
72         ;; 16-bit multiplication
73         ;;
74         ;; Entry conditions
75         ;;   BC = multiplicand
76         ;;   DE = multiplier
77         ;;
78         ;; Exit conditions
79         ;;   DE = less significant word of product
80         ;;
81         ;; Register used: AF,BC,DE,HL
82 .mul16:
83         ld      hl,#0
84         ld      a,b
85         ; ld c,c
86         ld      b,#16
87
88         ;; Optimise for the case when this side has 8 bits of data or
89         ;; less.  This is often the case with support address calls.
90         or      a
91         jp      NZ,1$
92
93         ld      b,#8
94         ld      a,c
95 1$:
96         ;; Taken from z88dk, which originally borrowed from the
97         ;; Spectrum rom.
98         add     hl,hl
99         rl      c
100         rla                     ;DLE 27/11/98
101         jr      NC,2$
102         add     hl,de
103 2$:
104         djnz    1$
105         ret