C/C++ 内嵌 RETN 与 JMP 指令调用函数(x86)

    本文列出的代码用于表示在 x86 环境下,如何通过 RETN 指令调用函数,这类方法调用函数有一些迷惑性,但意义并不是很大,而且相对麻烦一点,CALL 指令帮你处理了很多事情,例如 push 当前的 eip,栈放大,cookie,调用的目标函数执行到 retn 指令时“平衡堆栈”后 pop eip 回到 caller 的下一指令位置。

#include <stdio.h>

static char format[] = "n=%d\n";

int add(int x, int y) 
{
	return x + y;
}

int main()
{
	_asm
	{
		sub     esp, 12 // 4+4+4
		push	2 // [ebp+4]
		push	1 // [ebp+8]
		push	__reteip_addproc // [ebp+4]
		push	add // push
		retn	0	// RETN 0 = RETN = RET = C3
	}
	__reteip_addproc:
	_asm
	{
		add     esp, 12 // reduce stack
		// call printf proc
		push	eax	// 3
		lea		eax, dword ptr[format]
		push	eax
		call	printf
	}
	return 0;
}

    JMP 指令调用函数与 RETN 指令调用函数类似,但唯一的不同就是,JMP 指令并不需要在调用 RETN 指令前 在手动的 push 要调用的函数的地址,JMP 目标函数的相对地址就可以了。

#include <stdio.h>

static char format[] = "n=%d\n";

int add(int x, int y) 
{
	return x + y;
}

int main()
{
	_asm
	{
		sub	esp, 12 // 4+4+4
		push	2 // [ebp+4]
		push	1 // [ebp+8]
		push	__reteip_addproc // [ebp+4]
		jmp		add // push
	}
	__reteip_addproc:
	_asm
	{
		add	esp, 12 // reduce stack
		// call printf proc
		push	eax	// 3
		lea		eax, dword ptr[format]
		push	eax
		call	printf
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值