USR()

Post Reply
User avatar
mz-80a
Posts: 403
Joined: Thu Jan 25, 2018 10:46 am
Location: Devon, UK
Contact:

USR()

Post 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.....
MZ-80A Secrets
https://mz-80a.com/

Sharpworks (Sharp MZ homebrew)
https: //mz-sharpworks.co.uk/
User avatar
Pacman
Posts: 172
Joined: Mon Feb 05, 2018 2:59 pm

Re: USR()

Post 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
User avatar
mz-80a
Posts: 403
Joined: Thu Jan 25, 2018 10:46 am
Location: Devon, UK
Contact:

Re: USR()

Post 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.
MZ-80A Secrets
https://mz-80a.com/

Sharpworks (Sharp MZ homebrew)
https: //mz-sharpworks.co.uk/
User avatar
Pacman
Posts: 172
Joined: Mon Feb 05, 2018 2:59 pm

Re: USR()

Post 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.
hlide
Posts: 681
Joined: Thu Jan 25, 2018 9:31 pm

Re: USR()

Post 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.
User avatar
mz-80a
Posts: 403
Joined: Thu Jan 25, 2018 10:46 am
Location: Devon, UK
Contact:

Re: USR()

Post 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!
MZ-80A Secrets
https://mz-80a.com/

Sharpworks (Sharp MZ homebrew)
https: //mz-sharpworks.co.uk/
Post Reply