[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 23:05:07 UTC 2012


On 03/07/2012 01:36 PM, jim wrote:
> 
> 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.) 
> 
> 

asm is a built-in extension in GCC:

http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html

However, asm itself is part of the C standard, though not with all of
the extended functionality GCC gives it. I have a copy of a C99 draft
draft which states:

quote:
----------
J.5.10 The asm keyword

The asm keyword may be used to insert assembly language directly into
the translator output (6.8). The most common implementation is via a
statement of the form:

  asm ( character-string-literal );

----------

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


-- 
frigidcode.com
indicium.us



More information about the Courses mailing list