[Courses] One last diversion on binary, and then I'll shut up for a while.

jim jim at well.com
Wed Mar 7 22:36:39 UTC 2012


Thanks! 
    Looking at the code, it reads as  asm  is a function 
name rather than a keyword, yes? If so, what #include ; 
and it not, is it built-in to the Gnu C compiler? Is it 
spec'd? (I'll probably look these up, but seems helpful 
to raise the points to others.) 




On Wed, 2012-03-07 at 12:55 -0900, Christopher Howard wrote:
> On 03/07/2012 11:08 AM, jim wrote:
> > 
> > 
> >     The C keyword that may help is register; there are 
> > compiler command-line options that let one set some 
> > kinds of optimization such as fast or compact or .... 
> >     Can anyone provide helpful details? 
> > 
> > 
> > 
> 
> From what I understand, the register keyword only suggests to the
> compiler that a variable should be stored in a register, and prevents
> you from taking its memory address
> <http://tigcc.ticalc.org/doc/keywords.html#register>. But GCC extends
> this to allow you to assign a variable to a particular register
> <http://oreilly.com/linux/excerpts/9780596009588/gcc-extensions-to-the-c-language.html>.
> 
> The usual optimization flags are -O2, which optimizes for performance,
> and -Os, which optimizes for size.
> 
> You can also do inline assembly with the asm keyword. For example, a
> while ago I wrote a little demonstration code that checks the overflow
> after an integer add operation (i.e., uses amd64's built-in ability to
> check for integer overflow):
> 
> code:
> ----------
> #include <stdio.h>
> #include <limits.h>
> 
> int main() {
>   char carry;
>   unsigned long int val = ULONG_MAX - 10;
>   while(1) {
>     asm("movq %2, %%r9\n\t"
> 	"addq $1, %%r9\n\t"
> 	"setc %0\n\t"
> 	"movq %%r9, %1"
> 	:"=r"(carry), "=r"(val)
> 	:"r"(val)
> 	:"%r9"
> 	);
>     printf("%lu\n", val);
>     if(carry) {
>       printf("overflow!\n");
>       return 0;
>     }
>   }
> }
> ----------
> 
> However, the syntax in the example is specific to GCC. (And, of course,
> the amd64 architecture.)
> 
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses




More information about the Courses mailing list