// asm.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
int _stdcall asmfun1(int x)//内嵌32位汇编程序,寄存器eax代表了函数返回值
{
int tmp;
_asm
{
mov eax,x
mov tmp,eax
}
return tmp;
}
double _stdcall asmfun2(double x)//使用协处理器进行浮点运算,返回值自动填入eax
{
int ans;
_asm
{
finit;协处理器初始化
fld x;将参数x压入协处理器的堆栈
fsqrt;协处理器将堆栈顶的数据开平方再存回去
fwait;主处理器等待,以便协处理器完成其工作。主要影响主处理器要读写的数据
mov ans,eax;
}
return ans;
}
//根据操作码op获取CPU的相关信息,分别保存在eax,ebx,ecx,edx中,然后分别存入id[0],id[1],id[2],id[3]中
void _stdcall asmcpuid(int op,int * id)
{
_asm
{
mov esi,id
mov eax,op
cpuid
mov [esi],eax
mov [esi+4],ebx
mov [esi+8],ecx
mov [esi+12],edx
}
}
void _stdcall asm_memcpy(void * to ,const void * from,unsigned int n)//linux系统中实现内存拷贝的代码
{
_asm
{
mov edx,n
mov ecx,edx
shr ecx,2
mov esi,from
mov edi,to
rep movsd
test edx,2h
je no_word_left
movsw
no_word_left:
test edx,1h
je no_bit_left
movsb
no_bit_left:
}
}
void _stdcall DexToBinaryStr(int x,char * string,int start,int sum)//将十进制整数x转化为二进制格式,然后将从右向左第start位开始的sum个bit存入字符串string中
{
_asm
{
mov edx,x
mov ecx,start
rep shr edx,1
mov ecx,sum
mov esi,string
translate:
test edx,1
jz loc1
mov [esi+ecx-1],49
jmp loc2
loc1:
mov [esi+ecx-1],48
loc2:
shr edx,1
loop translate
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char a[20];
char b[]="1234";
printf("the ans of asm is %d\n",asmfun1(3));
printf("sqrt of 2 is %f\n",asmfun2(2));
asm_memcpy(a,b,5);//虽然字符串b只有4个字符,但是在内存中,字符串还有一个结束符\0,因此字符串的内存长度为5
printf("%s\n",a);
char str[33];
char temp[33];
int CpuInfo[4];
asmcpuid(0,CpuInfo);
printf("The largest standard function number supported of current CPU is %d.\n",CpuInfo);
asm_memcpy(str,&CpuInfo[1],4);
asm_memcpy(str+8,&CpuInfo[2],4);
asm_memcpy(str+4,&CpuInfo[3],4);
str[12]='\0';
printf("The Vender ID of current CPU is %s.\n",str);
asmcpuid(1,CpuInfo);
DexToBinaryStr(CpuInfo[0],str,0,32);str[32]='\0';
printf("The Processor Signiture of current CPU is %d,the binary is %s.\n",CpuInfo[0],str);
asm_memcpy(temp,str+4,8);temp[8]='\0';
printf("The Extended Family is %s.\n",temp);
asm_memcpy(temp,str+12,4);temp[4]='\0';
printf("The Extended Model is %s.\n",temp);
asm_memcpy(temp,str+18,2);temp[2]='\0';
printf("The Type is %s.\n",temp);
asm_memcpy(temp,str+20,4);temp[4]='\0';
printf("The Family Code is %s.\n",temp);
asm_memcpy(temp,str+24,4);temp[4]='\0';
printf("The Model Number is %s.\n",temp);
asm_memcpy(temp,str+28,4);temp[4]='\0';
printf("The Stepping ID is %s.\n",temp);
DexToBinaryStr(CpuInfo[2],str,0,32);str[32]='\0';
printf("The ecx is %s.\n",str);
DexToBinaryStr(CpuInfo[3],str,0,32);str[32]='\0';
printf("The edx is %s.\n",str);
asmcpuid(0x80000002,CpuInfo);
asm_memcpy(str,&CpuInfo[0],4);
asm_memcpy(str+4,&CpuInfo[1],4);
asm_memcpy(str+8,&CpuInfo[2],4);
asm_memcpy(str+12,&CpuInfo[3],4);
asmcpuid(0x80000003,CpuInfo);
asm_memcpy(str+16,&CpuInfo[0],4);
asm_memcpy(str+20,&CpuInfo[1],4);
asm_memcpy(str+24,&CpuInfo[2],4);
asm_memcpy(str+28,&CpuInfo[3],4);
str[32]='\0';
printf(str);
asmcpuid(0x80000004,CpuInfo);
asm_memcpy(str,&CpuInfo[0],4);
asm_memcpy(str+4,&CpuInfo[1],4);
asm_memcpy(str+8,&CpuInfo[2],4);
asm_memcpy(str+12,&CpuInfo[3],4);
str[16]='\0';
printf(str);
getchar();
return 0;
}
评论0