Jump to content
  • 0

Creating assembly function which can be called from Basic


Justin Baldock
 Share

Question

I have been experimenting with assembly for a little bit. I've created a simple function to replace some BASIC I have which works, but is slow (the BASIC is slow, not the assembly - whoosh its super fast!) . I'm using the cc65 toolchain. Its definitely a challenge I am enjoying. 

When I compile with .ORG $080D I can load the PRG and run it ok. If I change the memory address to .ORG $0400 that just breaks the program since the SYS2016 no longer works. If I adjust the PRG header so it is loaded to address $0400 instead of $0801 and then try to SYS to the actual first assembly instruction it is broken as well. If I adjust the .ORG $040C which is the first assembly instruction after the 3 BASIC NULLs, adjust the PRG header and call with SYS then my program works. 

My question is, how do I compile some assembly which I can call using the SYS command easily. eg Create some machine code that my BASIC program can just poke in to memory and then call with SYS. Or create a PRG which has the correct load address and no single line basic included. Or once compiled will I need to manually edit the PRG?  I'm sure I've missed some simple switch or I've not understand something. Any pointers would be great.

 

 

Edited by Justin Baldock
Clarify
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

I too like the cc65 toolchain for assembly programming. But it's a bit different compared to other assemblers.

You shouldn't use the .ORG directive (at all). It doesn't control the load address of code.

If you use the cl65 utility to assemble, specify the default config file for X16 assembly programming with the -C option, for instance:

cl65 -t cx16 -C cx16-asm.cfg -o test.prg test.asm

This will place the code at $0801, without a BASIC stub.

If you want the code to end up at address 8192, you may do this:

cl65 -t cx16 -C cx16-asm.cfg --start-addr 8192 -o test.prg test.asm

And if you want a BASIC stub to start your code from:

cl65 -t cx16 -u __EXEHDR__ -C cx16-asm.cfg -o test.prg test.asm

To get finer control over the assembly and link process, you may copy the default config file to your project and edit it to your needs. The cc65 manuals have very detailed information on the config file settings.

Edited by Stefan
Added -t cx16 option as suggested by Greg King
Link to comment
Share on other sites

  • 0
On 1/27/2022 at 6:19 PM, Justin Baldock said:

I have been experimenting with assembly for a little bit. I've created a simple function to replace some BASIC I have which works, but is slow (the BASIC is slow, not the assembly - whoosh its super fast!) . I'm using the cc65 toolchain. Its definitely a challenge I am enjoying. 

When I compile with .ORG $080D I can load the PRG and run it ok. If I change the memory address to .ORG $0400 that just breaks the program since the SYS2016 no longer works. If I adjust the PRG header so it is loaded to address $0400 instead of $0801 and then try to SYS to the actual first assembly instruction it is broken as well. If I adjust the .ORG $040C which is the first assembly instruction after the 3 BASIC NULLs, adjust the PRG header and call with SYS then my program works. 

My question is, how do I compile some assembly which I can call using the SYS command easily. eg Create some machine code that my BASIC program can just poke in to memory and then call with SYS. I'm sure I've missed some simple switch or I've not understand something. Any pointers would be great.

 

 

You have a short BASIC program that calls the assembly code with a SYS call, yes? Make sure your BASIC code starts at 0801, and change it from SYS2061 to whatever the address of the assembly code is. The BASIC has to start at 0801, though, and 0800 must contain 00.

Link to comment
Share on other sites

  • 0
  • Super Administrators
On 1/27/2022 at 5:19 PM, Justin Baldock said:

I have been experimenting with assembly for a little bit. I've created a simple function to replace some BASIC I have which works, but is slow (the BASIC is slow, not the assembly - whoosh its super fast!) . I'm using the cc65 toolchain. Its definitely a challenge I am enjoying. 

When I compile with .ORG $080D I can load the PRG and run it ok. If I change the memory address to .ORG $0400 that just breaks the program since the SYS2016 no longer works. If I adjust the PRG header so it is loaded to address $0400 instead of $0801 and then try to SYS to the actual first assembly instruction it is broken as well. If I adjust the .ORG $040C which is the first assembly instruction after the 3 BASIC NULLs, adjust the PRG header and call with SYS then my program works. 

My question is, how do I compile some assembly which I can call using the SYS command easily. eg Create some machine code that my BASIC program can just poke in to memory and then call with SYS. Or create a PRG which has the correct load address and no single line basic included. Or once compiled will I need to manually edit the PRG?  I'm sure I've missed some simple switch or I've not understand something. Any pointers would be great.

If your machine code is less than 1KB, you can assemble your code to $400. Do not include a BASIC header in your assembly program: The first byte of your program should be the first instruction. 

You can use DATA statements to inject your program, but it's faster to LOAD it into memory. Since LOAD restarts the program, you will need to use a flag to bypass the load when your program restarts. For example: 

10 IF LOADED=1 THEN 30
20 LOADED=1:LOAD "ROUTINES.PRG"
30 ...

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

Please review our Terms of Use