Stack pointer on entry to m/c programs

Everything about programming, including VDP and Sound programming.
Post Reply
User avatar
Dave
Posts: 1280
Joined: 11 Aug 2012 18:16
Contact:

Stack pointer on entry to m/c programs

Post by Dave »

Originally asked by Dom via e-mail, reproduced here for archiving . . . .

"I've just noticed that the Memotech target for z88dk is a little wonky on exit - either printing a whole bunch of keywords or jumping into the assembler - see https://github.com/z88dk/z88dk/issues/706

Searching for information I found http://www.primrosebank.net/computers/m ... taster.pdf and a small snippet of assembler that indicated that the stack pointer should be loaded from $FA96 - doing this improves the exit stability and we end up with a Params prompt which I can live with.

My question is, whether this is the correct way of starting machine code programs? Additionally should any registers be preserved or set to known values on exit. At present we just ret, but should we be jumping to a known address?

Many thanks,

dom"
User avatar
Dave
Posts: 1280
Joined: 11 Aug 2012 18:16
Contact:

Re: Stack pointer on entry to m/c programs

Post by Dave »

(Answer by Martin via email)

Hi Guys

If you want to return to the basic and the "Ready" prompt, then JP 0250H is a good place to go, it's an entry point called BASIC2 in the rom source. Jumping there will set up paged rom 0 that basic expects to see then reset the stack and the clock/cursor flash interrupt, The print ready and await input.

Exiting to basic with an error and returning to basic is RST 28 followed by a byte 0 to 63 for the error number.

Call machine code from basic, the usual way of LET X=USR(start) is set up so that as long as the stack is balanced a simple RET will return to basic. The value in the BC register being assigned to X on the process, there's no register set-up required.

You can just have basic process a CODE statement, that also just needs RET to exit.

I find printing garbage on the screen on exit us usually a consequence of me messing up the basic system variables. Exiting to Panel is more typical of an unbalanced stack.

Martin
User avatar
Dave
Posts: 1280
Joined: 11 Aug 2012 18:16
Contact:

Re: Stack pointer on entry to m/c programs

Post by Dave »

(Answer from Paul via email)

Hi Dom,

When I looked at z88dk, the crt0.c was not very sympathetic to the MTX environment, e.g. setting stack to $0000 so that return addresses start being saved at $fffe and $ffff etc. However there is an interrupt table at $fff8 to $ffff (or $fff0 to $fff8 possibly) too so that needs turning off too probably. I can provide code if you need it to turn interrupts off completely.

You could always return with RST 0 and let the MTX reboot itself. :)

Good luck with z88dk.

I did have a look at SDCC a while back and got a crt0.c setup. My reason for doing it was to get UWOL compiled for the MTX based on the SG1000 port. I've not had time to go back and finish it :(

Kind regards
[Paul]
User avatar
Crazyboss
Site Admin
Posts: 274
Joined: 09 Aug 2012 21:45
Location: Sweden
Contact:

Re: Stack pointer on entry to m/c programs

Post by Crazyboss »

Not sure if I understand it right or not.

Read the stackpointer at entry and put it back on exit?


LD HL,#0000
ADD HL,SP
LD (SPACE),HL
.
.
.
CODE
CODE
CODE

EXIT:
LD HL,(SPACE)
LD SP,HL
RET

Do not overwrite SPACE and SPACE+1 :)
//CLAUS - Webmaster at www.mtxworld.dk
Martin A
Posts: 799
Joined: 09 Nov 2013 21:03

Re: Stack pointer on entry to m/c programs

Post by Martin A »

That looks like 8080 derived code.

The Z80 has both LD (nn),SP and LD SP,(nn) to save and load the stack pointer to memory without involving HL.
User avatar
Crazyboss
Site Admin
Posts: 274
Joined: 09 Aug 2012 21:45
Location: Sweden
Contact:

Re: Stack pointer on entry to m/c programs

Post by Crazyboss »

Martin A wrote:That looks like 8080 derived code.

The Z80 has both LD (nn),SP and LD SP,(nn) to save and load the stack pointer to memory without involving HL.
Yes Martin you are right :) The single opcode is better to use.

I used the trick to put the Stack pointer back to the Init point, in a Intellivision game i wrote :) but that is on a CP1610 CPU, where you R6 is SP and R7 is the PC, so you really only have 6 registers at that CPU all are 16bits.
//CLAUS - Webmaster at www.mtxworld.dk
Post Reply