Page 56 of 62
SBasic User's Manual SBasic Version 2.7 Page 56
Printed: December 5, 1999
You can also use ASMFUNC to create statements. Remember that an
SBasic statement doesn't return a value, but simply performs an
operation. For example:
asmfunc setstk ' define an asm entry point
main: ' enter here
setstk $0060 ' change hardware stack addr
do loop ' silly loop
asm ' switch to assembly language
setstk ' entry point to setstk
tsx ' get current stack addr
ldx 0,x ' get return addr for SB in x
jsr _pull ' get new stack addr in d
xgdx ' new stack in x
txs ' move new stack addr to s
xgdx ' put return addr back in x
jmp 0,x ' return through x
endasm ' back to SBasic
end
This example is more advanced and shows how to change the return stack
pointer from inside SBasic. The SB program uses the SETSTK statement
to set the return stack pointer to $60, essentially moving the
hardware stack. The tricky part here is that SB will execute this
statement via a JSR to SETSTK. If the assembly language code simply
moved the new stack pointer into the S-register and returned, the
program would crash since the return address would be undefined. The
code above changes the S-register, but saves the return address in the
X-register. It finally returns by jumping through the X-register.
Here you can see how SBasic processes the arguments to a statement.
The argument $0060 for the SETSTK statement is pushed onto SB's data
stack; it is NOT passed in the D-register. Thus, before the assembly
language code in the SETSTK routine can do anything with the argument,
it must first execute a JSR to the SB internal routine _PULL. _PULL
pulls the top element from SB's data stack and returns it in the D-
register. Refer to the GOSUB statement above for details on how
SBasic parses arguments to statements.
Note that you don't have to use a JSR to _PULL just to get an argument
into the D-register. Advanced programmers can directly access
arguments using offsets to the Y-register. Regardless of how you
access the arguments, however, your assembly language routine MUST
remove all arguments from the data stack before returning! If not,
repeated invocations of your routine will eventually crash the target
system.
This brings up another element of the ASMFUNC usage. SBasic does no
error checking to make sure your program uses an ASMFUNC label
consistently. Thus, you could use the same assembly language routine