CPCWiki forum

General Category => Programming => Topic started by: ssg on 06:33, 06 January 21

Title: Is there a way to return to BASIC from a RUN'd binary?
Post by: ssg on 06:33, 06 January 21
If I run a binary using a command like RUN"test.bin", and the code RETs, the CPC resets.

Is there a way to return to BASIC's Ready prompt safely after a RUN, or is the behavior hardcoded into RUN's code?
Title: Re: Is there a way to return to BASIC from a RUN'd binary?
Post by: andycadley on 09:43, 06 January 21
Is there a reason for wanting to RUN the binary, as opposed to using a BASIC loader that loads and CALLs the relevant code?


It may be possible to construct the stack so that RET would take you back to a BASIC prompt, but it's not really designed for that.
Title: Re: Is there a way to return to BASIC from a RUN'd binary?
Post by: ssg on 02:10, 10 January 21
Quote from: andycadley on 09:43, 06 January 21
Is there a reason for wanting to RUN the binary, as opposed to using a BASIC loader that loads and CALLs the relevant code?

I want to have a single file instead of two .BAS and .BIN files. I can embed the binary code in BASIC file using some trickery but it looks harder currently.

Quote
It may be possible to construct the stack so that RET would take you back to a BASIC prompt, but it's not really designed for that.

I see. Let me find another way to do this, thanks :)
Title: Re: Is there a way to return to BASIC from a RUN'd binary?
Post by: AMSDOS on 03:35, 10 January 21
Quote from: ssg on 02:10, 10 January 21
I want to have a single file instead of two .BAS and .BIN files. I can embed the binary code in BASIC file using some trickery but it looks harder currently.


A BASIC Stub file is what I've seen it being referred to which is on Kev Thacker's Unofficial Amstrad WWW Resource, which is a file type BASIC, with it's Length Extended to include the Binary following it. You cannot simply create one using the BASIC SAVE command, it's either got to be a MC routine to save a file, or using an Utility such as Lara will let you save a file as Type BASIC as well as asking for the Start Address and Length. Normally if the Binary has to go somewhere else away from the BASIC, I would have a LDIR Routine to move the code to where it needs to go.


A word of Warning though, if the BASIC Code needs to dump stuff to Memory, lower HIMEM first, CALL the MC routine to move the Binary to the protected area is the safest approach.


Title: Re: Is there a way to return to BASIC from a RUN'd binary?
Post by: ssg on 04:52, 10 January 21
Quote from: AMSDOS on 03:35, 10 January 21A BASIC Stub file is what I've seen it being referred to which is on Kev Thacker's Unofficial Amstrad WWW Resource, which is a file type BASIC, with it's Length Extended to include the Binary following it

Thanks! :) I found it at http://cpctech.cpcwiki.de/docs/basstub.html (http://cpctech.cpcwiki.de/docs/basstub.html) but the article is also a stub itself. I've got the basic idea from your post though. That works for me. I thought it would be harder for some reason.
Title: Re: Is there a way to return to BASIC from a RUN'd binary?
Post by: ssg on 06:19, 10 January 21
Here is a stub I wrote for whoever wants to write a similar thing and doesn't want to do it from scratch :)


; assembly program with a BASIC stub
; amstrad cpc series - Locomotive BASIC
; ssg - 2021

org #170
write direct "stub.bas" ; change file format to BASIC with a disk editor
                        ; (02 -> 00 at offset 18 in AMSDOS header)

db #0A, #00, #2C, #02, #83, #20, #1C
dw start
db #00, #00, #00

start
; your code goes here

ret
Powered by SMFPacks Menu Editor Mod