Author Topic: Calling a BASIC subroutine from assembler?  (Read 1685 times)

0 Members and 1 Guest are viewing this topic.

Offline duncan_bayne

  • CPC464
  • **
  • Posts: 49
  • Country: au
    • Duncan Bayne
  • Liked: 27
  • Likes Given: 3
Calling a BASIC subroutine from assembler?
« on: 06:45, 31 January 13 »
Hi All,

I've a very naive question: from an assembly language program, is it possible to invoke the BASIC ROM in such a way as to call a subroutine (essentially a GOSUB)?

I'm working on a game idea that has a user-programmable element.  It'd be nice to allow the user to write a subroutine in BASIC, and then call it from within my game.

I guess I know that it must be possible - after all, somewhere in the BASIC ROM there will be code that does this and can be called externally.  I'm hoping someone else will have tried it before, or even have a pointer to the relevant documentation or source code?

Yours,
Duncan

Offline duncan_bayne

  • CPC464
  • **
  • Posts: 49
  • Country: au
    • Duncan Bayne
  • Liked: 27
  • Likes Given: 3
Re: Calling a BASIC subroutine from assembler?
« Reply #1 on: 06:56, 31 January 13 »
Having a look around http://cpcrulez.fr/firmware.txt it seems possible ... maybe with just a bit of hackery (fiddling with the BASIC stack etc.).  Fun project for the train ride home ...

Offline duncan_bayne

  • CPC464
  • **
  • Posts: 49
  • Country: au
    • Duncan Bayne
  • Liked: 27
  • Likes Given: 3
Re: Calling a BASIC subroutine from assembler?
« Reply #2 on: 08:24, 31 January 13 »
This looks like it might be helpful ...

http://cpctech.cpc-live.com/docs/basic.asm
« Last Edit: 09:53, 31 January 13 by duncan_bayne »

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.873
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1080
  • Likes Given: 1824
Re: Calling a BASIC subroutine from assembler?
« Reply #3 on: 10:46, 31 January 13 »
Unsure if that can be done. You can make it appear that the assembly has a connection with BASIC by setting up a variable in BASIC and assigning an Address to it and then you can just have you're assembly return values to that address, which can then be used in BASIC.


Space Storm 2 (or Meteor Storm II) is a nice example The bulk of the game is in Assembly, though that 10 Liner Shows that David Hall has setup a Variable 'd' and is assigning it to memory address '20000'. When the game finishes one way or another the relevant value is returned to that address and from there he does an SPEED INK switch depending on if you have died or completed the game along with a Message (from the text arrays) - if '1' is return the game is completed, otherwise '2' is returned if you have failed.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline arnoldemu

  • Supporter
  • 6128 Plus
  • *
  • Posts: 5.336
  • Country: gb
    • Unofficial Amstrad WWW Resource
  • Liked: 2271
  • Likes Given: 3478
Re: Calling a BASIC subroutine from assembler?
« Reply #4 on: 19:57, 31 January 13 »
Hi All,

I've a very naive question: from an assembly language program, is it possible to invoke the BASIC ROM in such a way as to call a subroutine (essentially a GOSUB)?

I'm working on a game idea that has a user-programmable element.  It'd be nice to allow the user to write a subroutine in BASIC, and then call it from within my game.

I guess I know that it must be possible - after all, somewhere in the BASIC ROM there will be code that does this and can be called externally.  I'm hoping someone else will have tried it before, or even have a pointer to the relevant documentation or source code?

Yours,
Duncan
it will be hard to do if you call into the rom.

first problem is that there are different revisions of basic, so the rom structure has changed.
second problem is that you'll need to hack some variables to make basic happy.
and third problem is calling direct into the basic rom to get what you want.

there are some alternatives.

basic calls your program at the beginning. when you want to jump to a subroutine, you can do something like this:
Func is a var that you setup before you "exit" back to basic with a simple "return".
you'll need to remember IX into your code so you can access FUNC and poke it back when you return.

Code: [Select]
10 FUNC = 0
20 call yourcode, @FUNC
30 gosub FUNC
40 goto 10

this should work with all basics, but you'll need to be nice to basic (don't overwrite it's areas, use HIMEM to avoid it breaking your code).

how it's normally done:

script code is compiled down to byte codes, the data is embedded within your program. it is executed by an interpreter which you implement. you tell the interpreter of some functions and variables you are happy it can mess with.

one example like this is lua.

it may be possible to compile java down to byte codes and execute that, providing some fixed native functions that your code supports.

if you do implement your own script, you'll need to implement a compiler and interpreter effectively.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Offline Bryce

  • The Hardware Guy.
  • Supporter
  • 6128 Plus
  • *
  • Posts: 11.654
  • Country: wf
  • It's not broken, it just hasn't been fixed yet.
    • index.php?action=treasury
  • Liked: 4180
  • Likes Given: 436
Re: Calling a BASIC subroutine from assembler?
« Reply #5 on: 21:57, 31 January 13 »
Why don't you make a defined set of AI reactions. Then the basic "SDK" could compile machine code depending on the choice of available AI reactions and this could be poked in wherever you need it.

Bryce.

Offline duncan_bayne

  • CPC464
  • **
  • Posts: 49
  • Country: au
    • Duncan Bayne
  • Liked: 27
  • Likes Given: 3
Re: Calling a BASIC subroutine from assembler?
« Reply #6 on: 23:29, 31 January 13 »

it will be hard to do if you call into the rom.

first problem is that there are different revisions of basic, so the rom structure has changed.
second problem is that you'll need to hack some variables to make basic happy.
and third problem is calling direct into the basic rom to get what you want.

Yeah, not easy ... but my idea (a game where people write AIs to compete against each other in an artificial ecosystem) absolutely requires providing users with a language in which to write their AIs.

I could probably squeeze a simple Forth implementation into 16K along with the game, but I'd much rather not ... I think I'll be lucky to get the game itself to fit.

It makes me appreciate modern development environments even more ... this'd be easy in Common Lisp, Scheme, Ruby, or Javascript.  But then that's part of the charm of retro computing, being creative in such a resource constrained environment! :)
« Last Edit: 23:40, 31 January 13 by duncan_bayne »

Offline cpcitor

  • The user previously known as FindYWay
  • CPC6128
  • ****
  • Posts: 289
  • Country: fr
  • My heart still runs on traditional CPC.
    • My code for the CPC.
  • Liked: 142
  • Likes Given: 351
Re: Calling a BASIC subroutine from assembler?
« Reply #7 on: 11:48, 01 February 13 »
Having a look around http://cpcrulez.fr/firmware.txt it seems possible ... maybe with just a bit of hackery (fiddling with the BASIC stack etc.).  Fun project for the train ride home ...

This looks like it might be helpful ...

http://cpctech.cpc-live.com/docs/basic.asm

Thanks for these references. I put links on the wiki :
Where to learn? - CPCWiki
Locomotive BASIC - CPCWiki
Had a CPC since 1985, currently software dev professional, including embedded systems.

I made the first CPC cross-dev environment that auto-installs C compiler and tools: cpc-dev-tool-chain: a portable toolchain for C/ASM development targetting CPC.