Page 1 of 1

Stack pointer on entry to m/c programs

Posted: 24 Apr 2018 11:07
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"

Re: Stack pointer on entry to m/c programs

Posted: 24 Apr 2018 11:08
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

Re: Stack pointer on entry to m/c programs

Posted: 24 Apr 2018 11:08
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]

Re: Stack pointer on entry to m/c programs

Posted: 16 Oct 2018 13:45
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 :)

Re: Stack pointer on entry to m/c programs

Posted: 16 Oct 2018 23:42
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.

Re: Stack pointer on entry to m/c programs

Posted: 17 Oct 2018 09:40
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.