’Cause all games were better on the GBC

You are not logged in.

- Index
- → Help/Question
- →
**Mutiplication/Division and Fraction/Decimal Arithmetic on GameBoy**

Pages: **1**

**visionseeming****New member**- Registered: 2011-07-19
- Post 3/7

I was going through the instruction set for the GB CPU and it seems that instructions are not available for the multiplication or division of arbitrary integers. I know the rotate commands can be used for multiplication or division by powers of 2, but I'm assuming the rest has to be done by the programmer. For Pokemon Red, looking at Marckus' disassembly, it seems there is a routine for the multiplication of two integers--addNtimes--so I'm assuming there is something for division as well.

Also, I was wondering how decimals or fractions are dealt with. I know in the experience calculation formula there are some decimals, so I'm assuming some type of fixed or floating point arithmetic routines are available in the engine as well?

"To some people, Pokemon hacks are just games. Others use them for battling/trading. Us? We study the hacks like a profession. But we don't know everything yet. There are still many mysteries left to be solved. That's why we hack Pokemon every day."--Mateo

Offline

**Tauwasser****Member**- Registered: 2010-10-16
- Post 198/448

Hi,

Multiplication can be implemented as a simple addition loop. Division can be implemented the same way using subtraction (without a fractional result). If you need fractions, you will have to roll your own for the GB-CPU, since there is no standard. You might get away with fixed-point values for small stuff, but real fractions e.g. for a software 3d renderer, are likely either fixed-point as well or just implementations of regular floats, which would some time to compute.

cYa,

Tauwasser

Offline

**visionseeming****New member**- Registered: 2011-07-19
- Post 4/7

Any idea what method is used in Pokemon? As far as I know, the only place fractions or decimals are used is in the experience to level conversion, so I suppose maybe they used fixed point? For division of integers repeated subtraction while counting would work as you mentioned, so I'm guessing that's whats used.

"To some people, Pokemon hacks are just games. Others use them for battling/trading. Us? We study the hacks like a profession. But we don't know everything yet. There are still many mysteries left to be solved. That's why we hack Pokemon every day."--Mateo

Offline

**Tauwasser****Member**- Registered: 2010-10-16
- Post 199/448

Indeed, for experience, fixed point arithmetic is used.

The Color games use a different trick to achieve faster results: They use powers of two to quickly shift the input right and the result left instead of computing everything. You use the same logic when dividing numbers manually using the decimal system:

Problem: 1010 : 05 Result = 0;

1010

-05

0510 Result++

-05

0010 Result++

-05

< 0 --> move to next digit (i.e. divisor /= 10, result *= 10)

Result = 20;

0010

- 05

< 0 --> move to next digit (i.e. divisor /= 10, result *= 10)

Result = 200;

0010

- 05

0005 Result++

- 05

0000 Result++

- 05

< 0 --> we don't have any more digits, we're done.

Result = 202;

Think of it as being the same only moving byte-wise in base 2 and shifting the divisor and result by one bit left and right respectively (i.e. multiplying with/dividing by the base).

cYa,

Tauwasser

Offline

Pages: **1**

- Index
- → Help/Question
- →
**Mutiplication/Division and Fraction/Decimal Arithmetic on GameBoy**