Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 335 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's the purpose of using assembly language inside a C program?

#1
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?
Reply

#2
There are a few, although not many, cases where hand-optimized assembly language can be made to run more efficiently than assembly language generated by C compilers from C source code. Also, for developers used to assembly language, some things can just seem easier to write in assembler.

For these cases, many C compilers allow inline assembly.

However, this is becoming increasingly rare as C compilers get better and better and producing efficient code, and most platforms put restrictions on some of the low-level type of software that is often the type of software that benefits most from being written in assembler.
Reply

#3
In general, it is performance but performance of a very specific kind. For example, the SIMD parallel instructions of a processor might not generated by the compiler. By utilizing processor specific data formats and then issuing processor specific parallel instructions (e.g. ARM NEON or Intel SSE), very fast performance on graphics or signal processing problems can occur. Even then, some compilers allow these to be expressed in C using intrinsic functions.

While it used to be common to use assembly language inserts to hand-optimize critical functions, those days are largely done. Modern compilers are very good and modern processors have very complicated timing requirements so hand optimized code is often less optimal than expected.
Reply

#4
I would rather think of that as a way to write a very specific code for a specific platform, optimization, though still common, is used less nowadays. Knowledge and usage of assembly in C is also practiced by all-color hats.
Reply

#5
Just as a curiosity, I'm adding here a concrete example of something not-so-low-level you can only do in assembly. I read this in an assembly book from my university time where it was used to show an inherent limitation of C/C++, and how to overcome it with assembly.

The problem is how do I _invoke_ a function when the exact number of parameters is only known at runtime? In fact, in C/C++ you can easily define a function that takes a variable number of arguments like `printf`. But when it comes to calling that function, the compiler wants to know exactly how many parameters must be passed. You may pass more paremters than required, that won't do any harm. But what if the number grows unexpectedly to 100 or 1000 parameters, and must be picked out of an array?
The solution of course is using assembly, where you can dynamically create a stack frame of the proper size, copy the parameters on the stack, invoke the function, and finally reset the stack.

In practice, this would hardly ever be a limitation (except if the library you're using is really really bad designed). People who use assembly in C have much better reasons to do so like others have pointed out in their answers. Still, I think may be an interesting fact to know.
Reply

#6
There were various reasons to write inline assemblies in C. We can simply categorize the reasons into **necessary** and **unnecessary**.

For the reasons of unnecessary, possibly be:

* platform compatibility
* performance concerning
* code optimization
* etc.

I consider above as unnecessary because sometime they can be discard or implemented through pure C. For example of platform compatibility, you can totally implement particular version for each platform, however, use inline assemblies might reduce the effort. Here we are not going to talk too much about the unnecessary reasons.

For necessary reasons, they possibly be:

* something with standard libraries was insufficient to do
* some instruction set was not supported by compilers
* object code generated incorrectly
* writing stack-sensitive code
* etc.

These reasons considered necessary, because of they are almost not possibly done with pure C language. For example, in old `DOS`es, software interrupt `INT21` was **not reentrantable**. If you want to write a **Virtual Dirve** fully use `INT21` supported by the compiler, it was impossible to do. In this situation, you would need to hook the original `INT21`, and make it reentrantable. However, the compiled code **wraps your every call with prolog/epilog**. Thus, you can never break something restricted, **or you just crashed the code**. You can try any of trick by using the pure language of C with libraries; but even you can successfully find a trick, that would mean **you found a particular order** that the compiler generates the machine code; this is implying: you tried to **let the compiler compiles your code to exactly machine code**. So, **why not** just write inline assemblies directly?

This example explained all above of necessary reasons except `instruction set not supported`, but I think that was easy to think about.
In fact, there're more reasons to write inline assemblies, but now you have some ideas of them, and so on.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through