内联汇编 - 运行时堆栈分配 [英] Inline Assembly - Runtime Stack Allocation

查看:68
本文介绍了内联汇编 - 运行时堆栈分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用临时缓冲区来传输一些数据,而宁愿将
分配到堆上。问题是

缓冲区的大小只有在进入使用它的函数时才知道。

我宁可不浪费空间或动态分配堆(因为

它不需要坚持)。现在我正在计划利用内联

汇编来直接修改堆栈指针以分配我需要的空间。


这是一个例子:


void doSomeStuff(无符号大小){

char * rawData;

// INLINED (英特尔p4组装)

// sub esp,dword ptr [size]

// mov dword ptr [rawData],esp


//使用临时缓冲区做一些事情,使用rawData访问




// INLINED(英特尔p4程序集)

//添加esp,dword ptr [size]

}


据我所知,它应该可以正常使用,就像魔法一样 - 没问题

。但是,这个假设是正确的吗?是否有任何奇怪的怪癖

我应该注意到我可能没有想到的?

I''m using a temporary buffer to transfer some data and would rather
not allocate it on the heap. The problem is that the size of the
buffer is only known upon entry into the function that utilizes it and
I''d rather not waste space or dynamically allocate on the heap (since
it doesn''t need to persist). Now I''m planning on utilizing inline
assembly to modify the stack pointer directly to allocate the space I
need.

Here''s an example:

void doSomeStuff(unsigned size) {
char* rawData;
//INLINED (Intel p4 assembly)
//sub esp, dword ptr [size]
//mov dword ptr [rawData], esp

//do some stuff with the temporary buffer, using rawData to access
it

//INLINED (Intel p4 assembly)
//add esp, dword ptr [size]
}

As far as I know, it should work just fine, like magic - no problems
at all. BUT, is this assumption correct? Are there any odd quirks that
I should be aware of that I may not have thought of?

推荐答案

Brian Lindahl <李****** @ hotmail.com>潦草地写道:
Brian Lindahl <li******@hotmail.com> scribbled the following:
我正在使用临时缓冲区来传输一些数据,而不是在堆上分配它。问题是
缓冲区的大小只有在进入使用它的函数时才知道。
我宁愿不浪费空间或在堆上动态分配(因为
它不需要坚持)。现在我正计划利用内联
程序集直接修改堆栈指针以分配我需要的空间。
I''m using a temporary buffer to transfer some data and would rather
not allocate it on the heap. The problem is that the size of the
buffer is only known upon entry into the function that utilizes it and
I''d rather not waste space or dynamically allocate on the heap (since
it doesn''t need to persist). Now I''m planning on utilizing inline
assembly to modify the stack pointer directly to allocate the space I
need.




内联汇编是comp.lang.c.本质上是特定于实现的,因此是非主题的

。请在专属于您自己的

实施的新闻组中询问。


-

/ - Joona Palaste(pa ** ***@cc.helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste --------- ------------规则! -------- /

计算机程序执行你告诉它要做的事情,而不是你想要它做什么。

- Anon



Inline assembly is inherently implementation-specific and thus off-topic
on comp.lang.c. Please ask in a newsgroup dedicated to your own
implementation.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A computer program does what you tell it to do, not what you want it to do."
- Anon


2004年6月30日23:44:19 -0700, li ** ****@hotmail.com (Brian Lindahl)

写道:
On 30 Jun 2004 23:44:19 -0700, li******@hotmail.com (Brian Lindahl)
wrote:
我正在使用临时缓冲区来传输一些数据并且宁愿不在堆上分配它。问题是
缓冲区的大小只有在进入使用它的函数时才知道。
我宁愿不浪费空间或在堆上动态分配(因为
它不需要坚持)。现在我正计划利用内联
程序集直接修改堆栈指针来分配我需要的空间。

这是一个例子:

void doSomeStuff(无符号大小){
char * rawData;
// INLINED(英特尔p4程序集)
// sub esp,dword ptr [size]
/ / mov dword ptr [rawData],esp

//用临时缓冲区做一些事情,用rawData访问

// INLINED(英特尔p4装配)
//添加esp,dword ptr [size]


据我所知,它应该工作得很好,就像魔法一样 - 没问题
一点都不但是,这个假设是正确的吗?是否有任何奇怪的怪癖我应该知道我可能没有想过?
I''m using a temporary buffer to transfer some data and would rather
not allocate it on the heap. The problem is that the size of the
buffer is only known upon entry into the function that utilizes it and
I''d rather not waste space or dynamically allocate on the heap (since
it doesn''t need to persist). Now I''m planning on utilizing inline
assembly to modify the stack pointer directly to allocate the space I
need.

Here''s an example:

void doSomeStuff(unsigned size) {
char* rawData;
//INLINED (Intel p4 assembly)
//sub esp, dword ptr [size]
//mov dword ptr [rawData], esp

//do some stuff with the temporary buffer, using rawData to access
it

//INLINED (Intel p4 assembly)
//add esp, dword ptr [size]
}

As far as I know, it should work just fine, like magic - no problems
at all. BUT, is this assumption correct? Are there any odd quirks that
I should be aware of that I may not have thought of?




comp.lang.asm.x86就在那边----->

-

Sev



comp.lang.asm.x86 is right over there ----->
--
Sev


li ****** @ hotmail.com (Brian Lindahl)写道:
li******@hotmail.com (Brian Lindahl) wrote:
我正在使用临时缓冲区来传输一些数据,而不是在堆上分配它。问题是
缓冲区的大小只有在进入使用它的函数时才知道。
我宁愿不浪费空间或在堆上动态分配(因为
它不需要坚持)。现在我正在计划利用内联
程序集直接修改堆栈指针以分配我需要的空间。
I''m using a temporary buffer to transfer some data and would rather
not allocate it on the heap. The problem is that the size of the
buffer is only known upon entry into the function that utilizes it and
I''d rather not waste space or dynamically allocate on the heap (since
it doesn''t need to persist). Now I''m planning on utilizing inline
assembly to modify the stack pointer directly to allocate the space I
need.




这通常是一个非常非常糟糕的主意。你有什么实际的理由使用malloc(),这非常适合这个任务?
_not_来吧

想到它,因为你只把这个空间用作转移缓冲区,

为什么你不能用固定大小的块传输你的数据?


Richard



This is usually a very, very bad idea. What actual reasons have you for
_not_ using malloc(), which is quite nicely suited to this task? Come to
think of it, since you''re using this space only as a transfer buffer,
why can''t you transfer your data in fixed-size chunks?

Richard


这篇关于内联汇编 - 运行时堆栈分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆