[Coco] DEF USR and strings?

Allen Huffman alsplace at pobox.com
Wed Oct 29 21:04:33 EDT 2025


> On Oct 29, 2025, at 5:12 PM, William Astle via Coco <coco at maltedmedia.com> wrote:
> 
> On 2025-10-29 16:04, Allen Huffman via Coco wrote:
>> Cool. The Extended Color BASIC manual even showed how to return a string:
>> A$=(USR1(VARPTR(A$))
>> You can have your ML subroutine modify B$'s value and then end the routine with an RT$ instruction. This causes U$R to return with B$’s modified value.
>> Weird use of extra parens, though.
>> 		— A
> The extra parens are definitely not needed. And A$=(USR1(VARPTR(A$)) will absolutely not work for returning a string if it just falls off the end with RTS. That *will* give a TM error.
> 
> You need to know exactly how the Basic ROM handles strings to be able to do this correctly. That's a bit more complicated than it sounds. You have to properly handle your string return value by making sure it *doesn't* point to any other string data in string space and make sure you put it on the string stack appropriately. If you don't get that right, you can have things go excitingly wrong during garbage collection. You also should not modify strings in place *at all* because the string data might be pointing to the Basic program text.
> 
> That might actually be a good topic for exploration on your blog.

I’ve never thought of a reason why I’d want to return a string, but I suppose there could be reasons.

I did some deep dives into how string space works with the pointers and such, including a basic program that displays the string stack on half of the 32 screen and you can “see” it work as it ads strings, deletes them, etc. That was a really cool experiment.

So another problem in the BASIC manual ;-) Good thing I didn’t try to make it work back then. I’d have zero places to turn for help in those days.

Any tips on how to handle passing in a string, though? I searched the FB group and found no relevant references to DEF USR, USR, etc. so I do not know where (maybe another group) I saw it.

But that person had it working.

		— A



More information about the Coco mailing list