summaryrefslogtreecommitdiff
path: root/common/math.asm
diff options
context:
space:
mode:
authorBryan Bishop <kanzure@gmail.com>2013-09-09 16:00:05 -0500
committerBryan Bishop <kanzure@gmail.com>2013-09-09 16:00:05 -0500
commit33d7ef72fef045198f0ab472332e6472a54a034f (patch)
treeb2a7700302bac3f0377ca2f841b61d09e8e1fb0b /common/math.asm
parent3a2dbb5289e745dfb5f26844148c0981f14e8fcf (diff)
parent608d0d86e47b09f1c1ddbcf3b9c51e0ac628d7ed (diff)
Merge branch 'yenatch/split-predefs-specials-stds' into fix-split-predefs-specials-stds
https://github.com/kanzure/pokecrystal/pull/198
Diffstat (limited to 'common/math.asm')
-rw-r--r--common/math.asm76
1 files changed, 76 insertions, 0 deletions
diff --git a/common/math.asm b/common/math.asm
new file mode 100644
index 000000000..87a00c412
--- /dev/null
+++ b/common/math.asm
@@ -0,0 +1,76 @@
+SimpleMultiply: ; 3105
+; Return a * c.
+ and a
+ ret z
+
+ push bc
+ ld b, a
+ xor a
+.loop
+ add c
+ dec b
+ jr nz, .loop
+ pop bc
+ ret
+; 3110
+
+
+SimpleDivide: ; 3110
+; Divide a by c. Return quotient b and remainder a.
+ ld b, 0
+.loop
+ inc b
+ sub c
+ jr nc, .loop
+ dec b
+ add c
+ ret
+; 3119
+
+
+Multiply: ; 3119
+; Multiply hMultiplicand (3 bytes) by hMultiplier. Result in hProduct.
+; All values are big endian.
+ push hl
+ push bc
+
+ callab _Multiply
+
+ pop bc
+ pop hl
+ ret
+; 3124
+
+
+Divide: ; 3124
+; Divide hDividend length b (max 4 bytes) by hDivisor. Result in hQuotient.
+; All values are big endian.
+ push hl
+ push de
+ push bc
+ ld a, [hROMBank]
+ push af
+ ld a, BANK(_Divide)
+ rst Bankswitch
+
+ call _Divide
+
+ pop af
+ rst Bankswitch
+ pop bc
+ pop de
+ pop hl
+ ret
+; 3136
+
+
+SubtractSigned: ; 3136
+; Return a - b, sign in carry.
+ sub b
+ ret nc
+ cpl
+ add 1
+ scf
+ ret
+; 313d
+