2

I tried to convert C code that is written under Linux (fedora 9) to assembly x86 code, however, I have problem in a Math.h functions. The functions in this library such as ceil, floor, log, log10, pow are undefined in the assembly x86. Can you please help me to solve this problem?

Thanks.

hamb
  • 159
  • 1
  • 2
  • 11
  • I solved the problem by adding -lm to the gcc when I compile the code: gcc -lm -o executable_name file_name.s – hamb May 28 '12 at 14:47

2 Answers2

3

Most library functions won't be defined in assembly language, at least not in the sense of the addition operator directly mapping to the ADD instruction. If you want to re-write the library in assembly, you'll have to implement the function using whatever capabilities that your processor has available. Most library functions will require a separate assembly language subroutine, not just a single operation. The easiest way to approach this is to get the individual library subroutines working in isolation, then incorporate them into the larger program.

You can compile the C code and examine the disassembled output, but beware of compiler optimizations that can make the output hard for a human to follow.

May I ask what the purpose is behind this task? Since a compiler is essentially a C to assembly-language translator, there's rarely a need to do this by hand. Is this homework?

bta
  • 43,959
  • 6
  • 69
  • 99
  • Note that it might be problematic to examine the disassembled output of compiled code as the functionality is achieved through calling the particular functions within the library to do the task, and as such the won't be found inside the compiled binary itself but rather from the library which provides the functions. – zxcdw May 26 '12 at 00:21
  • I am working on a project that needs these functions in assembly (x86) language. – hamb May 26 '12 at 16:23
  • 1
    @hamb- The output of a compiler *is* assembly. If you don't want to write these functions in assembly from scratch, you can write them in C, compile them, and get the assembly code from the compiler output. The functions in math.h are rudimentary enough, though, that it might be easier to just write the assembly routines manually. – bta May 27 '12 at 00:54
2

The best way to find out what these functions do is to take a look at their implementation in glibc's source. It should give you clear enough insight. Another way would be to take a look at the disassembly of lm.so found in /usr/lib/.

zxcdw
  • 1,629
  • 1
  • 10
  • 18
  • I am not familiar with this issues. can you please explain your solution? – hamb May 26 '12 at 16:15
  • To find out what these functions from math.h do, you need to either look at the source code of the library(glibc) or then disassemble the library binary file(/usr/lib/libm.so) itself and then read the assembly to find out the exact behavior of the functions so you can see how they work. You can use objdump with `-d` flag to disassemble the file. – zxcdw May 26 '12 at 16:26