Академический Документы
Профессиональный Документы
Культура Документы
��j���, __asm
http://www.yesky.com/20021014/1634708.shtml
����һ����j������ȱ��
����1.ʹ�û������д����
����2.����ٶҪ��dz�� Ĵ��룻
����3.�豸�������ֱ�ӷ���Ӳ����
����//(."naked"��������� �����Dz�֪����ô����^_^����ž��Dz���Ҫc/c++�ı�����(�������)��ɵĺ��
��ʼ������β���룬��ο�msdn��"naked functions"��˵��)
������j�����벻������ֲ�������ij�������
��ʱ�������ʹ��masm����Ϊmasm֧�ָ��ĵĺ�ָ������ָʾ��
����������j��� ���
����1.��__asm��
__asm
{
mov al, 2
mov dx, 0xd007
out al, dx
}
����2.��ÿ����ָ��֮ǰ��__asm� ���
����__asm mov al, 2 __asm mov dx, 0xd007 __asm out al, dx
������ ����һ�ַ�����C/c++�ķ���һ�£������к������
ܶ ŵ ����Ƽ�ʹ�ı�һ�ַ�����
����������c/c++�е�"{}"��__asm���"{}"����Ӱ��c/c++�� �����
� ÷Χ�� ͬʱ��__asm�����Ƕ��ףǶ��Ҳ�
���Ӱ��� �����÷�Χ��
��������__asm����ʹ�û������
����1.��j���ָ�
��(_emitαָ��˵�������)��
����2.masm���ʽ
����3.���ָʾ�� ����
__��ָ���������رasm���в�����masm�еĶ���ָʾ��:
DB��dw��dd��dq��dt��Df��Ҳ������dup��This�����masm�ṹ� �¼Ҳ������Ч����j��
����Struc��record��width����Mask��
����4.even��Alignָʾ��
����5.masm��ָʾ��
������j�� �Ǻ��
����ʹ��masm��ָʾ��(MACro��rept��irc��irp��Endm)�ͺ�����(<>��!��&��%��.type)��
����6.��˵��
��������ʹ�üĴ���4˵��Σ��� �α�����ʽ��
���� ˵� es:[bx]�
����7.���ͺ � ��С
�С�������һ�����飬��ı���һ�������еĵ��� �صĴ�С��
����8.��ע
��������ʹ��c/c++������ͣ�עƼ���Asm���";"���ͣ�עš�
����9._emitαָ��
����_emitαָ��� ��masm�е�Db����һ��ֻ����
ܶ һ���ֽ�����ڣ:
__asm
{
jmp _codeofasm
_codeofasm:
; �����Ǵ���
_emit 0x90 ; nopָ��
}
�����ġ���__asm����ʹ��c/c++���� ��
����c/c++������Ի��ʹ�ã�����j������ʹ��c/c++�ı� �ͺ �����
ܶ c/c++�� �
__���ءasm���п���ʹ������c/c++ ��:
����1.��ţ���(��š��� �ͺ�����
����2.�� ����
� (�ų� ��
� ö��� (enum)��Ա��
����3.�궨��� ����ָʾ��
����4."(���ͣ�ע/**/"��"//"��
����5.�������(����Masm�к �������
����__asm���в���ʹ����<<һ���C/c++�����c/c++��Masm �ıIJ�������"*"��"[]"���������Ϊ�ǻ��
��� IJ���������ٸ:
int array[10];
�������C/C++�е�� �ṹ����ö�
����Ψһ�ģ��������"."�����֮ǰ���ϱ� ���typedef��ơ����磬
�����}��ṹ������same_name����Ա�� :
struct first_type
{
char *weasel;
int same_name;
};
struct second_type
{
int wonton;
long same_name;
};
���������������� :
__asm
{
mov ebx, offset hal
mov ecx, [ebx]hal.same_name ; ����ʹ�� 'hal'
mov esi, [ebx].weasel ; ����ʡ�� 'hal'
}
�����塢�Ĵ���ʹ��
��Ĵ����ֵ��(����msdn��˵�ģ�����ʵ��ʹ��ʱ���֣���� �������������˵"һ��"����������)
�������һ�����������__fastcall�����������ڼĴ����У��⽫��Ĵ���Ĺ����4��� ���
����Ҫ��һ���������__fastcall�����뱣
�������/gr����ѡ��(��ȫ�ֵı��__fastcall)����ÿ���������__cdecl����__stdcall�������� ��
������ô� ��C������
�Ĵ����Ǿ�Ӧ��Push������Щ�Ĵ���
�����������иı�������std��Cld�ķ����������־뽫��ָ��� 4��ֵ��
��������ת
����������C����ʹ��goto��__asm���еı�Ŵ���Ҳ������__asm����__��תasm�����������ı�Ŵ���__a
sm���ڵı���Dz���ִ�Сд��(ָ� ʾ���Ҳ�Dz���ִ
ָ �Сд��)����:
void func()
{
goto c_dest; /* � � */
goto c_dest; /* ���� */
goto a_dest; /* � � */
goto a_dest; /* � � */
__asm
{
jmp c_dest ; � �
jmp c_dest ; msdn��˵� �����������
� vs.netб�� 룬��Ϊ���� �
jmp a_dest ; � �
jmp a_dest ; � �
c_dest: /* c�ı�� */
return;
}
������Ҫʹ�ú�����Ƶ����ţ�����ʹ������ִ�ж��DZ�Ŵ���������ʾ:
; ����: ʹ�ú�������Ϊ���
jne exit
.
.
.
exit:
; �����Ǹ���Asm����
������ ���$����ָ����ǰλ�ã��������ã�����������ת:
����� ����ú���
#include
//��j������C�������� �����
//�ò�ʹ�ı�Ebx�Ĵ������� ����ADD esp, 12
pop ebx
pop ebx
pop ebx
}
}
������ע:��������Ǵ�������ѹ ��
������������
ܹ C++�е����Ա�����ǿ��Է���extern "c"����
�����������������:
#include
void main()
{
char szhello[] = "hello, world!";
__asm
{
push mb_ok or mb_iconinformation
push offset szappname ; ȫ�ֱ� ��offset
����һ��4˵����visual c++��ʹ����j�����Ϊ������ٶȣ������Щ������þ�������C/c++д��
�����ˡ�һ������
�����������������Vs.net(��vc7)��c����д�ġ�� �һ��̣������д���ŵ������е�.c�ļ��б��룬
������������رã����ɱ��� ��
////////////////////////////////////////////////////////////////////////
// ����
#include
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//ȫ�ֱ�
hwnd g_hwnd;
hinstance g_hinst;
tchar sztemp[1024];
/////////////////////////////////////////////////////////////////////
//��������
dword getcrc32(const byte *pbdata, int nsize);
int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline,
int icmdshow);
lresult callback windowproc(hwnd hwnd, uint umsg, wparam wparam, lparam lparam);
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//����
int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline,
int icmdshow)
{
msg msg;
wndclassex wndclassex;
g_hinst = hinstance;
wndclassex.cbsize = sizeof(wndclassex);
wndclassex.style = cs_vredraw | cs_hredraw;
wndclassex.lpfnwndproc = (wndproc) windowproc;
wndclassex.cbclsextra = 0;
wndclassex.cbwndextra = 0;
wndclassex.hinstance = g_hinst;
wndclassex.hicon = loadicon(null, idi_application);
wndclassex.hcursor = loadcursor(null, idc_arrow);
wndclassex.hbrbackground = (hbrush) (color_window);
wndclassex.lpszmenuname = null;
wndclassex.lpszclassname = szappname;
wndclassex.hiconsm = null;
registerclassex(&wndclassex);
showwindow(g_hwnd, icmdshow);
updatewindow(g_hwnd);
//////////////////////////////////////////////////////////////////
���ڻ
//��صص
LResult callback windowproc(hwnd hwnd, uint umsg, wparam wparam, lparam lparam)
{
switch (umsg)
{
case wm_create:
createwindowex(ws_ex_clientedge, "edit", null, ws_child | ws_visible | ws_border |
es_autohscroll | es_autovscroll | es_nohidesel | ws_overlapped,
7, 12, 220, 22,
hwnd, (hmenu)1000, g_hinst, null);
createwindowex(0, "button", "&ok", ws_child | ws_visible | bs_pushbutton |
ws_overlapped | bs_flat,
244, 12, 40, 20,
hwnd, (hmenu)idok, g_hinst, null);
break;
case wm_command:
switch (loword(wparam))
{
case idok:
getdlgitemtext(g_hwnd, 1000, sztemp + 100, 800);
wsprintf(sztemp, "��ǰ�ı������ַ��ڵcrc32У������: 0x%lx", getcrc32(sztemp + 100,
(int)strlen(sztemp + 100)));
messagebox(g_hwnd, sztemp, szappname, mb_ok|mb_iconinformation);
}
break;
case wm_destroy:
postquitmessage(0);
break;
default:
return (defwindowproc(hwnd, umsg, wparam, lparam));
}
return (0);
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//getcrc32: ���ֽ����Crc32У����
//����:
// pbdata: ָ���ֽ�������ַ�
// nsize: �ֽ����
//
//����ֵ:
// �ֽ����Crc32У����
//
//����ʹ�ò���crc32У���룬� ���Dzο���� ����¡� ì��ܵĽ� ��2������crc ��ƪ����д�ġ�
// �ĵľ���������ο�:
http://asp.7i24.com/netcool/laoluo/articles/show_article.asp?article_id=15
//
//����ʹ����j�����crc32У���룬���ʹ����cpu�еļĴ����ٶȺͷ����Զ���ʹ��c/c++�������
ܱ
//
dword getcrc32(const byte *pbdata, int nsize)
{
dword dwcrc32table[256];
__asm //��Ƭ��j����dz�ʼ��crc32��
{
mov ecx, 256
_nexttable:
lea eax, [ecx-1]
push ecx
mov ecx, 8
_nextbit:
shr eax, 1
jnc _notcarry
xor eax, 0xedb88320
_notcarry:
dec ecx
jnz _nextbit
pop ecx
mov [dwcrc32table + ecx*4 - 4], eax
dec ecx
jnz _nexttable
}
__asm //��������crc32����
{
mov eax, -1
mov ebx, pbdata
or ebx, ebx
jz _done
mov ecx, nsize
or ecx, ecx
jz _done
_nextbyte:
mov dl, [ebx]
xor dl, al
movzx edx, dl
shr eax, 8
xor eax, [dwcrc32table + edx*4]
inc ebx
loop _nextbyte
_done:
not eax
}
}