Author Topic: Shifting program locations  (Read 5226 times)

0 Members and 1 Guest are viewing this topic.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Shifting program locations
« on: April 17, 2011, 12:38:53 pm »
Is there a B_CALL routine or a function someone wrote to relocate a program in RAM?

For example, say I have these five variables in RAM:

A  (located at $A000)
C
[D]
RUNNER
DOORS

Is there a routine that exists to move pgrmRUNNER to $A000 while shifting the other variables further down into RAM?

RUNNER
A
C
[D]
DOORS

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Shifting program locations
« Reply #1 on: April 17, 2011, 02:33:04 pm »
I'll take that as a "no", but it won't be hard to make my own ;D

Offline FloppusMaximus

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 290
  • Rating: +57/-5
    • View Profile
Re: Shifting program locations
« Reply #2 on: April 20, 2011, 11:09:02 pm »
There isn't a system routine to do that.  There are many third-party routines to do so; that's exactly what shells do. :)

Probably the most popular approach (taken by Ion and many others since) is to copy a block of up to 768 bytes from the start of the program to a temporary buffer (such as saveSScreen), then DelMem those bytes, InsertMem at the desired location, and copy the data from the temporary buffer to the new location.  Then, if the program was larger than 768 bytes, repeat as needed.

Another technique is the one used by Venus (which I belive Kalimero invented); what he does is to repeatedly swap blocks of memory, each time smaller than the last.  It's a beautiful algorithm, and a marvelously simple bit of code, and it's also faster in most cases than the Ion algorithm.  It will leave you scratching your head for a while trying to understand why the code works. :)  And to use it, you also need to know about a couple of undocumented system routines (DelVar3D and DelVar3DC), which you can use to fix up the VAT after moving programs around.