📄 asmlearn1.cpp
字号:
// asmlearn1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <wchar.h>
#include <tchar.h>
#include "testh2inc.h"
using namespace std;
extern "C" int __stdcall Test1(int val);
extern "C" char g_fileName[]; //变量声明时也要用extern "C"
extern "C" char g_pfileName[]; //label 指示的标识符不能指定指针
//extern "C" *char g_fileName; //本句与上句不同,
//上边的g_fileName代表串本身的地址,而本句中的g_fileName表示一个变量,他的内容是串的地址,
extern "C" H2INC2 testh2inc2;
extern "C" g_Version;
extern "C" char g_Data[];
extern "C" char g_Time[];
extern "C" g_Cpu;
//extern "C" char g_Environ[];
extern "C" char g_Interface[];
extern "C" g_Line;
int power2( int num, int power );
char * TestInlineAsm(int i,char *p,int k);
//声明一个共享段
#pragma section("myShareData",read,write,shared)
//在一个段内声明一个变量
//__declspec(allocate("myShareData")) int g_TestVal = 0;
//或者
/*
#pragma data_seg("shared")
HHOOK g_hProc = NULL; // 窗口过程钩子句柄
HHOOK g_hKey = NULL; // 键盘钩子句柄
HWND g_hRich = NULL; // 文本框句柄
#pragma data_seg()
#pragma comment(linker, "/section:shared,rws")
*/
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
/*
*
测试进程间共享内存
*/
//while(1)
//{
// Sleep(200);
// cout<< g_TestVal++ << endl;
//}
cout<<Test1(4) << endl;
// cout<<(char *)Test1(4) << endl;
cout << g_fileName <<endl;
cout << "g_Version: " << g_Version <<endl;;
cout << "g_Data: " << g_Data <<endl;;
cout << "g_Time: " << g_Time <<endl;;
cout << "g_Cpu: " << g_Cpu <<endl;;
//cout << "g_Environ: " << char g_Environ[] <<endl;;
cout << "g_Interface: " << g_Interface <<endl;;
cout << "g_Line: " << g_Line <<endl;;
cout << g_pfileName;
cout<< testh2inc2.k;
int i=33;
int j,k;
testproc1(i, (int)&j, (int)&k);
ll4:
return 0;
}
/*
*
#pragma alloc_text( "textsection", function1, ... )
textsection为要把指定得函数放到test段得哪一个sections里边,一般来说比较常用得是init和page。
init节中得函数在函数初始化之后就会从内存在清除(当然,虚地址还是保留得),比较适合一些初始化得函数,这样可以节省内存空间。
page是将函数放在内存得分页区(相对来说,windows下得未分页区是比较宝贵得资源)
*/
/*
*
Each assembly-language statement can contain only one C or C++ symbol. Multiple symbols can appear in the same assembly instruction only with LENGTH, TYPE, and SIZE expressions.
Functions referenced in an __asm block must be declared (prototyped) earlier in the program. Otherwise, the compiler cannot distinguish between function names and labels in the __asm block.
An __asm block cannot use any C or C++ symbols with the same spelling as MASM reserved words (regardless of case). MASM reserved words include instruction names such as PUSH and register names such as SI.
Structure and union tags are not recognized in __asm blocks.
typedef names, generally used with operators such as PTR and TYPE or to specify structure or union members
*/
/*
*
struct first_type hal;
struct second_type oat;
__asm
{
// mov ebx, OFFSET hal
mov ecx, [ebx]hal.same_name ; Must use 'hal'
mov esi, [ebx].weasel ; Can omit 'hal'
}
*/
/*
*
#define randasm __asm _emit 0x4A __asm _emit 0x43 __asm _emit 0x4B
.
.
.
__asm {
randasm
}
You must refer to segments by register rather than by name (the segment name _TEXT is invalid, for instance). Segment overrides must use the register explicitly, as in ES:[BX].
__asm C Size
LENGTH arr sizeof(arr)/sizeof(arr[0]) 8
SIZE arr sizeof(arr) 32
TYPE arr sizeof(arr[0]) 4
When using __asm to write assembly language in C/C++ functions, you don't need to preserve the EAX, EBX, ECX, EDX, ESI, or EDI registers.
Note If your inline assembly code changes the direction flag using the STD or CLD instructions, you must restore the flag to its original value
An __asm block can call only global C++ functions that are not overloaded. If you call an overloaded global C++ function or a C++ member function, the compiler issues an error
*/
/*
*
C macros offer a convenient way to insert assembly code into your source code, but they demand extra care because a macro expands into a single logical line. To create trouble-free macros, follow these rules:
Enclose the __asm block in braces.
Put the __asm keyword in front of each assembly instruction.
//Use old-style C comments ( /* comment *///) instead of assembly-style comments ( ; comment) or single-line C comments ( // comment).
/*To illustrate, the following example defines a simple macro:
#define PORTIO __asm \
/* Port output */ //可以这样注释\
{ \
__asm mov al, 2 \
__asm mov dx, 0xD007 \
__asm out dx, al \
}
void func( void )
{
goto C_Dest; /* Legal: correct case */
//goto c_dest; /* Error: incorrect case */
goto A_Dest; /* Legal: correct case */
//goto a_dest; /* Legal: incorrect case */
__asm
{
jmp C_Dest ; Legal: correct case
//jmp c_dest ; Legal: incorrect case
jmp A_Dest ; Legal: correct case
//jmp a_dest ; Legal: incorrect case
a_dest: ; __asm label
}
C_Dest: /* C label */
return;
}
int power2( int num, int power )
{
__asm
{
mov eax, num ; Get first argument
mov ecx, power ; Get second argument
shl eax, cl ; EAX = EAX * ( 2 to the power of CL )
}
/* Return with result in EAX */
}
__declspec (naked) char * TestInlineAsm(void)//当无参数时可以,或者不能改变寄存器的时候用naked
{
// Naked functions must provide their own prolog
__asm {
push ebp
mov esp, ebp
sub esp, __LOCAL_SIZE
}
_asm
{
mov eax, 0
ret
}
// ... and epilog
__asm {
pop ebp
ret
}
}
char * TestInlineAsm2(int i,char *p,int k)//当有参数时不要用naked
{
__asm
{
// org 500000 不允许
mov eax, 0;
//cupid ;
ret ;
}
}
/*
point struct
x byte ?
y byte ?
point ends
*
mov al,-3
cbw
nop
mov eax,55aah
bswap eax
nop
pushfd
pop eax
nop
mov bx,1
neg bx
nop
mov eax,-23
cdq
mov ecx,4
idiv ecx
mov eax,-23
sar eax,2
mov eax,0aa5555aah
bt eax,4
bt ax,6
nop
btc ax,3
btc ax,3
setc bl
setz cl
seto dl
mov ax,a1
movzx eax,ax
;add eax,a2
;daa
mul a2
aam
jmp ll1
jmp LoadLibraryA
jmp ll3
mov ebx,0401003h
jmp ebx
;jmp ll4
ll1:
nop
ll2:
nop
revsered DB 125 DUP(0AAh)
ll3:
nop
mov ecx,3
mov eax,0
ll5:
add eax,eax
mov i1,2
mov i2,3;
loopz ll5 ;//当ECX!=0,ZF=1时循环
jcxz
jecxz
mov eax,i2;
ret ;//根据过程的调用方式自动正确返回
IF (@VERSION GT 600) @VERSION 不支持
mov eax,600
ENDIF
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -