Author Topic: Calling function and using data while calling ASM from C  (Read 11356 times)

Offline mist

  • Jr. Member
  • *
  • Posts: 2
Calling function and using data while calling ASM from C
« on: July 04, 2018, 04:31:25 PM »
Hi,

I would like to call ASM (MASM) from C (GCC). Easy!
Now i would like my asm function to be able to use data and to call functions like printf().
I got two problems: the data section and the call to printf()
I have read examples on internet which are exactly like mine but don't seem to fail. Any help is welcomed.

This problem has been discussed on this forum anyway i can't find a way to compile my executable properly as GCC is also involved.
Thanks to fellow coders.

test.c:
Quote
#include <stdio.h>
int64_t asmfunc();
int main() {
asmfunc());
return 0;
}

test.asm:
Quote
global  asmfunc
section .data
msg: db "a very inspired message!",10,0
section .text
extern printf
asmfunc:
mov edi,msg
xor eax,eax
call printf
ret

compilation:
Quote
nasm -felf64 maxofthree.asm
gcc callmaxofthree.c maxofthree.o

result:
Quote
/usr/bin/ld: maxofthree.o: relocation R_X86_64_32 against `.data' can not 
be used when making a shared object; recompile with -fPIC
Quote

if i just leave the printf call, removing the .data section and the "mov edi,msg", i get
Quote
/usr/bin/ld: maxofthree.o: relocation R_X86_64_PC32 against symbol
`printf@@GLIBC_2.2.5' can not be used when making a shared object;
recompile with -fPIC

Hope my question will not be considered out of subject, my plan is to code mostly in asm with support of C, not the otherway ;)

By the way, if someone knows how to use global variable from the C source inside the assembly function, the answered is welcomed too. I'm quite a noob at C, sorry.

Dear

Offline mist

  • Jr. Member
  • *
  • Posts: 2
Re: Calling function and using data while calling ASM from C
« Reply #1 on: July 04, 2018, 05:33:11 PM »
solution has been found! (quite quickly i admit)(but with some external help)

Quote
       lea rdi, [rel msg]
          xor rax,rax
      call printf wrt ..plt

between you and me, i also missed a EDI/RDI ;)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Calling function and using data while calling ASM from C
« Reply #2 on: July 04, 2018, 06:46:28 PM »
Hi mist,
Welcome to the Forum.
Thanks for sharing the solution with us!
Before I saw that you'd posted the solution I beat on it a bit myself... or it beat on me...
First, typo in your c file: "asmfunc())" has an extra ")". Easily fixed.
Next, a puzzle. My gcc didn't like "int64_t". I think that should be fine. I changed it to just "int" to shut gcc up, but I think it should be fine. Dunno what my problem was there.
I spotted the "edi" instead of "rdi" but didn't change it.
Worked for me!

I was going to suggest "rel". You can put "default rel" in your file to change 'em all. I don't know why it worked for me without it. I don't know why your gcc apparently thought you were trying to make a shared object. I have a lot to learn about 64-bit code!

Anyway, glad you got it working!

Best,
Frank