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

Christopher Howard christopher.howard at frigidcode.com
Wed Mar 7 21:55:53 UTC 2012


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.)

-- 
frigidcode.com
indicium.us



More information about the Courses mailing list