及时编译汇编程序并跳转到c ++程序的结果 [英] compiling assembler just in time and jumping to the result from a c++ program

查看:88
本文介绍了及时编译汇编程序并跳转到c ++程序的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要是出于测试原因,我想看看是否有必要选择

以下方法为

的着色器及时编译渲染器:

看起来像着色器他们主要是非常基本的操作

我想将它们翻译成汇编,让汇编程序编译

二进制代码,然后从c ++调用生成的机器代码。


事情就是到现在为止我只使用内联汇编我的

c ++项目,所以有一些我几乎不知道的事情和

如果有人在这里可以指向我正确的方向

方向:
- 有一组asm指令,比如说addl 5,%% eax,或者分别添加eax,5

,我如何将这一行翻译成

二进制文件? (在某种程度上,并不意味着我必须重新编写整个

的东西,如果可能的话,移植到不同的操作系统:)

- 如何从我的c ++应用程序中跳转到二进制文件,我可以在汇编代码段结束时返回



谢谢!

解决方案

sp *** ***@crayne.org napsal:


主要是出于测试原因我想看看是否有意义选择

以下为

渲染器及时编译着色器的方法:

看作着色器,他们主要是非常基本的操作
我想将它们翻译成汇编,让汇编程序编译

二进制代码,然后从c ++调用生成的机器代码。


事情就是到现在为止我只在我的
中使用了内联汇编
c ++项目,所以有一些我几乎不知道的事情和

如果有人在这里可以指出我正确的方向

方向将是非常伟大的:

- 有一组asm指令,比如说addl 5,%% eax。或者分别添加eax,5

,我如何将这一行翻译成

二进制文件? (在某种程度上,并不意味着我必须重新编写整个

的东西,如果可能的话,移植到不同的操作系统:)

- 如何从我的c ++应用程序中跳转到二进制文件,我可以在汇编代码段结束时返回



谢谢!



在实际操作系统中,你通常不能简单地在

内存中创建一些数据,然后说这是我的例程,跳转有" (它可以在
DOS下工作,但是谁现在关心DOS ...)。


没有标准的C ++解决方案。但你可以这样做:

- 生成汇编程序(或C,C ++,无所谓)源代码

某些文件

- 用你最喜欢的编译器编译它

- 将它链接到动态库

- 将这个库加载到内存中,解析符号并调用你的函数

对于编译,您需要启动一个新流程。启动流程从

您的程序是不可移植的。在POSIX系统上使用fork和exec(或execl或类似的)

或Windows中的CreateProcess。

从C ++的角度来看,加载动态库也是非标准的。

但是它在带有dlopen系列的POSIX平台上以及带有LoadLibrary的Windows平台上是标准化的。函数有非常类似的API,它可以简单地用#ifdef''ed来定义。


而不是#ifdefs你可以使用一些隐藏平台的库

特定问题(如ACE,wxWidgets或其他任何东西)实现

启动进程和加载库。


< blockquote> Ondra Holub写道:


在实际操作系统中,你通常不能简单地在

内存中创建一些数据然后说它是我的惯例,跳到那里 (它可以在
DOS中工作,但是谁现在关心DOS ......)。



我不确定实际操作系统是什么你有这个想法,但是在

Windows中,你肯定*可以*直接将机器代码写入

内存''并立即执行它。这就是BBC BASIC for Windows中

汇编程序的工作原理,如果没有这种能力,那么语言将会严重削弱!一些现代处理器可以设置为阻止代码在数据内存中执行,但在32位Windows中它并不是常态的b $ b。 />

Richard。

BBC BASIC for Windows的作者。
http://www.rtrussell.co.uk/


- 有一套asm指令,说addl 5,%% eax或者添加eax,5或


,我怎样才能将这一行翻译成

二进制文件?



寻找已经做到这一点的解决方案。它可能是商业上的b / b
。谷歌是你的朋友。


谁知道,也许某人已经免费编写了一个x86编译器,纯粹的
C ++代码。编写编译器实际上并不难,只要你坚持最小的实现并实现1%的功能

使用了99%的编写asm编译器应该是

实际上很容易做到。你只需拿起一本x86手册就可以获得
吧。


(以某种方式并不意味着我会拥有如果可能的话,在移植到不同的操作系统时重写整个

的东西:)



假设操作系统仍然在相同的处理器......我想不应该是一个问题。但是,在两个操作系统上获取编译器将是一个不同的问题。


- 如何从我的c ++应用程序跳转到二进制文件以某种方式我可以在汇编代码段结束时jmp




不知道对不起。答案应该很简单。


Mostly for testing reasons I''d like to see if it makes sense to chose
the following approach for just-in-time compilation of shaders for a
renderer:
Seeing as the shaders themsefs consist mostly of very basic operations
I''d like to translate them into assembly, have an assembler compile the
binary code and then call the resulting machine code from c++.

The thing is that up until now I have only used inline assembly in my
c++ projects, so there''s a few things I hardly know anything about and
would be very greatful if anyone here could point me in the right
direction:
- Having a set of asm instructions, say "addl 5, %%eax" or "add eax, 5"
respectively, how would I go about translating just this one line into
binary? (in a way that doesn''t mean i''ll have to re-write the whole
thing when porting to a different os if at all possible :)
- How do I jump into the binary from my c++ app in a way that I can jmp
back at the end of my assembly code segment?

Thanks!

解决方案

sp******@crayne.org napsal:

Mostly for testing reasons I''d like to see if it makes sense to chose
the following approach for just-in-time compilation of shaders for a
renderer:
Seeing as the shaders themsefs consist mostly of very basic operations
I''d like to translate them into assembly, have an assembler compile the
binary code and then call the resulting machine code from c++.

The thing is that up until now I have only used inline assembly in my
c++ projects, so there''s a few things I hardly know anything about and
would be very greatful if anyone here could point me in the right
direction:
- Having a set of asm instructions, say "addl 5, %%eax" or "add eax, 5"
respectively, how would I go about translating just this one line into
binary? (in a way that doesn''t mean i''ll have to re-write the whole
thing when porting to a different os if at all possible :)
- How do I jump into the binary from my c++ app in a way that I can jmp
back at the end of my assembly code segment?

Thanks!

In real operating system you usually cannot simply create some data in
memory and then say "It''s my routine, jump there" (It could work in
DOS, but who cares about DOS now...).

There is no standard C++ solution for this. But you can do it this way:
- generate your assembler (or C, C++, it does not matter) source into
some file
- compile it with your favourite compiler
- link it into dynamic library
- load this library into memory, resolve symbols and call your function

For compilation you need start a new process. Starting processes from
your program is non-portable. Use fork and exec (or execl or similar)
on POSIX systems or CreateProcess in Windows.

Loading dynamic library is also non standard from C++ point of view.
However it is standardized on POSIX platforms with dlopen family and on
Windows platform with LoadLibrary. Functions have very simillar API, it
may be simply #ifdef''ed.

Instead of #ifdefs you can use some library which hides platform
specific issues (like ACE, wxWidgets or anything else) which implements
starting processes and loading library.


Ondra Holub wrote:

In real operating system you usually cannot simply create some data in
memory and then say "It''s my routine, jump there" (It could work in
DOS, but who cares about DOS now...).

I''m not sure what "real operating system" you had in mind, but in
Windows you most certainly *can* write machine code directly into
memory ''on the fly'' and execute it there immediately. That''s how the
assembler in BBC BASIC for Windows works, and without that capability
the language would be severely crippled! Some modern processors can be
set to prevent code execution in ''data'' memory, but it''s not the norm
in 32-bit Windows.

Richard.
Author of BBC BASIC for Windows.
http://www.rtrussell.co.uk/


- Having a set of asm instructions, say "addl 5, %%eax" or "add eax, 5"

respectively, how would I go about translating just this one line into
binary?

Look for a solution that does this already. It''s probably something
commercial. Google is your friend.

Who knows, maybe someone has written an x86 compiler for free, in pure
C++ code. Writing compilers isn''t hard at all actually, as long as you
stick to minimal implementation and implement the 1% of the features
that is used 99% of the time ;) Writing an asm compiler should be
really easy to do yourself actually. Just pick up an x86 manual and
away you go.

(in a way that doesn''t mean i''ll have to re-write the whole
thing when porting to a different os if at all possible :)

Assuming the OS is still on the same processor... shouldn''t be a
problem I imagine! Getting the compiler on both OSs will be a different
matter, though.

- How do I jump into the binary from my c++ app in a way that I can jmp
back at the end of my assembly code segment?

No idea sorry. The answer should be simple though.


这篇关于及时编译汇编程序并跳转到c ++程序的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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