Author Topic: 3 byte vars?  (Read 3934 times)

0 Members and 1 Guest are viewing this topic.

Offline FinaleTI

  • Believe in the pony that believes in you!
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1830
  • Rating: +121/-2
  • Believe in the pony that believes in you!
    • View Profile
    • dmuckerman.tumblr.com
3 byte vars?
« on: December 19, 2010, 06:35:14 pm »
I was looking at this 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.


Spoiler For Projects:

My projects haven't been worked on in a while, so they're all on hiatus for the time being. I do hope to eventually return to them in some form or another...

Spoiler For Pokemon TI:
Axe port of Pokemon Red/Blue to the 83+/84+ family. On hold.

Spoiler For Nostalgia:
My big personal project, an original RPG about dimensional travel and a few heroes tasked with saving the world.
Coding-wise, on hold, but I am re-working the story.

Spoiler For Finale's Super Insane Tunnel Pack of Doom:
I will be combining Blur and Collision Course into a single gamepack. On hold.

Spoiler For Nostalgia Origins: Sky's Story:
Prequel to Nostalgia. On hold, especially while the story is re-worked.

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: 3 byte vars?
« Reply #1 on: December 19, 2010, 06:54:46 pm »
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:
Code: (4-byte addition) [Select]
: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:
Code: (3-byte addition) [Select]
:Lbl ADD
:{r₁}ʳ→r₅+{r₂}ʳ→r₄
:<r₅+{r₁+2}+{r₂+2}
:→{r₄→{r₃}ʳ+1}
:Return


And here are their subtraction counterparts:
Code: (4-byte subtraction) [Select]
:Lbl SUB
:{r₁}ʳ→r₄≥({r₂}ʳ→r₅)
:-1+{r₁+2}ʳ+{r₂+2}ʳ
:→{r₄-r₅→{r₃}ʳ+1}ʳ
:Return

Code: (3-byte subtraction) [Select]
:Lbl SUB
:{r₁}ʳ→r₄≥({r₂}ʳ→r₅)
:-1+{r₁+2}-{r₂+2}
:→{r₄-r₅→{r₃}ʳ+1}
:Return
« Last Edit: December 19, 2010, 09:55:09 pm by Runer112 »

Offline FinaleTI

  • Believe in the pony that believes in you!
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1830
  • Rating: +121/-2
  • Believe in the pony that believes in you!
    • View Profile
    • dmuckerman.tumblr.com
Re: 3 byte vars?
« Reply #2 on: December 19, 2010, 06:56:55 pm »
Awesome!
Now if anyone knows how to display a 3 byte var...


Spoiler For Projects:

My projects haven't been worked on in a while, so they're all on hiatus for the time being. I do hope to eventually return to them in some form or another...

Spoiler For Pokemon TI:
Axe port of Pokemon Red/Blue to the 83+/84+ family. On hold.

Spoiler For Nostalgia:
My big personal project, an original RPG about dimensional travel and a few heroes tasked with saving the world.
Coding-wise, on hold, but I am re-working the story.

Spoiler For Finale's Super Insane Tunnel Pack of Doom:
I will be combining Blur and Collision Course into a single gamepack. On hold.

Spoiler For Nostalgia Origins: Sky's Story:
Prequel to Nostalgia. On hold, especially while the story is re-worked.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: 3 byte vars?
« Reply #3 on: December 19, 2010, 07:00:02 pm »
I know there's a DCS routine to display a three byte var. :)  You may want to ask Kerm about it.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: 3 byte vars?
« Reply #4 on: December 19, 2010, 07:25:00 pm »
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.
« Last Edit: December 19, 2010, 07:25:38 pm by graphmastur »

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: 3 byte vars?
« Reply #5 on: December 19, 2010, 08:22:36 pm »
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.
« Last Edit: December 19, 2010, 10:47:18 pm by Runer112 »