內嵌彙編練習-1


//compiler: visual c++

//system: win7


#include <stdio.h> 


#define pureasmcall(retval)\
__declspec(naked) retval _stdcall

// desc: directly return n
pureasmcall(int) getparam(int)
{
__asm{
push ebp
mov ebp,esp
add ebp,8
mov eax,[ebp]
pop ebp
ret 4
}
}




// desc: return a+b
pureasmcall(int) add(int a, int b) 
{
__asm{
push ebp 
mov ebp,esp 
add ebp,8
mov eax,[ebp]
add eax,[ebp+4]
pop ebp 
ret 8
}
}


// desc: directly return the pointer
pureasmcall(char*) getpointer(char * p)
{
__asm{
push ebp
mov ebp,esp 
add ebp,8
 
mov eax,[ebp]
 
pop ebp 
ret 4
}
}


class CC
{
public:
CC():a(11),b(22){}
int a;
int b;
};


pureasmcall(int) getclassvar(const CC *)
{
__asm{
push ebp 
mov ebp,esp 
add ebp,8

// get the class object address
mov esi,[ebp] //error: mov esi,p

mov eax,[esi] 

pop ebp 
ret 4
}
}


int main(int argc, char * argv[])
{
printf("value: %d\n", getparam(5));
printf("addr: %d\n", add(4,5)); 
char * p = "abc";
printf("address:%x %x\n", getpointer(p), p);
CC cc;
printf("class var:%d\n", getclassvar(&cc)); 
return 1;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章