asm("movl $1,%eax\n\t"
"movl $0,%ebx\n\t"
"int $0x80"); //注意此处的;
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int result;
result = a * b;
asm("nop");
printf("result is %d\n",result);
return 0;
}
//user c global variable
#include <stdio.h>
int a = 10;
int b = 20;
int result;
int main(void)
{
asm("pusha\n\t"
"movl a,%eax\n\t"
"movl b,%ebx\n\t"
"imull %ebx,%eax\n\t"
"movl %eax,result\n\t"
"popa");
}
//extened ASM
asm("assembly code":output locations:input operands:changed register);
input output variable list:
"constraint"(variable)"
asm("assembly code":"=a"(result):"d"(data1),"c"(data2));
int main()
{
int data1 = 10;
int data2 = 20;
int result;
asm(
"imull %%edx,%%ecx\n\t"
"movl %%ecx,%%eax\n\t"
:"=a"(result)
:"d"(data1),"c"(data2)
);
printf("result %d\n",result);
return 0;
}
int main()
{
char input[30] = {"this is a meaasege.\n"};
//char input[30] = "this is a meaasege.\n";
char output[30];
int length = 25;
asm volatile(
"rep movsb"
:
:"S"(input),"D"(output),"c"(length)
);
printf("%s",output);
return 0;
}
asm ("assembly code"
: "=r"(result)
: "r"(data1),"r"(data2)
);
#include <stdio.h>
int main()
{
int data1 = 10;
int data2 = 20;
int result;
asm("imull %1,%2\n\t"
"movl %2,%0"
: "=r"(result);
: "r"(data1),"r"(data2)
);
printf("the result is %d\n",result);
return 0;
)
}
asm("imull %1,%0"
: "=r"(data2)
: "r"(data1),"0"(data2)
);
#include <stdio.h>
int main()
{
int data1 = 10;
int data2 = 20;
asm("imull %1,%0"
: "=r"(data2)
: "r"(data1),"0"(data2)
);
printf(printf("the result is %d\n",data2);
return 0;
}
//alternative placeholder
asm("imull %[value1],%[value2]"
: [value2] "=r"(data2)
: [value1] "r"(data1),"0"(data2)
);
int main()
{
int data1 = 10;
int data2 = 20;
asm("imull %[value1],%[value2]"
: [value2]"=r"(data2)
: [value1] "r"(data1),"0"(data2)
printf(printf("the result is %d\n",data2);
return 0;
);
}
int main()
{
int dividend = 20;
int divisor = 5;
int result;
asm("divb %2\n\t"
"movl %%eax,%0"
: "=m"(result)
: "a"(dividend),"m"(divisor)
printf(printf("the result is %d\n",data2);
return 0;
)
}
asm("fsincos"
:"=t"(cosine),"=u"(sine)
:"0"(radian)
);
int main()
{
float angle = 90;
float radian,cosine,sine;
radian = angle/180*3.142592;
asm("fsincos"
:"=t"(cosine),"=u"(sine)
:"0"(radian)
);
printf("the cosine is %f,the sine is %f\n",cosine,sine);
return 0;
}
int main()
{
int a = 10;
int b =20;
int result;
asm("cmp %1,%2\n\t"
"jge greater\n\t"
"movl %1,%0\n\t"
"jmp end\n"
"greater"\n\t"
"movl %2,%0\n\t"
"end:"
: "=r"(result)
: "r"(a),"r"(b)
);
printf("the larger is %d\n",result);
return 0;
}
int main()
{
int a = 10;
int b =20;
int result;
asm("cmp %1,%2\n\t"
"jge 0f\n\t"
"movl %1,%0\n\t"
"jmp 1f\n"
"0:\n\t"
"movl %2,%0\n\t"
"1:"
:"=r"(result)
:"r"(a),"r"(b)
);
}