Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!apple!stadler From: stadler@Apple.COM (Andy Stadler) Newsgroups: comp.sys.apple2 Subject: Re: ML subroutines (passing parameters in ML) Message-ID: <51983@apple.Apple.COM> Date: 25 Apr 91 00:38:39 GMT References: <3397@kluge.fiu.edu> <13845@ucrmath.ucr.edu> Organization: Apple Computer Inc., Cupertino, CA Lines: 55 In article <13845@ucrmath.ucr.edu> rhyde@musial.ucr.edu (randy hyde) writes: >4) Pass parameters on the stack. This is how most HLLs pass parameters. >It is slow, big, and accessing the parameters is inconvenient, >especially on processors like the 65816 which don't have a frame pointer >register. Nonetheless, there are some advantages to this technique: >It's easy to understand, easy to verify, and naturally supports >recursion and reentrancy. It supports any number of parameters (unlike >registers) and is easily expanded. Actually, this is a pretty GOOD way to pass parameters. Not only does it support recursion and reentrancy (as you mentioned) but it also has the _advantage_ of being the method used by HLL's. This is great for two reasons. First, it makes it much easier to freely mix asm and high-level, with routines calling each other and not caring what language is used; Second, since you have to use this method to call the toolbox, why not use it everywhere for consistency? I disagree with your assessment that it's big and slow. There are two tech- niques which make it quite efficient and useable. The first is to use the Pascal style of never passing any items greater than 4 bytes long. If a parameter is longer than 4 bytes, push a pointer to it. This is an area where Pascal has an edge over C because C is always pushing and pulling huge streams of bytes on and off the stack (especially when working with strings). Second, the 65816 -does- have a frame pointer register - the DP register. Since the stack and direct page can freely overlap, a procedure can set up a frame pointer with the following steps: 1. save old D (push it) 2. calc new D based on stack pointer 3. set D to new value Now the direct page provides quick access to the parameters pushed by the caller. In fact there are a couple of bonuses- first, by clever manipulation of the amount you subtract from D, you can also create local, private scratch storage. Additionally, because you've placed the parameters in direct page, any pointers which were passed can immediately be used for indirect accesses. This is a _distinct_ advantage over the register-based and globals-based methods you discussed. But wait, you say, this all sounds kinda complicated. You're right. But with a little work, one can create assembler macros which do all this and more, automatically. For example, the macros I used for assembly subroutines in HyperCard IIGS analyze the sizes of parameters, function results, and local variables, and pick the best optimized code for each combination; generate code to manipulate the SP and DP registers; support optional functions such as save/restore B register (globals data bank); and last but _definitely_ not least, create EQUATES for every single parameter and local variable. So how can you get great macros like these? Tell you what, how about if I post them! See next posting after this one.... Andy Stadler Apple Computer, Inc.