c内嵌汇编
2021/8/19 17:06:18
本文主要是介绍c内嵌汇编,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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)
);
}
这篇关于c内嵌汇编的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)
- 2025-01-03停止思考数据管道,开始构建数据平台:介绍Analytics Engineering Framework
- 2025-01-03如果 Azure-Samples/aks-store-demo 使用了 Score 会怎样?
- 2025-01-03Apache Flink概述:实时数据处理的利器
- 2025-01-01使用 SVN合并操作时,怎么解决冲突的情况?-icode9专业技术文章分享
- 2025-01-01告别Anaconda?试试这些替代品吧