call sys in basic: sys address,a,b

All aspects of programming on the Commander X16.
Post Reply
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

call sys in basic: sys address,a,b

Post by funkheld »

Hi good afternoon.

how can you create plaese "sys adresse,a,b" in asm?

Thanks.
User avatar
ahenry3068
Posts: 1139
Joined: Tue Apr 04, 2023 9:57 pm

Re: call sys in basic: sys address,a,b

Post by ahenry3068 »

funkheld wrote: Wed Apr 24, 2024 3:17 pm Hi good afternoon.

how can you create plaese "sys adresse,a,b" in asm?

Thanks.
I do not understand what your asking exactly.

If you mean how would you do the same thing in Assembly then it's actually really easy. SYS is a wrapper to call machine code.

So in Basic if your doing.

POKE $30C, 65
SYS CHROUT


In assembly you just
LDA #65
JSR CHROUT.



The BASIC sys has to do some other things to like Move the register back into the "Cache" Address's $30C-$30F then return to the BASIC code. In assembly you just work with the registers directly.
TomXP411
Posts: 1783
Joined: Tue May 19, 2020 8:49 pm

Re: call sys in basic: sys address,a,b

Post by TomXP411 »

funkheld wrote: Wed Apr 24, 2024 3:17 pm Hi good afternoon.

how can you create plaese "sys adresse,a,b" in asm?

Thanks.
https://github.com/X16Community/x16-doc ... sor.md#jsr
mgkaiser
Posts: 60
Joined: Sat Dec 02, 2023 6:49 pm

Re: call sys in basic: sys address,a,b

Post by mgkaiser »

To further expand on this, "JSR" is the equivalent of "SYS". The first parameter of SYS is the address, so that is also the address with JSR. With SYS, any additional parameters that come after the address need to be pulled off using KERNAL and BASIC functions. I know how to do that on a C64/C128, but am not sure the BASIC functions are at the same addresses on the CX16. If you are calling an assembly language subroutine from another assembly language routine, you can either pass your parameters in registers, use the zero page pseudo registers, or come up with your own way of passing them like pushing them onto the hardware stack or creating a software stack. For simple subroutines I use registers or pseudo registers. If you need to call the subroutine recursively you need a stack and I generally use a software stack and leave the 6502 hardware stack alone to mostly store return addresses.
User avatar
ahenry3068
Posts: 1139
Joined: Tue Apr 04, 2023 9:57 pm

Re: call sys in basic: sys address,a,b

Post by ahenry3068 »

mgkaiser wrote: Wed Apr 24, 2024 4:42 pm To further expand on this, "JSR" is the equivalent of "SYS". The first parameter of SYS is the address, so that is also the address with JSR. With SYS, any additional parameters that come after the address need to be pulled off using KERNAL and BASIC functions. I know how to do that on a C64/C128, but am not sure the BASIC functions are at the same addresses on the CX16. If you are calling an assembly language subroutine from another assembly language routine, you can either pass your parameters in registers, use the zero page pseudo registers, or come up with your own way of passing them like pushing them onto the hardware stack or creating a software stack. For simple subroutines I use registers or pseudo registers. If you need to call the subroutine recursively you need a stack and I generally use a software stack and leave the 6502 hardware stack alone to mostly store return addresses.
Ditto on the Psuedo registers. There's really rarely much need for any other method.
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

Re: call sys in basic: sys address,a,b

Post by funkheld »

hello, thanks for help.

what kind of registers are these?
$30C-$30F

greeting.
User avatar
ahenry3068
Posts: 1139
Joined: Tue Apr 04, 2023 9:57 pm

Re: call sys in basic: sys address,a,b

Post by ahenry3068 »

funkheld wrote: Wed Apr 24, 2024 9:13 pm hello, thanks for help.

what kind of registers are these?
$30C-$30F

greeting.
Those are not true registers. They are 6502 Memory addresses that BASIC uses to store register Values. The following isn't exact code but BASIC does something like this when SYS is invoked.

LDA $30C LDX $30D LDY $30E LOADS THE PROCESSOR REGISTER FROM $30F ( I really don't know the instructions here) JSRS to the SYS target Does the reverse Storing the Registers to the CACHE addresss Returns to BASIC.
kelli217
Posts: 532
Joined: Sun Jul 05, 2020 11:27 pm

Re: call sys in basic: sys address,a,b

Post by kelli217 »

When you call a machine language routine in assembly, you simply load the necessary values in the registers and perform a JSR to the address of the routine.

Code: Select all

    LDA #$65        ;load accumulator with value $65
    JSR $FFD2       ;jump-subroutine to CHROUT ($FFD2)
When you do this from BASIC, you have no way to access the registers directly, because there are no commands to do this, and besides, BASIC is constantly using them for its own purposes. So the memory locations $30C through $30F are temporary storage locations, from which the binary code that is executed as part of the SYS command will load the registers before doing its own jump-subroutine to the address.

The location $30C is where the accumulator value will be loaded from. $30D is for the X index register. $30E is for the Y index register, and $30F is for the status register and is used for things like setting the carry bit. So what BASIC essentially does, when you issue a SYS command such as SYS $FFD2, is to run the following assembly code:

Code: Select all

	LDA $030F	;load the accumulator with the value stored in $030F
			;(to be placed in the status register)
	PHA		;push the value of the accumulator onto the stack
	LDA $030C	;load the accumulator with the value stored in $030C
	LDX $030D	;load the X register with the value stored in $030D
	LDY $030E	;load the Y register with the value stored in $030E
	PLP		;pull the value at the top of the stack into the status register
There is more detail involved than this, but this is the essential portion.
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

Re: call sys in basic: sys address,a,b

Post by funkheld »

Hello, thank you for the good explanation

greeting
Post Reply