* device/lib/z80/mul.s (__mulsuchar_rrx_s, __muluschar_rrx_s),
[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+1
8         add     hl, sp
9         ld      e, (hl)
10         dec     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 ; operands have different sign
24
25 __mulsuchar_rrx_s::
26         ld      hl,#2+1
27         add     hl,sp
28
29         ld      e,(hl)
30         dec     hl
31         ld      c,(hl)
32         ld      b, #0
33         jr      signexte
34
35 __muluschar_rrx_s::
36         ld      hl,#2+1
37         add     hl,sp
38
39         ld      c,(hl)
40         ld      b, #0
41         dec     hl
42         ld      e,(hl)
43         jr      signexte
44
45 ;; Originally from GBDK by Pascal Felber.
46
47 __mulschar_rrx_s::
48         ld      hl,#2+1
49         add     hl,sp
50
51         ld      e,(hl)
52         dec     hl
53         ld      l,(hl)
54
55         ;; Fall through
56 __mulschar_rrx_hds::
57         ;; Need to sign extend before going in.
58         ld      c,l
59
60         ld      a,l
61         rla
62         sbc     a,a
63         ld      b,a
64 signexte:
65         ld      a,e
66         rla
67         sbc     a,a
68         ld      d,a
69
70         jr      .mul16
71
72 __mulint_rrx_s::
73         ld      hl,#2
74         add     hl,sp
75
76         ld      e,(hl)
77         inc     hl
78         ld      d,(hl)
79         inc     hl
80         ld      a,(hl)
81         inc     hl
82         ld      h,(hl)
83         ld      l,a
84
85         ;; Fall through
86
87 __muluchar_rrx_hds::
88 __mulint_rrx_hds::
89         ;; Parameters:
90         ;;      HL, DE (left, right irrelevant)
91         ld      b,h
92         ld      c,l
93
94         ;; 16-bit multiplication
95         ;;
96         ;; Entry conditions
97         ;;   BC = multiplicand
98         ;;   DE = multiplier
99         ;;
100         ;; Exit conditions
101         ;;   DE = less significant word of product
102         ;;
103         ;; Register used: AF,BC,DE,HL
104 .mul16:
105         ld      hl,#0
106         ld      a,b
107         ; ld c,c
108         ld      b,#16
109
110         ;; Optimise for the case when this side has 8 bits of data or
111         ;; less.  This is often the case with support address calls.
112         or      a
113         jr      NZ,1$
114
115         ld      b,#8
116         ld      a,c
117 1$:
118         ;; Taken from z88dk, which originally borrowed from the
119         ;; Spectrum rom.
120         add     hl,hl
121         rl      c
122         rla                     ;DLE 27/11/98
123         jr      NC,2$
124         add     hl,de
125 2$:
126         djnz    1$
127         ret