Avoiding stack overflow with Assembler routines

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Andrew_Waite
Posts: 34
Joined: Tue Aug 30, 2016 2:58 pm

Avoiding stack overflow with Assembler routines

Postby Andrew_Waite » Thu Jun 08, 2017 6:26 pm

The 6502 stack has 256 bytes, but presumably when CALLing a user assembly routine from BASIC many of these bytes are already being used by BASIC or the OS for use after the user routine ends with an RTS, or are used by the OS if the user routine is interrupted by an interrupt.

So when calling 6502 assembler routines from BASIC, what is the maximum number of bytes on the stack that my 6502 assembler routines can assume are available so as to avoid causing a stack overflow and crash the machine?

Thanks in advance for your help!

User avatar
tricky
Posts: 1823
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Avoiding stack overflow with Assembler routines

Postby tricky » Thu Jun 08, 2017 6:41 pm

I have no idea what the official number is, but several of the DFS type ROMs store the text of their error messages starting at &100.
When I was doing Carnival, experimenting, on the DFS on my beeb, at least the bottom &80 were free, but then after trying others, I left the bottom 30ish bytes alone and then used I think the next 50for the high-score table - when I am at my PC I could check.

User avatar
hoglet
Posts: 6390
Joined: Sat Oct 13, 2012 6:21 pm
Location: Bristol

Re: Avoiding stack overflow with Assembler routines

Postby hoglet » Thu Jun 08, 2017 6:42 pm

Surprisingly, pretty much all of the stack is available.

(The stack pointer on entry to an assembler routine from basic is &FD)

User avatar
MartinB
Posts: 4512
Joined: Mon Mar 31, 2008 9:04 pm
Location: Obscurity

Re: Avoiding stack overflow with Assembler routines

Postby MartinB » Thu Jun 08, 2017 7:17 pm

At any point in your program you can use the TSX instruction to find out where the stack-pointer is to give your code a steer or, during development, for you to get a feel for the dynamic capacity of the stack in your context.

SteveF
Posts: 429
Joined: Fri Aug 28, 2015 8:34 pm

Re: Avoiding stack overflow with Assembler routines

Postby SteveF » Thu Jun 08, 2017 10:19 pm

It's obviously not Acorn-specific advice, but Garth Wilson over at http://wilsonminesco.com/stacks/enoughstackspace.html suggests that in practice (and I'm paraphrasing here) there's usually way less use of the stack than you'd think.

It would perhaps be interesting - but surely someone's already done it? - to instrument a BBC emulator and run a few programs and see how low the stack pointer gets/where the lowest actual stack write is.

ETA: As a crude substitute, I just fired up BeebEm in Master 128 mode. I poked &FF into all of page 1 then prodded around a bit in View and ViewSheet, including saving and loading a few files and *SPOOLing at the same time. Apart from some "corruption" at &100-&102, the &FF bytes remained intact right up to and including &1C0, suggesting in the worst case there were 193 bytes of stack free.

ETA2: Forum regular BigEd gets a mention on that page and if I do the PRINT 1+((((((((((((1)))))))))))) example in BASIC, the stack gets used as low as &146. But unless you plan on calling your machine code via something like PRINT 1+((((((((((((USR(mycode))))))))))))) you probably don't need to get too worked up about that.

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Avoiding stack overflow with Assembler routines

Postby Elminster » Fri Jun 09, 2017 12:05 am

Weird concidence I was reading 'The BBC Micro an expert guide" (although I am no one) this evening and game aross a similar topic and program.

Okay went back to look and the section is about stacks and heaps but the program is the heap. I do wonder why Acorn didnt call it them stalagmites and stalactites, would have been much more easier to remember which one goes up and which one comes down.

User avatar
jgharston
Posts: 2661
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield

Re: Avoiding stack overflow with Assembler routines

Postby jgharston » Fri Jun 09, 2017 12:57 pm

StackSrc creates a utility that displays the current stack pointer as a graph down the screen. I've found it quite useful for discovering where highly nested stack use occurs. GoMMC stacks huuuge amounts.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_


Return to “programming”

Who is online

Users browsing this forum: No registered users and 3 guests