Best way to init SP (ROM)?

Page 1/3
| 2 | 3

By viejo_archivero

Paragon (1395)

viejo_archivero's picture

23-07-2020, 12:15

Hi there! I Recently came up with a discussion on which is the best way to initialize the stack pointer (ROM enviroment).

What I usually do is:
ld sp, (HIMEM)

What I am being told is better to do is:
ld sp, f380h

I always thought the first way was the standard and the safest way to do it instead of hardcoding the sp adress... but various MSX developers are telling me that if a DOS2 interface or a Flashjacks with Nextor is present, the ROM won't work, and that I should hardcode sp adress. Am I doing things wrong? Question

Login or register to post comments

By zeilemaker54

Champion (348)

zeilemaker54's picture

23-07-2020, 12:23

If it does not interact with basic, using himem should be ok

By viejo_archivero

Paragon (1395)

viejo_archivero's picture

23-07-2020, 12:24

zeilemaker54 wrote:

If it does not interact with basic, using himem should be ok

It does not Wink

By theNestruo

Champion (423)

theNestruo's picture

23-07-2020, 12:48

I do it the HIMEM way... and also preserve the existing hook.

I prefer this "respectful" style for no technical reason; I just find it more "respectful" and feels "right"... But I agree that depending on the firmware/other cartridge, it can lead to some problems if the previous software moves the stack down a lot (may interfere with your variables) or if the previous hook tooks really long (may degrade the game performance).

As a game ROM is intented to be used stand-alone, I guess it is not really *that* important: you won't be returning to previous software, so whatever it had in stack / hook can be safely drop out.

By Pencioner

Scribe (1565)

Pencioner's picture

23-07-2020, 14:36

Is there any problem of using the stack pointer 'AS IS' after ROM starts? What could be wrong with that?

By Grauw

Ascended (10772)

Grauw's picture

23-07-2020, 15:57

I reckon the context is this recommendation from the MSX2 technical handbook. It seems mostly relevant when you want to use all of the upper memory up to F380H. So in that sense I reckon there is maybe not much point to setting the stack pointer to HIMEM, it will only save a few bytes.

I myself so far always used the stack pointer as-is. But I do usually have a check if there’s sufficient stack space between the end of my variable area and the stack. Ignoring HIMEM and manually setting the stack pointer to F380H seems a bit dirty. What if a hook has been set up that point to that area (e.g. interrupt handler?). But I guess it’s nice that the option is there if you really need a lot of RAM.

By viejo_archivero

Paragon (1395)

viejo_archivero's picture

23-07-2020, 19:19

Thnx Grauw for the detailed info. So... it is not a good practice to initialize stack at the beginning of the program with ld sp,(HIMEM)? It would be better to just let the sp be wherever it is and just check it to see if it's located somewhere safe for your RAM usage? Just trying to learn here Smile

By theNestruo

Champion (423)

theNestruo's picture

23-07-2020, 20:06

You can always take a look at the disassembly of the first insturctions of some classics and modern games... but I'm afraid that will simply add more confusion oO

  • Alcazar: di / ld sp,$F032
  • Ali Baba: im 1 / ld sp,$F300
  • Butamaru Pants: nothing (no di, no im, no ld sp)
  • Crazy Train: (...) / ld sp,$E800
  • Eggerland Mystery: (...) / di / ld sp,$DE3F
  • Gommy Medieval Defender: di / im 1 / ld sp,(HIMEM)
  • Metal Gear: di / ld sp,Stack (Stack is a 1.130 bytes long space reserved in RAM)
  • Pyramid Warp: nothing (no di, no im, no ld sp)
  • Relevo's Snowboarding: di /im 1 / ld sp,(HIMEM) :-P
  • Stevedore: di / im 1 / ld sp,(HIMEM)
  • X-Spelunker: ld sp,$F380
  • Yazzie: ld sp,$F380

By viejo_archivero

Paragon (1395)

viejo_archivero's picture

23-07-2020, 20:27

heh, I did a similar research when they told me on ld sp,(HIMEM) not being the correct way Wink, that's mostly the reason I came here looking for answers on the best practice for stack pointer initialization.

By SjaaQ

Champion (375)

SjaaQ's picture

24-07-2020, 11:50

In DotAttack, I use IM2 and do a LD SP,#FFFE. No byte to waste!

By gdx

Enlighted (6221)

gdx's picture

24-07-2020, 12:17

MSXs use IM1 only and LD SP,#FFFE is not advisable because in this way you overwrite system variables MSX2/2+.

Page 1/3
| 2 | 3