Path: utzoo!attcan!uunet!mcrware!kim From: kim@mcrware.UUCP (Kim Kempf) Newsgroups: comp.os.os9 Subject: Re: Passing parameters by registers is bad use?!?!? Keywords: C compiler, parameter passing Message-ID: <3117@mcrware.UUCP> Date: 28 Sep 90 18:47:22 GMT References: <3537@rwthinf.UUCP> <2820@cernvax.UUCP> Reply-To: kim@mcrware.UUCP (Kim Kempf) Organization: Microware Systems Corp., Des Moines, Iowa Lines: 129 In article <2820@cernvax.UUCP> rbt@cernvax.UUCP (roberto divia) writes: >Sorry, I tend to disagree. It is true that allocating two registers for >parameter passing means some overload to the executable code but on the >other hand it is *MUCH* faster to do things this way. I did some tests >with a MC68030 and the same routine called with parameters in registers >was 10-15% faster then the equivalent called with parameters on the >stack. This comes for two main reasons: > >1) the stack push/pop/stackPointerRearrangement takes time; >2) bigger is the data area used by the code is and easier is to have a > cache miss (this in case of processors with data caches). > Couldn't have stated it better myself, Roberto! The following is an example, albeit trival, of the savings that adds up quickly on non-trivial programs: The example C program: main() { register int i,j; joe(1,2); for (i=1; i < 10; i++) { joe(i,5); } } The OS-9 C code in assembler. Notice the 2-byte argument loads and the absence of the stack cleanup after the function call. 00001 psect 00002 ttl main 00003 0000 4e55 main: link a5,#0 0000 00004 0004 48e7 movem.l #_1!1,-(sp) cc00 00005 0008 203c move.l #_3,d0 :6 ffffffbc 00006 000e=6100 bsr _stkcheck 0000 00007 0012 7202 moveq.l #2,d1 :2 00008 0014 7001 moveq.l #1,d0 :2 00009 0016=6100 bsr joe 0000 00010 001a 7801 moveq.l #1,d4 :2 00011 001c 6000 bra _7 000c 00012 _5 00013 0020 7205 moveq.l #5,d1 :2 00014 0022 2004 move.l d4,d0 :2 00015 0024=6100 bsr joe 0000 00016 _8 00017 0028 5284 addq.l #1,d4 :2 00018 _7 00019 002a 700a moveq.l #10,d0 :2 00020 002c b084 cmp.l d4,d0 :2 00021 002e 6e00 bgt _5 fff0 00022 _6 00023 _4 00024 0032 4ced movem.l -12(a5),#_1 0032fff4 00025 0038 4e5d unlk a5 00026 003a 4e75 rts :2 00027 ffffffbc _3 equ 0xffffffbc :0 00028 00000032 _1 equ 0x00000032 :0 00029 00000018 _2 equ 0x00000018 :0 00030 0000003c ends Now the code from the SunOS 4.0 UNIX compiler. Notice that the same instructions take *6-bytes* and the extra 2-bytes for the addq after the function call. 00001 psect 00002 _main: 00003 0000 4e56 link a6,#0 0000 00004 0004 dffc add.l #-4,sp fffffffc 00005 000a 48d7 movem.l #0x0,(sp) 0000 00006 000e 4879 pea 0x2 00000002 00007 0014 4879 pea 0x1 00000001 00008 001a=6100 bsr _joe 0000 00009 001e 504f addq.w #0x8,sp 00010 0020 7e01 moveq #0x1,d7 00011 L17: 00012 0022 0c87 cmpi.l #0xa,d7 0000000a 00013 0028 6c14 bge.s L16 00014 002a 4879 pea 0x5 00000005 00015 00016 0030 2f07 move.l d7,-(sp) 00017 0032=6100 bsr _joe 0000 00018 0036 508f addq.l #0x8,sp 00019 L15: 00020 0038 5287 addq.l #0x1,d7 00021 003a 6000 bra L17 ffe6 00022 L16: 00023 003e 7000 moveq.l #0,d0 00024 LE12: 00025 0040 4cee movem.l 4(a6),#0x80 00800004 00026 0046 4e5e unlk a6 00027 0048 4e75 rts Which is smaller and faster! ---------------- Kim Kempf, Microware Systems Corporation {sun,uunet}!mcrware!kim "Opinions! We doan need no stinkink opinions!" -- ---------------- Kim Kempf, Microware Systems Corporation {sun,uunet}!mcrware!kim "Opinions! We doan need no stinkink opinions!"