Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of using assembly language inside a C program?

What's the purpose of using assembly language inside a C program? Compilers are able to generate assembly language already. In what cases would it be better to write assembly than C? Is performance a consideration?

like image 318
san6086 Avatar asked Jan 03 '13 01:01

san6086


People also ask

For what purpose assembly language is used?

Assembly language is used to directly manipulate hardware, access specialized processor instructions, or evaluate critical performance issues. These languages are also used to leverage their speed advantage over high level languages for time-sensitive activities such as high frequency trading.

What is the use of assembler in C?

The Assembler is used to translate the program written in Assembly language into machine code. The source program is an input of an assembler that contains assembly language instructions. The output generated by the assembler is the object code or machine code understandable by the computer.

What is the importance of assembly code to programming?

Why is Assembly Language Useful? Assembly language helps programmers to write human-readable code that is almost similar to machine language. Machine language is difficult to understand and read as it is just a series of numbers. Assembly language helps in providing full control of what tasks a computer is performing.


1 Answers

In addition to what everyone said: not all CPU features are exposed to C. Sometimes, especially in driver and operating system programming, one needs to explicitly work with special registers and/or commands that are not otherwise available.

Also vector extensions.

That was especially true before the advent of compiler intrinsics. Those alleviate the need for inline assembly somewhat.


One more use case for inline assembly has to do with interfacing C with reflected languages. Specifically, assembly is all but necessary if you need to call a function when its prototype is not known at compile time. In other words, when the quantity and datatypes of that function's arguments are but runtime variables. C variadic functions and the stdarg machinery won't help you in this case - they would help you parse a stack frame, but not build one. In assembly, on the other hand, it's quite doable.

This is not an OS/driver scenario. There are at least two technologies out there - Java's JNI and COM Automation - where this is a must. In case of Automation, I'm talking about the way the COM runtime is marshaling dual interfaces using their type libraries.

I can think of a very crude C alternative to assembly for that, but it'd be ugly as sin.


Yet another use case: crash/run-time error reporting. For postmortem debugging, you'd want to capture as much of program state at the point of crash as possible (i. e. all the CPU registers), and assembly is a much better vehicle for that than C.


I've been only covering cases where assembly is necessary. Hand-optimizing for performance is covered in other answers.

like image 128
Seva Alekseyev Avatar answered Nov 03 '22 18:11

Seva Alekseyev