关于naked (C++)

本文探讨了C++中的naked关键字,该关键字使得编译器不生成函数的prolog和epilog代码,适用于自定义汇编序列,特别是在编写虚拟设备驱动中。通过测试代码和反汇编分析,指出naked函数不处理局部变量栈空间,不包含返回指令,需要手动添加,且仅适用于x86平台,不可用于x64或Itanium。
摘要由CSDN通过智能技术生成

MSDN中关于naked关键字的介绍:

For functions declared with the naked attribute, the compiler generates code without prolog and epilog code. You can use this feature to write your own prolog/epilog code sequences using inline assembler code. Naked functions are particularly useful in writing virtual device drivers. Note that thenaked attribute is only valid on x86, and is not available on x64 or Itanium.


在网上搜了些资料后不是很理解,写了测试代码并反汇编了下:

#include <stdio.h>

void call_me(int x, int y)
{
	int a = x;
	static int b;
	int c;
	b = y;
	c = a+b;
}

void __declspec (naked) kkk(int a, int b) {
	static int c;
	int d;
	c = a + b;
	d = c;
	call_me(a, d);
}

void aaa(int a, int b) {
	static int c;
	int d;
	c = a + b;
	d = c;
	call_me(a, d);
}

int main()
{
	kkk(5, 6);
	aaa(5, 6);
	getchar();
	return 1;
}

主要想看看函数编译后,函数中栈的情况变化以及nakedshu属性的函数中,进行函数调用的情况,反汇编后如下:

 
.text:00401000 sub_401000      proc near               ; CODE XREF: .text:0040104Cp
.text:00401000                                         ; sub_401060+20p
.text:00401000
.text:00401000 var_8           = dword ptr -8
.text:00401000 var_4           = dword ptr -4
.text:00401000 arg_0           = dwor
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值