Omnimaga
Calculator Community => TI Calculators => Axe => Topic started by: FinaleTI on December 19, 2010, 06:35:14 pm
-
I was looking at this (http://bulbapedia.bulbagarden.net/wiki/Pok%C3%A9mon_data_structure_in_Generation_I) page to see what kind of data structure I'd need for Pokemon TI, but for experience, it says a 3 byte var was used.
If someone could give me a routine for addition, subtraction, and displaying a 3 byte var (or 4 byte if necessary), I would greatly appreciate it.
-
An earlier thread asked how to do this too, so I have a bit of knowledge regarding this already. The following routines all accept inputs in the form of:
sub(___, Pointer to first argument, Pointer to second argument, Pointer to store answer to)
This routine adds 2 4-byte numbers and stores the result to another 4-byte number:
:Lbl ADD
:{r₁}ʳ→r₅+{r₂}ʳ→r₄
:<r₅+{r₁+2}ʳ+{r₂+2}ʳ
:→{r₄→{r₃}ʳ+1}ʳ
:Return
Here is a slightly modified version to work with 3-byte numbers instead:
:Lbl ADD
:{r₁}ʳ→r₅+{r₂}ʳ→r₄
:<r₅+{r₁+2}+{r₂+2}
:→{r₄→{r₃}ʳ+1}
:Return
And here are their subtraction counterparts:
:Lbl SUB
:{r₁}ʳ→r₄≥({r₂}ʳ→r₅)
:-1+{r₁+2}ʳ+{r₂+2}ʳ
:→{r₄-r₅→{r₃}ʳ+1}ʳ
:Return
:Lbl SUB
:{r₁}ʳ→r₄≥({r₂}ʳ→r₅)
:-1+{r₁+2}-{r₂+2}
:→{r₄-r₅→{r₃}ʳ+1}
:Return
-
Awesome!
Now if anyone knows how to display a 3 byte var...
-
I know there's a DCS routine to display a three byte var. :) You may want to ask Kerm about it.
-
Let's see, addition of 3 byte vars. In Axe. Sounds like fun. Okay, let's see. Assuming A is the MSB, and B has the LSW. For the second number, C=MSB, and D has the LSW. Unsigned, I'm assuming. LSW=Least significant word (2-bytes). MSB=most significant byte. mSB=middle significant byte. LSB=Least significant byte.
It is essentially D+B is the LSW. And (D+B-65535)+A+B is the MSB. Note, that assumes D+B>65535. If it's not, then you get the wrong solution, so check that beforehand. Note, that with assembly, I can think of a better routine than that. Example: (Note, X >> 8 means shift X 8 bits to the right)
0xECD182. 0xEC=A 0xD182=B
0x9287F6. 0x92=C 0x87F6=D
B & 0x00FF = 0082h = E
D & 0x00FF = 00F6h = F
E+F = 0x0178 = G
G >> 8 = 0x0001 = H
B >> 8 = I
D >> 8 = J
H+I+J = 0x959 = K
K >> 8 = L
L+A+B = 0x0187 = M
(G&0x00FF)+(K<<8) = N
M is now your MSW, and N is the LSW.
So, your number is MN or 0x0187 5978.
I think that should work. Good luck with that. I don't know about subtraction, though. ;-)
EDIT: triple-ninja'd.
-
I'm guessing you would want the values displayed in their base-10 representations, but how would you want the resulting strings to be formatted? Would you want them:
- Right-aligned in a block of 8 characters, padded with spaces? Like: " 12345"
- Or would you want the string to have variable length with the leading zeros/spaces cut away? Like: "12345"
EDIT:
I went ahead under the assumption that you would want the second option. So I wrote up an Axe library that has everything you need: 3-byte addition, 3-byte subtraction, and 3-byte decimal display. The whole thing is somewhat large (371 bytes), but I optimized it as much as I could.
And since it isn't that much more of an extension, I also made an Axe library that has routines for 4-byte addition, subtraction, and decimal display (407 bytes). I figure even if you don't end up wanting it, I'm sure it could be very handy for other Axe programmers. I know at least a few others have actually made threads asking about using 4-byte numbers before.
Because this turned out to be a pretty nice tool for Axe programmers in general, I posted it in the Routines thread. You can find the post and download the library/libraries you want here (http://ourl.ca/4129/155369).