Return to BASIC

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
User avatar
Forthretro.new.fr
Posts: 32
Joined: Thu Nov 19, 2009 9:34 pm

Return to BASIC

Post by Forthretro.new.fr » Mon Feb 08, 2010 9:51 pm

Hello friends,

I am sure the answer to be somewhere, but let ask it again :
I have a program I call from the BASIC with the usual RAND USR XXXX

But in the program I will use all the REG and the Stack.
My question is : where is the information for to comeback to the BASIC ?
In the STACK ? In a REG ?
If in the stack I can do, at the start of the program something like
POP DE
LD (save_return),DE
....
Then
LD DE, (save_return)
PUSH DE
C9

But none of the solution seems to work

Merci

sirmorris
Posts: 2813
Joined: Thu May 08, 2008 5:45 pm

Re: Return to BASIC

Post by sirmorris » Mon Feb 08, 2010 11:01 pm

I seem to remember that BC has something to do with it :)

if the machine code is:
LD BC, returnvalue
RET

LET N = USR(..)

will put the return value in the variable N.

Don't use IX and IY, AF' registers in slow mode - you'll crash the computer. If you do use them in FAST, make sure that IY = $4000 on return.

Charlie

User avatar
siggi
Posts: 883
Joined: Thu May 08, 2008 9:30 am
Location: Dauernheim, Germany
Contact:

Re: Return to BASIC

Post by siggi » Mon Feb 08, 2010 11:06 pm

Forthretro.new.fr wrote:Hello friends,

I am sure the answer to be somewhere, but let ask it again :
I have a program I call from the BASIC with the usual RAND USR XXXX

But in the program I will use all the REG and the Stack.
If you run the program in SLOW mode, NEVER touch the IX, IY and AF' registers, because they are used by the display routine and /INT and /NMI. The only exception is, when you want to write your own display driver and you know, what you do ...
My question is : where is the information for to comeback to the BASIC ?
In the STACK ? In a REG ?
If in the stack I can do, at the start of the program something like
POP DE
LD (save_return),DE
....
Then
LD DE, (save_return)
PUSH DE
C9

But none of the solution seems to work

Merci
The solution is

RST 08H
DEFB 0xFF

That calls the Basic error routine with "error" 0, cleans up the stack and jumps back to BASIC.

HTH Siggi
My ZX81 web-server: online since 2007
http://zx81-siggi.endoftheinternet.org/index.html

User avatar
Forthretro.new.fr
Posts: 32
Joined: Thu Nov 19, 2009 9:34 pm

Re: Return to BASIC

Post by Forthretro.new.fr » Tue Feb 09, 2010 1:01 am

Many thanks for the answer. It helped me to understand the RAND USR !

With a debug I noted that the adress of the SP allway was 7FEA at the start.

While the SP was at 7FEA I could come back to the BASIC with C9.

However FORTH change the Stack adress later. It is why the computer crashed with any C9.

The solution was to put 7FEA in a 'return_adress' and for to exit, to use the instruction

ED 7B 'return_adress' ; LD SP, (return_adress) -> LD SP , 7FEA
C9


OK !!!

User avatar
XavSnap
Posts: 757
Joined: Sat May 10, 2008 4:23 pm

Re: Return to BASIC

Post by XavSnap » Fri Apr 09, 2010 5:23 pm

Hi,

There's a call adresse to ignore all asm/basic errors.
How you can use eratics basic codes like:

10 Print USR <your address>,"HELLO BOYS"
20 END

or
10 LET A$=STR$ USR <your address>+"HELLO BOYS"
20 END


a CALL xxxx will come back to the basic PC, but will ignore all the end of the basic line !
The "HELLO BOYS" will be ignored !
end the END wil be execute.
The famous "Call" is used in my "Something to do," codes !
upated!!!

ORG +16514

@DECODE=07BD
@DEST=+16402
@DFILE=+16396
@SEED=+16434
@FAST=2D6
@SLOW=2BF
@CLS=A2A
@KEYLOW=+16421
@KEYHI=+16422
@NXTL= 4029
@KEYB=02BB
@SCROLL=0C0E
@AKEY=+3893

@EXTERR=005B

@RETBAS
JP EXTERR
----------------------------
1 RET *****************
2 LET A$=STR$ USR 16514+"DATAS"
3 PRINT


In this exemple :
A$=A$ (Null string) after the line 2.
(Basic Line breaked after the RAND USR xxxx)

Before the "Call 5B" the next line is the line 2 address.
Datas offset = (NXTL)+4+6+5+5+2
="++++"(x4)+"LET A$=RAND USR"(x6)+"16514xxxxx"(x10)+"+~"(x2)

NextLIne (@NXTL= 4029) after the "Call 5B", is the line 3 address. (Address-3= the last quote)

Note: DEST = A$ Vars memory offset! (data =+3) in cas a string value.
Last edited by XavSnap on Sat Apr 10, 2010 8:47 am, edited 1 time in total.

User avatar
XavSnap
Posts: 757
Joined: Sat May 10, 2008 4:23 pm

Re: Return to BASIC

Post by XavSnap » Sat Apr 10, 2010 8:40 am

Hello guys,

I wrote a little exemple:
Xur file :

Code: Select all

    10  REM [HEX:\
2A,29,40,2B,2B,01,00,00,\
3E,0B,2B,03,BE,20,FB,79,\
3D,32,20,40,EB,13,D5,21,\
20,00,0B,ED,42,7D,1F,37,\
01,00,0A,4F,CD,D6,02,CD,\
F5,08,D1,3A,20,40,06,00,\
4F,CD,6B,0B,CD,0E,0C,CD,\
BF,02,C3,5B,00 ]

    20 PRINT USR 16514;"HELLO ..."
    30 PRINT USR 16514;"THIS IS A DEMO"
    40 PRINT USR 16514;"USING AN asm DATA TRANSFERT"
    50 PRINT USR 16514;"AND THE JP 005B FUNCTION"
    60 LET A$=STR$ USR 16514+"-.-"
    70 LET A$=STR$ USR 16514+"-1-"
    80 LET A$=STR$ USR 16514+"-2-"
    90 LET A$=STR$ USR 16514+"-3-"
   100 LET A$=STR$ USR 16514+"-4-"
   110 LET A$=STR$ USR 16514+"-5-"
   120 RUN SIN SIN SIN SIN SIN SIN PI
Xur ASM 2 codes:

Code: Select all

; WELCOME TO MY ARTIC
; ASSEMBLER 2 DEMO.
;------------------------


ORG +16514

;CONSTANTS
@DECODE=07BD
@DEST=+16402
@DFILE=+16396

@FRAMES=+16436
@SEED=+16434

@KEYLOW=+16421
@KEYHI=+16422
@NXTL= 4029
@EXTERR=005B
@TEMP8=4020

; PRINT AT FUNCTION
; SET CURSOR
; BC = X,Y VALUES
@CURSEUR=8F5
@CLEAR=A2A
@KBD=2BB

;PRINT A STRING
; DE= STRING OFFSET
; BC= STRING LEN
@CHAINE=B6B

@DECODE=7BD

;BASIC FUNCTIONS.
@SLOW=2BF
@FAST=2D6
@CLS=A2A
; SCROLL FUNCTION
@SCROLL=C0E

;
@START
; - GET DATAS
LD HL,(NXTL)
DEC HL
DEC HL
LD BC,0

;SEARCH THE FIRST
, -"- CHARACTER
LD A,0B
@RETCNT
DEC HL
INC BC
CP (HL)
JR NZ RETCNT
LD A,C
DEC A
LD (TEMP8),A
; A REGISTER IS STORED
; IN MEMORY
; A= STRING LEN

;SAVE DE (SWAP HL AND DE)
EX DE,HL
INC DE
PUSH DE
; COMPUTE THE Y COORD
; (32-STRING LENGHT)/2
LD HL,+32
DEC BC
SBC HL,BC
LD A,L
RRA ; A REGISTER/2
; RESET CARRY IF NOT
; A PAIR VALUE
SCF 
LD BC,0A00
LD C,A  

CALL FAST

CALL CURSEUR
POP DE
LD A,(TEMP8)
LD B,0
LD C,A  
CALL CHAINE
CALL SCROLL
CALL SLOW
; IGNORE THE END OFF
; BASIC LINE
JP EXTERR
Have fun...
Attachments
Demo.p
Display, Datas transfert and bios 5Bh Basic call back.
(1.26 KiB) Downloaded 125 times

User avatar
mrtinb
Posts: 1066
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Return to BASIC

Post by mrtinb » Wed Sep 14, 2016 4:14 pm

Both

Code: Select all

RST 08H
DEFB 0xFF
and

Code: Select all

JP 0x005B
clears the BASIC program i memory. Can I return a little nicer, even if the stack destoyed?
Martin
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube

sirmorris
Posts: 2813
Joined: Thu May 08, 2008 5:45 pm

Re: Return to BASIC

Post by sirmorris » Wed Sep 14, 2016 10:04 pm

LD HL,(ERR_SP)
LD SP,HL
RET

IIRC

User avatar
PokeMon
Posts: 2225
Joined: Sat Sep 17, 2011 6:48 pm

Re: Return to BASIC

Post by PokeMon » Thu Sep 15, 2016 3:49 pm

mrtinb wrote:Both

Code: Select all

RST 08H
DEFB 0xFF
and

Code: Select all

JP 0x005B
clears the BASIC program i memory. Can I return a little nicer, even if the stack destoyed?
There must be something wrong with your program.
RST 08H
DEFB 0xFF
does work very well even with an unclean stack.
This is the official method of any ROM routine returning an error (or no error with $ff).

Code: Select all

 10 LET V = 40000

 20 PRINT V

 30 REM _asm
    LD  HL,($401C)
    PUSH AF
    PUSH BC
    PUSH DE
    RST $08
    db  $ff
    RET
    END _asm

 40 RAND USR #30#
Did try this out and works 100% with no deleted program. 8-)

User avatar
mrtinb
Posts: 1066
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Return to BASIC

Post by mrtinb » Fri Sep 16, 2016 9:57 am

PokeMon wrote:There must be something wrong with your program.
RST 08H
DEFB 0xFF
does work very well even with an unclean stack.
Thanks for your reply. I'll test further with RST 08h.
Martin
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube

Post Reply