Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack bounds checking on the Amiga 500

Tags:

amiga

I have a 68000 assembly language program running on my Commodore Amiga 500 that could potentially use a lot of stack space, so I want to do bounds checking.

If I call FindTask(NULL), and check tc_SPUpper and tc_SPLower, I get $c22c24 and $c21fa4, which is 3200 bytes of stack; however, the CLI has 8000 bytes of stack allocated, and the program starts with a stack pointer of $c29598—about 26K higher in memory than tc_SPUpper.

I read in the AmigaDOS Developer's Manual that, on start, 4(sp) contains the stack size. This value does contain 8000. ("Below this on the stack at 4(SP) is the size of the stack in bytes, which may be useful if you wish to perform stack checking.")

Can I safely take sp - 4(sp) as the lower limit of the stack? Do I need to allow for the stack size, the return address, and some other data that the CLI may have on the stack?

like image 383
tim.smith Avatar asked Oct 23 '25 15:10

tim.smith


1 Answers

After re-re-(…)-reading the manuals, I may have figured it out.

From Amiga ROM Kernel Reference Manual: Libraries & Devices, p.584:

The CLI does not create a new process for a program; it jumps to the program's code and the program shares the process with the CLI.

From this, I gather that the process returned by FindTask(NULL) is the CLI process, and tc_SPUpper and tc_SPLower refer to the stack for that process.

From AmigaDOS Developer's Manual, p. 160:

When the CLI starts up a program, it allocates a stack for that program. This stack is initially 4000 bytes, but you may change the stack size with the STACK command. AmigaDOS obtains this stack from the general free memory heap just before you run the program; it is not, however, the same as the stack that the CLI uses.

From this, I conclude that my program stack is separate from the stack in the task returned by FindTask(NULL).

Also from AmigaDOS Developer's Manual, p. 160:

AmigaDOS pushes a suitable return address onto the stack that tells the CLI to regain control and unload your program. Below this on the stack at 4(SP) is the size of the stack in bytes…

From this, I conclude that, for programs run from the CLI, the following code will give me the lowest address available on the stack.

        move.l  sp,d0               ; current stack pointer
        addq.l  #8,d0               ; return address and stack size
        sub.l   4(sp),d0            ; size of stack
        move.l  d0,stack_lowest     ; save for stack checking

For programs launched from Workbench, I think tc_SPUpper and tc_SPLower are the values that I want.

From Amiga ROM Kernel Reference Manual: Libraries & Devices, p.584:

When a user activates a tool or project, Workbench runs a program. This program is a separate process and runs asynchronously to Workbench.

I have confirmed that the difference between these two values is, indeed, the stack size specified in the .info file.

like image 101
tim.smith Avatar answered Oct 27 '25 03:10

tim.smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!