一、Turbo
C ,
也就是所说的TC。
1、使用预处理程序的伪指令#asm和#endasm,#asm用来开始一个汇编程序块,而#endasm指令用于该块的结束。
参考代码:
int mul(int a, int b)
{
/*汇编开始*/
#asm
mov ax,word ptr 8[bp]
imul ax word ptr 10[bp]
#endasm
/*汇编结束。*/
}
2、使用asm语句:
格式:asm<汇编语句>
参考代码:
int mul(int a, int b)
{
asm mov ax,word ptr 8[bp]
asm imul ax word ptr 10[bp]
/*
每个asm对应一句汇编
注意结尾不需要分号
*/
}
二、VC++/VS
格式:
__asm
汇编指令
[
]
__asm
{
汇编指令
}
[
]
asm前面是两条下划线,后面的方括号内容表示分号可有可无。
使用方法:
1、一条一条地用:
__asm mov al, 2
__asm mov dx, 0xD007
__asm out dx, al
每行一条汇编,
可以有分号,也可以没有。
2、组成一块地用:
__asm {
mov al, 2
mov dx, 0xD007
out dx, al
}
整体作为一个汇编代码块。
3、也可以将多条汇编写在一行:
__asm mov al, 2 __asm mov dx, 0xD007 __asm out dx, al
三、GNU
GCC
GCC对汇编的支持是最丰富的,简单介绍如下:
1、
用到的关键字:
“__asm__”
表示后面的代码为内嵌汇编,“asm”是“__asm__”的别名。
“__volatile__” 表示编译器不要优化代码,后面的指令保留原样,“volatile”是它的别名。
括号里面是汇编指令。
内嵌汇编语法如下:
__asm__(
汇编语句模板:
输出部分:
输入部分:
破坏描述部分)
一个简单的汇编模板:
int a=10,b
asm("movl %1, %%eax
movl %%eax, %0"
:"=r"(b) /*输出部*/
:"r"(a) /*输入部*/
:"%eax" /*毁坏部*/
)
表示C语言里的“b=a”。
里边r表示使用任意寄存器,%0、%1表示使用两个寄存器,一般只能%0~%9共十个操作数,按输入输出部变量出现顺序进行映射。
寄存器用两个百分号,是因为使用了%0%1这些数字使百分号有了特殊意义,所以在操作数出现的寄存器必须用双百分表示。
毁坏部里边的%eax表示eax寄存器在汇编代码块执行过程中会被改写,在执行前要保护好,这是提交给编译器决定的。
语言都是向下兼容的汇编是低级
C语言是高级
所以 不管谁嵌入谁
都要用高级的 也就是C编译器来编译
也就是说,其实 还是C语言嵌入汇编。
比较常见的做法是, 使用汇编编写.S文件,调用C函数
然后 单独写C文件,用C编译器编译
这个的经典应用, 在开源的uboot代码启动部分,有很好的样例。
今天有点时间,重新改下了下,为避免因编译器和平台实现而出现的问题,我写了三个版本,分别是windows下vc6.0,windows下mingw和cygwin和linux下的gcc/g++。vc6.0:
#include <stdio.h>
const char* input = "%d"
const char* output = "%d\n"
int n
int main()
{
__asm
{
lea eax, n
push eax
push input
loopx:
call scanf
cmp eax, 1
jne end
mov ecx, n
jecxz end
dec ecx
push ecx
push output
call printf
add esp, 8
jmp loopx
end:
add esp, 8
}
return 0
}
mingw/cygwin:
#include <stdio.h>
const char* input= "%d"
const char* output = "%d\n"
int n
int main()
{
__asm__
(
"loop: \n"
"pushl $_n \n"
"pushl _input \n"
"call _scanf \n"
"addl $8, %esp\n"
"cmpl $1, %eax\n"
"jneend \n"
"movl _n, %ecx\n"
"jecxz end \n"
"decl %ecx\n"
"pushl %ecx\n"
"pushl _output \n"
"call _printf \n"
"addl $8, %esp\n"
"jmploop\n"
"end:"
)
return 0
}
linux gcc/g++:
#include <stdio.h>
const char* input = "%d"
const char* output = "%d\n"
int n
int main()
{
__asm__
(
"pushl $n \n"
"pushl input \n"
"loop: \n"
"call scanf \n"
"cmp $1, %eax\n"
"jne end \n"
"movl n, %ecx \n"
"jecxz end \n"
"decl %ecx\n"
"pushl %ecx\n"
"pushl output \n"
"call printf \n"
"addl $8, %esp\n"
"jmp loop\n"
"end: \n"
"addl $8, %esp\n")
return 0
}