Page 1 of 1

USR()

Posted: Tue Nov 17, 2020 9:11 pm
by mz-80a
Hey all,

Does anyone know if the USR() command in BASIC actually preserves the Z80 registers and BASIC stack before jumping to a machine code routine?

Think it'd be nice to have a full disassembly of the BASICs really.....

Re: USR()

Posted: Thu Nov 19, 2020 7:51 am
by Pacman
On sbasic 1Z-013b, USR can be found in $3305:

USR:
CALL $57FD; Syntax check for open parenthesis
CALL $4D65; Evaluate the expression (HL) and provide a 2-byte value: Address
PUSH DE
INC HL
CP 29
JR Z, USR_1
DEC HL
CALL $3332; Comma syntax check
CALL $4DBC; Evaluate the string expression and provide a pointer to the string
LD C, B
CALL $5804; syntax check for closed parenthesis
USR_1:
EX (SP), HL
CALL USR_2
POP HL
RET

USR_2:
JP (HL)

Basically, we check the syntax, the address and then direct call to the desired machine code.

Page 110 of document https://original.sharpmz.org/mz-700/dow ... ing_ge.pdf

Re: USR()

Posted: Thu Nov 19, 2020 10:06 pm
by mz-80a
Interesting, thanks! I suppose that means that any machine code routine needs to begin with storing all the registers on the stack in case any which BASIC is using get overwritten. And then obviously POP them back off the stack again before returning to BASIC.

Re: USR()

Posted: Fri Nov 20, 2020 6:46 am
by Pacman
Possible! Not having analyzed the complete code of s-basic 1z-013b.
It seems to me that there is a whole working environment in the form of memory locations and that saving this environment is not necessarily essential ... to be checked.

Re: USR()

Posted: Fri Nov 20, 2020 12:02 pm
by hlide
HL is already saved in stack and that may be the only thing which needs to be preserved when calling USR - that is, no need to save/restore in the code called by USR.

Re: USR()

Posted: Fri Nov 20, 2020 6:48 pm
by mz-80a
hlide wrote: Fri Nov 20, 2020 12:02 pm HL is already saved in stack and that may be the only thing which needs to be preserved when calling USR - that is, no need to save/restore in the code called by USR.
That would be very cool if so! It's annoying having to begin every machine code routine with PUSH AF, PUSH BC, PUSH HL, PUSH DE, etc etc etc and then POPing it all off the stack again at the end. If BASIC protects its own registers then that'd be highly useful!