Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!swrinde!mips!pacbell.com!ucsd!ucrmath!feller!rhyde From: rhyde@feller.ucr.edu (randy hyde) Newsgroups: comp.sys.apple2 Subject: Re: ML subroutines (passing parameters in ML) Message-ID: <13975@ucrmath.ucr.edu> Date: 27 Apr 91 00:08:40 GMT References: <3397@kluge.fiu.edu> <13845@ucrmath.ucr.edu> <51983@apple.Apple.COM> Sender: news@ucrmath.ucr.edu Reply-To: rhyde@feller.ucr.edu (randy hyde) Lines: 63 Passing parameters on the (65816) stack is elegant, but it's still slow. Most routines only access the parameters a few times (do a dynamic analysis of your code sometime). Besides the set up involved (saving and loading DP), you also have to take into account pushing the parameters on the stack in the first place. Pushing parameters on the stack is great if 1) you're interfacing to code which requires this (e.g., toolbox or HLLs), 2) you need reentrancy, 3) you need simplicity and consistency. Simplicity? Isn't using the stack complicated? Maybe the first time around for beginners, but after you get used to it it becomes automatic (even without macros.) As I pointed out in a previous post, however, the cost is high. You have to give up DP (a great place to pass parms in global locations) and there is a lot of set up involved. Imagine someone writing a procedure called in 8-bit mode to convert a character to upper case (not that I would suggest this, in-line code would be more appropriate): Char passed in A: ToUpper cmp #'a' blt >0 cmp #'z'+1 bge >0 and #$5f ^0 ret Char passed on stack, value returned on stack, no bp setup: ToUpper pha lda 3,s cmp #'a' blt >0 cmp #'z'+1 bge >0 and #$5f sta 3,s ^0 pla ret Now what happens when you want to pass it on the stack? Even assuming the processor is in 16-bit mode (so you can easily transfer S->A->D there is a lot more work involved. Of course, if the routine is relatively complex and eats up a lot of time, who cares how long it takes to set up and access the parameters? But for short routines it can be a significant penalty. Indeed, one of the main reasons C compilers do so well on SparcStations and other RISC machines is because they pass their parameters in registers rather than on the stack (on the SPARC, the register back *is* a stack, local to the chip, but that's another story). I did not suggest that people *not* use the stack to pass parameters. There are a lot of advantages to it. However, it wouldn't be my first choice for time critical, short, routines. As long as HLLs pass their parameters in this fashion, I can always write code that will outperform said stuff, by a wide margin, in assembly.