Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...