Jump to content
  • 0
hth313

How to exit from C

Question

I just got my first output from my (own) C compiler and it was also among the first times I ever played with the X16 emulator, so I want to share my (minor) success.

How should I exit from the program back to the system? Sorry if this is really a stupid question, but it was a long time ago since I programmed a Commodore 8-bit machine.271945703_ScreenShot2021-01-13at16_58_26.thumb.png.59b9792ac6650db6f708e12d4e95ef95.png

 

 

Share this post


Link to post
Share on other sites

8 answers to this question

Recommended Posts

  • 0

SYS calls a machine language subrotine. End your C with a return() or 6502 with a RTS. The BASIC will resume and execute the next statement after the SYS call. Assuming the state of the BASIC interpreter has been unmodified (ZP variables, etc) and the system stack is still intact.

Share this post


Link to post
Share on other sites
  • 0
41 minutes ago, hth313 said:

Sorry if this is really a stupid question, but ..

 

The only stupid question is one not asked. 🙂 Good luck with your project!

Share this post


Link to post
Share on other sites
  • 0
Just now, codewar65 said:

The only stupid question is one not asked. 🙂 Good luck with your project!

The problem is that I take over and set my own stack. I also put some stuff in zero page, but that should be in permitted and safe area (so far). What I am looking for is some soft-reset entry point back to BASIC that leaves the display and program. It should perhaps clear variables and sets up the stack again. Perhaps I should not touch the CPU stack at all?

Share this post


Link to post
Share on other sites
  • 0
2 minutes ago, hth313 said:

Perhaps I should not touch the CPU stack at all?

As long as pop everything you pushed onto the stack and there hasn't been a stack overflow, you should be fine. There should be a jmp vector for a soft reset on the CX16, but I don't have it at hand.

Share this post


Link to post
Share on other sites
  • 0

Would the simple answer not be: "just return from main()"  with a final RTS?

As long as you make sure the SP is valid before doing that final RTS, and you didn't overwrite the data that was already on the stack before your program starts, and the part of the zero page that basic and kernal use is not overwritten, and the screen and I/O configurations are reset to the defaults, you should return to the ready prompt cleanly 🙂

With TSX and TXS instructions you can even store and restore the SP from anywhere in your program. Prog8's exit() routine uses this.

Soft-resetting the system is also possible as @codewar65 mentioned, but this clears your program (easily recoverable via "OLD" though).  I am not entirely sure if the setting of $01 is correct though for the changes that are coming in the banking register location, but this is what I use currently for a soft-reset routine:

            sei
            lda  #14
            sta  $01        ; bank the kernal in (? new way?)
            stz  $9f60        ; bank the kernal in (old way)
            jmp  ($FFFC)            ; reset vector

Share this post


Link to post
Share on other sites
  • 0

I changed the C startup code to simply use the existing stack as-is. This solves the problem and I now get back to the blinking prompt when main() returns.

I also took the opportunity to optimize it to the metal, more or less. I still have a proper C startup for the X16, but I changed the C code to be more like how I would write a Hello World in assembly, using a loop to send it to a character out routine, no printf() or stream related functions dragged in from the C library, no 0 exit code return either. The result is a C program that when compiled and linked results in a .PRG file that is 88 bytes.

Share this post


Link to post
Share on other sites
  • 0

@desertfishThank you for the suggested way of going back to the Kernal. I have added that as the fallback way of doing explicit exit(), which is useful if the program has messed things up too much and want to exit back to a clean machine.

Share this post


Link to post
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.


×
×
  • Create New...

Important Information

Please review our Terms of Use