Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does C code call assembly code (e.g. optimized strlen)?

Tags:

c

unix

assembly

I always read things about how certain functions within the C programming language are optimized by being written in assembly. Let me apologize if that sentence sounds a little misguided.

So, I'll put it clearly: How is it that when you call some functions like strlen on UNIX/C systems, the actual function you're calling is written in assembly? Can you write assembly right into C programs somehow or is it an external call situation? Is it part of the C standard to be able to do this, or is it an operating system specific thing?

like image 672
John Humphreys Avatar asked Sep 04 '11 15:09

John Humphreys


People also ask

How does strlen work in C?

C strlen() The strlen() function calculates the length of a given string. The strlen() function takes a string as an argument and returns its length. The returned value is of type size_t (an unsigned integer type). It is defined in the <string.

Can you run assembly code in C?

We can write assembly program code inside c language program. In such case, all the assembly code must be placed inside asm{} block.

What is assembly code in C?

An assembly language is a low-level programming language designed for a specific type of processor. It may be produced by compiling source code from a high-level programming language (such as C/C++) but can also be written from scratch. Assembly code can be converted to machine code using an assembler.

Does assembly code run faster than machine code?

Actually, the short answer is: Assembler is always faster or equal to the speed of C. The reason is that you can have assembly without C, but you can't have C without assembly (in the binary form, which we in the old days called "machine code").


2 Answers

The C standard dictates what each library function must do rather than how it is implemented.

Almost all known implementations of C are compiled into machine language. It is up to the implementers of the C compiler/library how they choose to implement functions like strlen. They could choose to implement it in C and compile it to an object, or they could choose to write it in assembly and assemble it to an object. Or they could implement it some other way. It doesn't matter so long as you get the right effect and result when you call strlen.

Now, as it happens, many C toolsets do allow you to write inline assembly, but that is absolutely not part of the standard. Any such facilties have to be included as extensions to the C standard.

like image 158
David Heffernan Avatar answered Sep 22 '22 09:09

David Heffernan


At the end of the road compiled programs and programs in assembly are all machine language, so they can call each other. The way this is done is by having the assembly code use the same calling conventions (way to prepare for a call, prepare parameters and such) as the program written in C. An overview of popular calling conventions for x86 processors can be found here.

like image 30
fvu Avatar answered Sep 18 '22 09:09

fvu