5 Replies - 738 Views - Last Post: 14 November 2012 - 12:35 AM

#1 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

"Embed" assembly subroutines in C++ programs?

Posted 13 November 2012 - 09:05 PM

I have created an object class called BigInt that computes really large numbers.

I have also come up with an efficient algorithm in x86 that computes the factorial of 10000 in 1.6 seconds.

is there a way to "embed" this assembly code into my C++ program?

This post has been edited by carnivroar: 13 November 2012 - 09:05 PM

Is This A Good Question/Topic? 0
  • +

Replies To: "Embed" assembly subroutines in C++ programs?

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: "Embed" assembly subroutines in C++ programs?

Posted 13 November 2012 - 10:18 PM

You can if the compiler supports inline asm blocks.
Was This Post Helpful? 0
  • +
  • -

#3 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: "Embed" assembly subroutines in C++ programs?

Posted 13 November 2012 - 10:58 PM

Okay I did some reading...

I'm trying to write a simple factorial function but it's no working

If I get this working I think I'll be set.

long factorial(long f) {
    long fact;
    __asm__("push   %%rax" : "=a" (f)); rax = f ??
    __asm__("FACT:"
            "cmp    $1, (%esp)"
            "je     MULTIPLY"
            "mov    (%esp), %%rax"
            "dec    %%rax"
            "push   %%rax"
            "jmp    FACT"
            "MULTIPLY:"
            "add    $4, %esp" 
            "mov    (%esp), %%rax"
            "mul    (%esp), %%rax"
            "cmp    (%esp), %%rbx" : "=b" (f)); //error here.... rbx = f?
    __asm__("jle    MULTIPLY" 
            "mov    %%rax, %%rbx" : "=b" (fact)); rbx = fact?
            
    return fact;
}



error: invalid 'asm': operand number missing after %-letter

This post has been edited by carnivroar: 13 November 2012 - 11:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: "Embed" assembly subroutines in C++ programs?

Posted 13 November 2012 - 11:15 PM

I have absolutely no idea on how to embed assembly, but only that it's possible. However, I'm seeing many Google search results on that error, so you might want to look in that direction.

This post has been edited by blackcompe: 13 November 2012 - 11:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: "Embed" assembly subroutines in C++ programs?

Posted 13 November 2012 - 11:18 PM

View Postblackcompe, on 13 November 2012 - 11:15 PM, said:

I have absolutely no idea on how to embed assembly, but only that it's possible. However, I'm seeing many Google search results on that error, so you might want to look in that direction.

I'm doing that right now with no luck.
Was This Post Helpful? 0
  • +
  • -

#6 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: "Embed" assembly subroutines in C++ programs?

Posted 14 November 2012 - 12:35 AM

Yay I did it!!!

Now is there a way to store these code bits into external files? Because it can get huge and messy pretty quick.

long factorial(long f) {
    long fact;
    __asm__("mov    $1, %%rbx\n"
            "mov    %1, %%rdi\n"
            "FACT:\n"
            "cmp    $1, %%rdi\n"
            "je     DONE\n"
            "mov    %%rdi, %%rax\n"
            "mul    %%rbx\n"
            "mov    %%rax, %%rbx\n"
            "dec    %%rdi\n"
            "jmp    FACT\n"
            "DONE:\n"
            "mov    %%rbx, %0\n"
            : "=r" (fact)
            : "r" (f)
            : "%rbx"
           );
            
    return fact;
}



So for anyone that reads this:

My biggest confusion what this part

: "=r" (arg0), "=r" (arg1) ---> outputs
: "r" (arg2), "r" (arg3) -----> inputs
: "%rbx" ---------------------> tells compiler not to mess with this register

this means that (arg0) gets stored into the %0, (arg1) into %1, etc

This post has been edited by carnivroar: 14 November 2012 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1