-1

I face a problem on my job to developing hadoop source code. I need to use function in an asm library. The API is given, for example: uint32_t func(uint32_t var1,const char var2,uint64_t var3). How can I use this function and get the return value in a C program? I always get undefined reference to "a function name(which I want to use asm api)". Are there some typical declarations I have to add in C or something else?

It seems easy for professional C programmer and no worthy a question. But I am a Java programmer, really unfamiliar to ASM and C. I hope to get an answer efficiently. Thank you.

AnyangWang
  • 33
  • 2
  • 5
  • 2
    The API is given how? Is there a header file with the function prototype in? Did you include it? Is the asm library added to the project so that it gets linked? – Lundin Apr 27 '17 at 11:25
  • 1
    "The API is given?" If you have a header file make sure you include it in you .c file and also make sure that your library is added to the project and is it linked to your project. Maybe this thread will help you: http://stackoverflow.com/questions/24991944/linking-c-with-nasm – 23ars Apr 27 '17 at 11:28
  • What operating system and architecture are you programming for? Can you show us the relevant code? – fuz Apr 27 '17 at 11:32
  • 2
    And what compiler do you use? And what assembler? The exact way you interface between C and assembler is dependent on such things... – skyking Apr 27 '17 at 11:39

2 Answers2

1

Lets take gnu for example, gcc and the language itself tells us that if we declare a function as static then it is not available to other .c files. In gnu assembler and assembly language is defined by the assembler not some universal language standard, a label is local unless declared global.

hello:
.globl there
there:

we would not be able to link to hello but we would be able to link to there.

Then you need to know the calling convention, which you can look up and read it and/or do experiments to figure it out. Even if you read up on it to make sure the compiler you are using conforms to the one you are reading you should still do experiments, the compiler you are using will conform to whatever standard it is using, not the one you wish it to use, so make your asm match...

unsigned int fun ( unsigned int a, unsigned int b )
{
    return((a<<2)+b);
}

gives

00000000 <fun>:
   0:   e0810100    add r0, r1, r0, lsl #2
   4:   e12fff1e    bx  lr

this is arm, and in this case the parameters are passed in r0,r1,r2,r3 if they fit, and then go to the stack if you have more. the return value is in r0 if it fits, there are lots of exceptions. you can destroy r0-r3 in the function but have to preserve r4 on (there may be one exception to that). so in this case r0 is shifted left 2 and added to r1 so r0 is a and r1 must then be b and the return value is in r0 per this experiment.

00000000 <fun>:
   0:   0f 5f           rla r15     
   2:   0f 5f           rla r15     
   4:   0f 5e           add r14,    r15 
   6:   30 41           ret 

using another instruction set r15 appears to be a and r14 b and the answer returned in r15

00000000 <_fun>:
   0:   1166            mov r5, -(sp)
   2:   1185            mov sp, r5
   4:   1d40 0004       mov 4(r5), r0
   8:   0cc0            asl r0
   a:   0cc0            asl r0
   c:   6d40 0006       add 6(r5), r0
  10:   1585            mov (sp)+, r5
  12:   0087            rts pc

and yet another instruction set uses the stack

I recommend you prototype your asm function that you want to call from C in C then compile it and disassemble and build your asm from there. It is far easier to actually assemble it rather than try to use inline assembly, that is an advanced topic and very dependent on the compiler, you have a better chance of it working if you dont use inline assembly.

so for example I would take the knowledge learned and create

.globl myfun
myfun:
   add r0,r1,r0, lsl #2
   bx lr

I could then assemble that and link it in with whomever wants to call myfun with two parameters. Or I could just use that knowledge to do other things with those two parameters to create the return value.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Wow!Really good advice to join asm and C together. I will think further about it. You said inline assembly is an advanced topic and compiler dependent. Do you mean prototyping asm in C can avoid the problem of compiler difference? – AnyangWang Apr 27 '17 at 15:24
  • in order for your inline assembly to not conflict with the compiler generated assembly specifically registers you want to use or worse if you want to connect variables in C with the assembly you have to learn the syntax for that compiler on how to do this, it is not just assembly it is other compiler stuff, which is not never used but not commonly used so the documentation and/or number of people out there doing that are fewer. If you want to learn asm, learn asm, THEN learn specific compiler stuff, same goes with C learn C then learn compiler specific stuff...or... – old_timer Apr 27 '17 at 15:58
0

If you need to call C or assembly from Java, use JNI or its alternatives. To call assembly, make a small C wrapper function to simulate what the assembly should do, then compile it and debug with your Java program (verify that you can pass parameters, get return values).

When this works, focus on calling the actual assembly from C. This really depends on that "asm library", it should have documentation how to call it from C.

Note that assembly libraries usually require certain CPU architecture. For example, if the library is for Intel 32-bit, it cannot be called from Java VM for 64-bit ARM (at least, that won't be easy).

ddbug
  • 1,392
  • 1
  • 11
  • 25
  • Yes, my target assembly library is a Intel one. I successfully built it and the function I need can be found in xxx.so file. Like you said, what I need is just a C wrapper function to connect java and assembly. The C code is ready. However, I down know exactly how to make reference between C and assembly. I feel like it just need some declaration in C code. Could you give me some clue? – AnyangWang Apr 27 '17 at 15:46
  • An Intel library should come with a decent documentation and some examples. Try to locate it. Typically they should have some C include file aka ".h file". – ddbug Apr 27 '17 at 16:09