Author Topic: How can i write inline assembler in c code  (Read 15004 times)

Offline miskin

  • Jr. Member
  • *
  • Posts: 10
  • Country: de
  • use Linux mint 15 in VM
How can i write inline assembler in c code
« on: May 15, 2015, 11:14:09 AM »
#include<stdio.h>

int main()
{
   int a=10,b=20,c;
   
   _asm_test:
      enter 0,0
      pusha
      mov eax,a
      mov ebx,b
      add eax,ebx
      mov c,eax
      popa
      leave
      ret   
   
   printf(" %d + %d = %d\n",a,b,c);
   return 0;
}

//this dont work .thank for helping me!

Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: How can i write inline assembler in c code
« Reply #1 on: May 15, 2015, 02:55:54 PM »
Hello, your question should be in other discussion.

Else what compiler you use for C code ?

In gcc, we write:
Code: [Select]
asm (".intel_syntax noprefix");
asm ("...");
asm (".att_syntax noprefix");

and compiler flag:
Code: [Select]
-masm=intel

In MS/intel compiler:
Code: [Select]
__asm
{
...
}

and no compiler flag needed.
« Last Edit: May 15, 2015, 04:41:49 PM by shaynox »

Offline miskin

  • Jr. Member
  • *
  • Posts: 10
  • Country: de
  • use Linux mint 15 in VM
Re: How can i write inline assembler in c code
« Reply #2 on: May 15, 2015, 04:37:08 PM »
i use gcc on linux
my editor is geany.
i would like to write the code for a subtraktion function with inline assambler but i dont know how to do this.

Offline miskin

  • Jr. Member
  • *
  • Posts: 10
  • Country: de
  • use Linux mint 15 in VM
Re: How can i write inline assembler in c code
« Reply #3 on: May 15, 2015, 04:48:45 PM »
#include<stdio.h>

int main()
{
   int a=10,b=20,c;
   
   asm ("
      enter 0,0
      pusha
      mov eax,a
      mov ebx,b
      add eax,ebx
      mov c,eax
      popa
      leave
      ret " 
   );
   printf(" %d + %d = %d\n",a,b,c);
   return 0;
}


this dont work why?Can sombody help me.

Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: How can i write inline assembler in c code
« Reply #4 on: May 15, 2015, 04:53:23 PM »
Take care about asm inline, you risk to modify some register that are already use before your asm.

You need to put quote for each instruction and add brackets around variables.

Here's the code for do subtraction function:
Code: [Select]
int     a = 2, b = 1, c;

int         main(void)
{
    int     tmp;

    asm (".intel_syntax noprefix");
        asm ("mov   eax, [a]");
        asm ("sub   eax, [b]");
        asm ("mov   [c], eax");
    asm (".att_syntax noprefix");

    printf("%d - %d = %d\n", a, b, c);

    return (0);
}

And like you see, you need global variable (global label) for use variable written in ascii (a, b, c), else you need to use stack param.

(off-topic)
Unlike Nasm who give the ability to use 3 way to create variable: global label + param stack + local label (like structure in C code), variables range is just the way how see/manage their addresses.

We can expand those way with give the possibility to manage data's address directly while writing code maybe.
« Last Edit: May 15, 2015, 05:24:02 PM by shaynox »

Offline miskin

  • Jr. Member
  • *
  • Posts: 10
  • Country: de
  • use Linux mint 15 in VM
Re: How can i write inline assembler in c code
« Reply #5 on: May 15, 2015, 05:18:30 PM »
Hi
i compiled it and it's ok but i dont get an exucutible.
Thanks

Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: How can i write inline assembler in c code
« Reply #6 on: May 15, 2015, 05:22:45 PM »
No problem

You don't arrived to get an executable file ?

Offline miskin

  • Jr. Member
  • *
  • Posts: 10
  • Country: de
  • use Linux mint 15 in VM
Re: How can i write inline assembler in c code
« Reply #7 on: May 15, 2015, 05:40:06 PM »
the terminal output

./geany_run_script.sh: 5: ./geany_run_script.sh: ./testnasm: not found


------------------
(program exited with code: 127)
Press return to continue

Yes its running uuuuuuuuuuuuuuuuuuuuuuuhhhhuuuuuuuuuuuuuuuuuuu
1000 Thanks  to SHAYNOX
« Last Edit: May 15, 2015, 05:51:45 PM by miskin »

Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: How can i write inline assembler in c code
« Reply #8 on: May 15, 2015, 05:45:44 PM »
Congratulation ^^
« Last Edit: May 15, 2015, 06:09:32 PM by shaynox »