Author Topic: scripts and code snippets  (Read 10919 times)

0 Members and 1 Guest are viewing this topic.

metagross111

  • Guest
Re: scripts and code snippets
« Reply #15 on: December 08, 2008, 05:45:54 pm »
whoa, i wasn't aware this topic existed! its got some interesting stuff.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: scripts and code snippets
« Reply #16 on: April 22, 2009, 12:45:41 am »
here is a good one for people trying to fit in all their subprograms into 1 program.

While 0
Lbl A
Disp "This is inside a subprogram
End

For(F,-1,0
If F
Goto A
End


It allows the program execution to jump to any place in the program, and then return back to where it was called.  And it causes no Memory leaks, so your program will not slow down because of it!

You can even use the subprogram inside your code instead of just pasting it at the begining with a While 0 in front of it

Repeat 1
Lbl A
Disp "This will be executed once
Disp "And then again froma subProgram
End

For(F,-1,0
If F
Goto A
End


That way you can have at least one part in your code where it does't need to jump to the subprogram, because everything else is jumping to it!

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: scripts and code snippets
« Reply #17 on: April 22, 2009, 01:36:19 am »
mhmm I'll study this a bit, it will prbly take a while for me to get used to this structure, because I just started coding 2 months ago for the first time in 18 months and most of my programming activity was around 2002, when using lbl/goto anywhere without caring about memory leaks was legion. On top of that I have trouble learning and understanding stuff it seems (probably the reason why I sucked so much at reading tests and why I never managed to succeed at learning assembly, even after 3 hard tries) It might be useful to merge some subroutines together or something. Maybe not in Illusiat, though, because almost everything needs to be stored into archive and only copied temporarly in RAM when needed (sub-routines, map data, etc.) due to the incredible file size and amount of maps (over 200 so far and game is only 20% complete)

Offline nitacku

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 300
  • Rating: +30/-1
  • ni-ta-ku ^_^
    • View Profile
Re: scripts and code snippets
« Reply #18 on: April 23, 2009, 05:40:56 pm »
I used a similar method in a number of my programs.
You want to make sure that your "sub-program" is located at the top of the program because the time it takes for the interpreter to locate the Lbl is directly proportional to its location from the top of the program.

The main advantage of this method is you can send information via Ans, and you don't use any variables.

Start of program:
:Code
:More code
:Goto 0 (you have to skip to the main code since this next part will be the sub-programs)
:Lbl 1
:woot!
:subprogram
:0 (needed so that the loop will end)
:End (normally this would be extraneous, but we induced a temporary memory leak that this will fix)
:Lbl 0 (start of main code)
:code
:stuff
:the last digit of pi is 0
:lets run a sub-program
:1 (can be any non-zero value. This is useful if you want to send information via Ans to subprograms)
:Repeat Ans
:If Ans
:Goto 1
:1
:End
:back to main code
:example finished.
« Last Edit: April 23, 2009, 05:48:08 pm by nitacku »

Offline Halifax

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1334
  • Rating: +2/-1
    • View Profile
    • TI-Freakware
Re: scripts and code snippets
« Reply #19 on: May 26, 2009, 06:59:26 pm »
BASIC: Length of Integer
Code: [Select]
1+iPart(log(N
This will return the number of the digits that make up the number. Note the use of Integer, meaning that it will not work on floating-point values.
Ex: N = 1234, Output = 4

z80 ASM: Pow2 Multiplication Trick
Code: [Select]
;a = input
ld d,a
ld e,0 ; de = a * 256
srl d \ rr e ; de = a * 128
srl d \ rr e ; de = a * 64
etc.

z80 ASM: Raw Key Input Optimization
Code: [Select]
ld a,$FF
out (1),a
ld a,$FE
out (1),a
nop \ nop
in a,(1)
becomes
Code: [Select]
ld a,$FF
out (1),a
dec a
out (1),a
ld a,(de)
in a,(1)
As you can see, the use of the first optimization can only occur when analyzing the key group $FE. The second optimization takes into account the fact that a will be destroyed by the port read, but the instruction will still delay for the same amount of time that two nop's will.
There are 10 types of people in this world-- those that can read binary, and those that can't.

Offline skuller972

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 335
  • Rating: +11/-4
  • BEAST MODE
    • View Profile
Re: scripts and code snippets
« Reply #20 on: May 26, 2009, 07:04:54 pm »
that basic one is pretty good!
Then again, maybe not...
<a href="http://www.nerdtests.com/ft_nt2.php">
<img src="http://www.nerdtests.com/images/badge/nt2/bc7bf8e12353f09e.png" alt="NerdTests.com says I'm a Cool Non-Nerd.  Click here to take the Nerd Test, get nerdy images and jokes, and talk to others on the nerd forum!">
</a>