LWN.net Logo

How Do I Make This Hard to Misuse?

How Do I Make This Hard to Misuse?

Posted Apr 2, 2008 4:27 UTC (Wed) by xbobx (subscriber, #51363)
In reply to: How Do I Make This Hard to Misuse? by jzbiciak
Parent article: How Do I Make This Hard to Misuse?

True, for that specific case it is better the other way around. But suppose you have a function such as:

void print_strings(FILE *stream, int num_strings, const char **list) {
    int i;
    for (i = 0; i < num_strings; i++) {
        fputs(list[i], stream);
    }
}
In this case, the assembly for this function will push stream once, then just push/pop n pointers to strings onto the stack and call fputs to print all of the strings. One could imagine that this would be useful when, say, implementing fprintf or other similar higher-level functions which all output to the same FILE *.


(Log in to post comments)

How Do I Make This Hard to Misuse?

Posted Apr 2, 2008 4:36 UTC (Wed) by jzbiciak (✭ supporter ✭, #5246) [Link]

See?  I knew I was missing something.  Thanks to you and felixfix both, since you are both
describing the same particular optimization.  Cute, in an ugly, quaint way.  :-D

I'm certain I'm guilty of far worse horrors in my 2 decades of assembly programming.  This
optimization never occurred to me since I've always managed to pass arguments in registers.

(An odd fluke of history, that.  I've written whole-program assembly where I control all the
conventions, C with inline-asm only (no function calls from asm) on machines with stack-based
calling conventions, and C callable asm on machines with register based calling conventions.)

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds