Restarting new BASIC program

discussion of beeb/electron applications, languages, utils and educational s/w
Post Reply
User avatar
flynnjs
Posts: 811
Joined: Tue Jul 06, 2010 9:33 pm
Contact:

Restarting new BASIC program

Post by flynnjs » Fri Jun 02, 2017 9:51 am

If call out from a BASIC program to a bit of machine code which for example
replaced the original BASIC program with some new BASIC, is there any way of
throwing away the BASIC stack and starting again?

cmorley
Posts: 936
Joined: Sat Jul 30, 2016 7:11 pm
Location: Oxford
Contact:

Re: Restarting new BASIC program

Post by cmorley » Fri Jun 02, 2017 9:59 am

I think issuing an END resets everything. You can call it from your assembler code even if you wish (&8A50 BASIC I, &8AC8 BASIC II).

I'm sure a guru can say for sure.

User avatar
Rich Talbot-Watkins
Posts: 1505
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: Restarting new BASIC program

Post by Rich Talbot-Watkins » Fri Jun 02, 2017 1:42 pm

I think the best way to do this which doesn't require calling directly into BASIC internal routines is just to insert a line into the keyboard buffer to set PAGE and RUN, then exit to BASIC. Something like this:

Code: Select all

\ acknowledge ESCAPE and clear buffers
LDA #126:JSR &FFF4

\ insert a bunch of characters into the keyboard buffer
LDX #0
.loop
LDY command,X:BEQ done
TXA:PHA
LDA #138:LDX #0:JSR &FFF4
PLA:TAX:INX:BNE loop
.done

\ Turn off VDU output
LDA #21:JSR &FFEE

\ Exit to BASIC
LDX #basic AND 255:LDY #basic DIV 256:JMP &FFF7

.basic
EQUS "BASIC":EQUB 13

.command
EQUS "PA.=&1900":EQUB 13
EQUS "O.":EQUB 13
EQUS "V.6:RUN":EQUB 13
EQUB 0
I'm not sure how big the keyboard buffer is, hence the abbreviations!

User avatar
BigEd
Posts: 2563
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Restarting new BASIC program

Post by BigEd » Fri Jun 02, 2017 2:33 pm

Do you need an OLD?

User avatar
Rich Talbot-Watkins
Posts: 1505
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: Restarting new BASIC program

Post by Rich Talbot-Watkins » Fri Jun 02, 2017 2:37 pm

Yep, there's an "O." there :)

Although I think it'll even work without that!

Edit: Oh, that's what you meant. Yeah I don't think it's really necessary.

User avatar
BigEd
Posts: 2563
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Restarting new BASIC program

Post by BigEd » Fri Jun 02, 2017 2:49 pm

Ah, no, I didn't notice you already had one! I thought OLD is necessary to make Basic re-assess the content of memory.

User avatar
Rich Talbot-Watkins
Posts: 1505
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: Restarting new BASIC program

Post by Rich Talbot-Watkins » Fri Jun 02, 2017 3:08 pm

Yes, absolutely right, it is! (I always thought RUN also did an internal 'OLD' but clearly not)

Code: Select all

>PAGE=&1900
>NEW
>10P."HELLO"
>P.~TOP
      190E
>PAGE=&2900
>NEW
>10P."BLAH BLAH BLA"
>P.~TOP
      2916
>PAGE=&1900
>P.~TOP
      2916
>RUN
HELLO
>P.~TOP
      2916
>OLD
>P.~TOP
      190E
>

User avatar
flynnjs
Posts: 811
Joined: Tue Jul 06, 2010 9:33 pm
Contact:

Re: Restarting new BASIC program

Post by flynnjs » Fri Jun 02, 2017 3:14 pm

Rich Talbot-Watkins wrote:I think the best way to do this which doesn't require calling directly into BASIC internal routines is just to insert a line into the keyboard buffer to set PAGE and RUN, then exit to BASIC.
But if my machine code was originally called by BASIC CALL then won't there will still be a call on the stack
when OSCLI starts BASIC?

User avatar
Rich Talbot-Watkins
Posts: 1505
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: Restarting new BASIC program

Post by Rich Talbot-Watkins » Fri Jun 02, 2017 3:26 pm

The BASIC stack will be reinitialized, but the 6502 stack will indeed have an old return address on it which is unused. The AUG says that languages are supposed to reinitialize the stack pointer (P.325) but I can't see BASIC doing this anywhere in the disassembly on 8bs.com. Bad BASIC!

User avatar
flaxcottage
Posts: 3673
Joined: Thu Dec 13, 2012 8:46 pm
Location: Derbyshire
Contact:

Re: Restarting new BASIC program

Post by flaxcottage » Fri Jun 02, 2017 3:27 pm

What everyone has described so far is what Sherston did in their W1S file protection system. :)

Don't worry about waste return addresses on the stack. When BASIC starts anew it will ignore any stack contents. In theory, I suppose, there could be a problem if you nest a lot of loops or recursion.
- John

Image

User avatar
Rich Talbot-Watkins
Posts: 1505
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: Restarting new BASIC program

Post by Rich Talbot-Watkins » Fri Jun 02, 2017 3:34 pm

Haha, yeah, it was pretty common. Exile amongst doubtless many others did it too!

I've just seen that BASIC resets the 6502 SP all the time! e.g. when entering any command at the prompt. As someone said in some other thread somewhere [*], it also copies the 6502 stack frame to its own stack when calling a PROC, and then resets the 6502 stack.

[*] that's why I don't edit Wikipedia

User avatar
flynnjs
Posts: 811
Joined: Tue Jul 06, 2010 9:33 pm
Contact:

Re: Restarting new BASIC program

Post by flynnjs » Fri Jun 02, 2017 3:39 pm

A quick check, recursively starting BASIC doesn't appear to overflow the 6502 stack so
BASIC must be resetting it somewhere.

User avatar
BigEd
Posts: 2563
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Restarting new BASIC program

Post by BigEd » Fri Jun 02, 2017 3:50 pm

Rich Talbot-Watkins wrote:As someone said in some other thread somewhere [*], it also copies the 6502 stack frame to its own stack when calling a PROC, and then resets the 6502 stack.
Maybe here?

User avatar
Rich Talbot-Watkins
Posts: 1505
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: Restarting new BASIC program

Post by Rich Talbot-Watkins » Fri Jun 02, 2017 3:59 pm

Yep, that's it! =D>

Post Reply