[go: up one dir, main page]

Menu

[r813]: / src / lib / cmoc / mulww.c  Maximize  Restore  History

Download this file

66 lines (60 with data), 2.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// By Pierre Sarrazin <http://sarrazip.com/>.
// This file is in the public domain.
//
// Converted to C for WinCMOC by Derek Evans 2016
#include "_cmoc.h"
/*
** Multiples u by v to obtain a 32-bit product.
** Stores the high word of the product in *productHi,
** and returns the low word of the product.
*/
asm word mulww(word* productHi, word u, word v)
{
asm {
pshs u
tfr s,u
ldx 6,u // load u
ldd 8,u // load v
bsr MUL168_TO32
pshs x // preserve low word of product
ldx 4,u // get productHi pointer
std ,x // return high byte of product
puls a,b // retrieve and return low word of product
tfr u,s
puls u,pc
MUL168_TO32:
pshs x,b,a
clr ,-s // allocate 4 bytes to store 24-bit product
clr ,-s
clr ,-s
clr ,-s
lda 7,s // low byte of X
mul // mul by B
std 2,s // low 16-bit of 32-bit storage
ldd 5,s // original B in A, high byte of X in B
mul
addd 1,s // add D to middle 16 bits of storage
pshs cc // preserve carry
std 2,s // store sum in middle 16 bits of storage
puls b
andb #1 // carry from sum
stb ,s // store carry in high byte of storage
lda 4,s // original A
ldb 7,s // low byte of X
mul
addd 1,s // add D to middle 16 bits of storage
pshs cc // preserve carry
std 2,s // store sum in middle 16 bits of storage
puls b
andb #1 // carry from sum
addb ,s // add carry to high byte of storage
stb ,s
lda 4,s // original A
ldb 6,s // high byte of X
mul
addd ,s // add D to high word of storage
std ,s // drop carry
ldx 2,s // return low word of product in X
leas 8,s
}
}