Author Topic: Routines  (Read 313575 times)

0 Members and 1 Guest are viewing this topic.

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: Routines
« Reply #495 on: December 14, 2010, 04:02:23 pm »
That looks neat!  Nice routine Sir! ;D

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Routines
« Reply #496 on: December 14, 2010, 05:50:10 pm »
I believe I posted some normal sorting routines a while ago, but there are probably many more times when a linked sort is in order. So here are two linked sorting libraries. LSORTBLB sorts a list of bytes whereas LSORTWLB sorts a list of words, and both are linked to a list of words (which will typically be pointers). These routines allow you to sort objects based on certain attributes.

The general syntax for a sort call:
Code: [Select]
sub(___, Pointer to list of values to sort, Pointer to list of words to rearrange based on sort, Number of bytes to sort)Note: If you are sorting bytes, the last argument will be the number of values to sort. If you are sorting words, the last argument will be the number of values times 2.

Routines in LSORTBLB:
  • SBA - Sort bytes in ascending order
  • SBD - Sort bytes in descending order

Routines in LSORTWLB:
  • SWA - Sort words in ascending order
  • SWD - Sort words in descending order



As an example, say your program was managing 256 objects, and each object has 4 bytes of data: [x position, y position, x velocity, y velocity]. You have a list of pointers to the objects located at P. Let's say you want to sort the objects in this list by descending x velocity.
Code: [Select]
.Move attributes to be sorted by to a sequential section of memory
For(A,0,255)
{{A*2+P}+2}→{A+L₁}
End
.Sort
sub(SBD,L₁,P,256)
« Last Edit: December 14, 2010, 06:09:46 pm by Runer112 »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Routines
« Reply #497 on: December 15, 2010, 12:47:04 am »
Oh a sorting routine? Nice! This might be handy eventually if I was to work on an item menu like Illusiat 13 :D

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: Routines
« Reply #498 on: December 15, 2010, 04:45:07 pm »
Wow, that looks nice!  Great job. :)

Ashbad

  • Guest
Re: Routines
« Reply #499 on: December 15, 2010, 05:18:06 pm »
Draw an elliptical shape like an oval/circle

Code: [Select]
For(A,0,255
sin(A)//W->S
cos(A)//H->C
pxl-on(X+S,Y-C
End

W = Width inverse, the smaller the value, the wider the circle.  16 draws with outward radius of ~10 pixels
H = Height inverse, the smaller the value, the taller the circle.  16 draws with outward radius of ~10 pixels
A = used in for statement as the intermediate "angle"
S = intermediate sine value
C = intermediate cosine value
X = Elliptical center X
Y = Elliptical center Y
« Last Edit: December 15, 2010, 05:18:43 pm by Ashbad »

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Routines
« Reply #500 on: December 15, 2010, 05:25:31 pm »
Code: [Select]
For(A,0,255
pxl-on(sin(A)//W+X,Y-(cos(A)//H)
End
yay optimization


Ashbad

  • Guest
Re: Routines
« Reply #501 on: December 15, 2010, 05:27:12 pm »
yay ;)

funny thing is, until I wrote the routine and looked up what a sine and cosine is, I had NO IDEA what they did O.O

I mean, I used before in Alg honors last year, but it was the last chapter for honors only, which was a crash course of trig basics :P

..and we just used the sin/cos functions like once and were never told what they did :P
« Last Edit: December 15, 2010, 05:28:06 pm by Ashbad »

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Routines
« Reply #502 on: December 15, 2010, 07:04:38 pm »
Code: [Select]
For(A,0,255
pxl-on(sin(A)//W+X,Y-(cos(A)//H)
End
yay optimization

Code: [Select]
256→A
While
Pxl-On(sin(A)//W+X,sin(A+64)//H+Y)
A-1→A
End

Good fight ;)

And just a note: Unless you absolutely want width and height to be inverted, multiplying instead of signed dividing is probably better. The multiplication routine is smaller and faster than the signed division routine.
« Last Edit: December 15, 2010, 07:06:21 pm by Runer112 »

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Routines
« Reply #503 on: December 15, 2010, 07:09:05 pm »
Code: [Select]
257
While -1→A
Pxl-On(sin(A)//W+X,sin(A+64)//H+Y)
A
End

Even better ;)




Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Routines
« Reply #504 on: December 15, 2010, 07:16:02 pm »
Ah how did I not see that!

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Routines
« Reply #505 on: December 15, 2010, 07:18:16 pm »
Code: [Select]
257
While -1→A
Pxl-On(sin()//W+X,sin(A+64)//H+Y)
A
End

I should stop.




Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Routines
« Reply #506 on: December 16, 2010, 04:29:06 am »
Nice routine guys!

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Routines
« Reply #507 on: December 19, 2010, 10:43:15 pm »
FinaleTI was wondering how he could do some basic operations on 3-byte numbers, namely addition, subtraction, and decimal display. It took me a while to get it all working, but I wrote an Axe library to do all of the above. And while I was at it, because it would only require minor modifications to this code, I wrote a similar library to perform those three operations on 4-byte numbers. No longer shall Axe be limited to 2 bytes! (At least regarding addition, subtraction, and decimal display)

Anyways, the two libraries are very similar. They accept inputs in the form of pointers to the 3- or 4-byte numbers you want to operate on and have the same three functions:
  • Addition: sub(ADD,Pointer to first argument, Pointer to second argument, Pointer to store answer to)
  • Subtraction: sub(SUB, Pointer to first argument, Pointer to second argument, Pointer to store answer to)
  • Decimal display: Disp Pointer to number to display sub(DEC) - Note that this does not return a string of a fixed length padded on the left with spaces like the usual Disp ►Dec. The string returned has any leading zeros clipped and consists only of the number itself.


To include the library in your program, have the library you want present on your calculator and insert a line reading ":prgmLIB3BYTE" or ":prgmLIB4BYTE" in the subroutine section of your code. The syntax for the commands is also included as comments under each function in the library source files, so you can look there if you need a refresher.


NOTE: The decimal display routine uses OP4-OP5 for scratch memory and OP6 to store the resulting string, so if you're wondering why any data you store there is mysteriously being corrupted, that's why.


EDIT: I just realized there was a slight flaw with my code. But I fixed it and reuploaded the two libraries before anyone could download them (I think). That's why the "by Runer112" is on the end of the filenames, I wasn't sure what else to add.
« Last Edit: December 20, 2010, 02:53:11 am by Runer112 »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Routines
« Reply #508 on: December 19, 2010, 10:50:45 pm »
Hmm question, you say pointer to first argument, but I assume this grabs 3 numbers starting there, right? Just wondering. Also does the routine allows you to display such number without adding/substracting? I am curious since this might be useful for games where money or scores can go above 65536, for example.

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Routines
« Reply #509 on: December 19, 2010, 10:56:20 pm »
Yes and yes. The arguments should be pointers to where you store the values, not the actual values, because the actual values wouldn't fit in Axe's 2-byte value system. For instance, if you had a 4-byte number spanning variables A-B in memory and a 4-byte number spanning variables C-D, you could add the two values and store the result in A-B like so:
Code: [Select]
sub(ADD,°C,°A,)(Note the trick employed here. You can do this if you are adding two values and storing the result back into one of the values. Enter the pointer to the value that should serve as both an argument and the answer as the second argument and then leave the third argument blank.)

The display routines can display any number that you can fit in the 3 or 4 bytes of data: 0-16777215 for the 3-byte library and 0-4294967295 for the 4-byte library.



EDIT: Here's a little program I whipped up using the 4-byte library, showing its capabilities. It pases 65536 with ease, but unfortunately it can only go up to about 4 billion. ;) You might recognize the pattern...
(Note that the actual routines run a lot faster than this, I manually set it to iterate only twice per second)

You can look at the source for an example of how to use the commands.
« Last Edit: December 20, 2010, 02:53:43 am by Runer112 »