compiling asm codes in c++ compilers

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
User avatar
Ali Arman Tamal
Learning poster
Posts: 76
Joined: Sat Jan 15, 2005 5:04 pm
Location: Dhaka
Contact:

compiling asm codes in c++ compilers

Post by Ali Arman Tamal » Fri Nov 11, 2005 9:07 am

can anyone tell me how can i write the asm part of the following program
for online judge. i get cpmpile error and judge says asm is undefined keyword.

I coded & compiled it with Borland C++.

I will be very pleased if anyone tells me how to write that part in
visual studio.

thanks a lot ! :)

-- ali arman

CODE:
int checkParity(int n)
{
int i,count = 0;

for(i=0;i<s;i++)
{

asm {
shl n,1;
jc Label1;
}

val='0';
goto skip;

Label1:
val='1';
count++;

skip:

}

return count;
}

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Fri Nov 11, 2005 10:11 am

__asm__ keyword seems to always work.
But keep in mind, that the judge's compiler is gcc and it has a very specific asm syntax.

Here's a hello-world in asm, e.g.:

Code: Select all

int main()
{
        __asm__(
                "movl   $hello, %eax\n"
                "pushl  %eax\n"
                "call   puts\n"
                "popl   %eax\n"

                "movl   $0, %eax\n"
                "pushl  %eax\n"
                "call   exit\n"

                "hello:\n"
                ".ascii \"Hello, world!\\0\"\n"
        );
}
As for your function, I think, you can use asm in this way:

Code: Select all

extern int s, val[];

int checkParity(int n)
{
        int i, count = 0;

        for (i = 0; i < s; i++) {
                __asm__("shll   %0\n"
                        "jc     Label1\n"
                        "movl   $'0', %1\n"
                        "jmp    skip\n"
                        "Label1:\n"
                        "movl   $'1', %1\n"
                        "incl   %2\n"
                        "skip:\n"
                        :: "g"(n), "g"(val[i]), "g"(count)
                );
        }

        return count;
}

Post Reply

Return to “C++”