struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / sm83 / mul.s
blobeefe322c908ff802d4527f65e2f2c5a117c71a77
1 ;--------------------------------------------------------------------------
2 ; mul.s
4 ; Copyright (C) 2000, Michael Hope
5 ; Copyright (C) 2021-2022, Sebastian 'basxto' Riedel (sdcc@basxto.de)
6 ; Copyright (c) 2021, Philipp Klaus Krause
8 ; This library is free software; you can redistribute it and/or modify it
9 ; under the terms of the GNU General Public License as published by the
10 ; Free Software Foundation; either version 2, or (at your option) any
11 ; later version.
13 ; This library is distributed in the hope that it will be useful,
14 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ; GNU General Public License for more details.
18 ; You should have received a copy of the GNU General Public License
19 ; along with this library; see the file COPYING. If not, write to the
20 ; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 ; MA 02110-1301, USA.
23 ; As a special exception, if you link this library with other files,
24 ; some of which are compiled with SDCC, to produce an executable,
25 ; this library does not by itself cause the resulting executable to
26 ; be covered by the GNU General Public License. This exception does
27 ; not however invalidate any other reasons why the executable file
28 ; might be covered by the GNU General Public License.
29 ;--------------------------------------------------------------------------
31 ;; Originally from GBDK by Pascal Felber.
33 .area _CODE
35 .globl __mulsuchar
36 .globl __muluschar
37 .globl __mulschar
38 .globl __muluchar
39 .globl __mulint
41 ; operands with different sign
43 __mulsuchar:
44 ld c, a
45 jr signexte
47 __muluschar:
48 ld c, e
49 ld e, a
51 signexte:
52 ld a,e
53 rla
54 sbc a,a
55 ld d,a
57 xor a
58 jr .mul8
60 __mulschar:
61 ; Sign-extend before going in.
62 ld c,a
64 rla
65 sbc a,a
66 ld b,a
68 ld a,e
69 rla
70 sbc a,a
71 ld d,a
73 __mulint:
74 ;; 16-bit multiplication
76 ;; Entry conditions
77 ;; BC = multiplicand
78 ;; DE = multiplier
80 ;; Exit conditions
81 ;; BC = less significant word of product
83 ;; Register used: AF,BC,DE,HL
84 .mul16:
85 ;; Let the smaller number loop
86 ld a,b
87 cp a,d
88 jr c, keep
89 ;; d <= b
90 ld a, e
91 ld e, c
92 ld c, a
93 ld a, d
94 ld d, b
95 ld b, a
96 keep:
97 ;; Optimise for the case when this side has 8 bits of data or
98 ;; less. This is often the case with support address calls.
99 or a
100 jp Z, .mul8
102 ld l,#0
103 ld b,#16
104 loop16:
105 ;; Taken from z88dk, which originally borrowed from the
106 ;; Spectrum rom.
107 add hl,hl
108 rl c
109 rla ;DLE 27/11/98
110 jr NC,skip16
111 add hl,de
112 skip16:
113 dec b
114 jr NZ,loop16
116 ;; Return in bc
117 ld c,l
118 ld b,h
122 __muluchar:
123 ld c, a
124 xor a
125 ;; Clear the top
126 ld d, a
128 ;; Version that uses an 8bit multiplicand
130 ;; Entry conditions
131 ;; C = multiplicand
132 ;; DE = multiplier
133 ;; A = 0
135 ;; Exit conditions
136 ;; BC = less significant word of product
138 ;; Register used: AF,BC,DE,HL
139 .mul8:
140 ld l,a
141 ld b,#8
142 ld a,c
143 loop8:
144 add hl,hl
146 jr NC,skip8
147 add hl,de
148 skip8:
149 dec b
150 jr NZ,loop8
152 ;; Return in bc
153 ld c,l
154 ld b,h