0

Assume I made a C++ program and compiled it. Now you have the assembly code of it. Then, if I use the assembly code in asm(), Will it have the exact same result? If it is, what could we use it for?

  • 1
    It will not be the same. Variables, procs and many other things will be replaced by offsets, the code might be optimized, etc. But you can use it to make changes in the flow or logic (like a virus does). – Jose Manuel Abarca Rodríguez Jun 23 '16 at 16:09
  • So, some does, some does not? –  Jun 23 '16 at 16:10
  • 1
    The problem is that the asm isn't the only thing needed to reproduce the program. You also need the same sections, code offsets, linkage... Which might not be included in the disassembly listing. – ElderBug Jun 23 '16 at 16:14
  • What is happening? –  Jun 23 '16 at 16:30
  • when the code is reloaded, it will be in a different environment, the actual location will be likely different. which means that the linking loader will be going thru the object code and placing final values for the assembly jumps. if you rebuild the code, then the jump targets (ie jumps to the end of a while statement) could change, If the code that you pulled out does not take this into account, and code execution may become misaligned resulting in undefined behavior. The assembler and compilers usually take care of these issues for us. – Gregg Jun 23 '16 at 17:13
  • Possible duplicate of http://stackoverflow.com/questions/4309771/disassembling-modifying-and-then-reassembling-a-linux-executable and http://stackoverflow.com/questions/8510129/trying-to-assemble-the-output-of-an-disassembler-such-as-objdump – Cody Gray - on strike Jun 24 '16 at 07:21

1 Answers1

0

Most compilers generally have an option that enables to produce the full assembly listing, however there is no use in just inserting the generated assembly code into the asm(" ").

The use of the assembly listing is generally in understanding how the compiler works, in order to write the code in C/C++ in such a way that the produced assembly code will be 1. efficient in time. 2. economical in memory.

In case of using high levels of compiler optimizations, in time or memory, the produced code is generally 3. slightly different in it's logic than the original C/C++ code 4. may contain bugs. Full listing helps to understand the connection between the original C/C++ code to the produced assembly code.

  1. In processors that the amount of memory of code and data is limited, It might be important to see in the produced assembly listing, the sizes of the memory code and data allocations.
  2. In case of implementing algorithm such as FFT that generally demands certain alignments on the addresses of the data, it might be important to see that these alignments are also fulfilled.

As a summary the assembly listing helps as to work in C/C++ and keep (as possible) efficiency in time and memory, sources that might be limited in embedded environment.

The assembly listing can be later assembled to produce an object code, as done generally directly by the compiler.

shaul boyer
  • 124
  • 3