Controlling program flow through memory addressees in c / c++

164 views Asked by At

Sorry if the title is a little obscure, I am not a native speaker and had a bit of trouble formulating my idea...

Assuming I have all the necessary functions and objects for a collection of procedures to be executed compiled and created in memory and that I know their addresses and sizes, how can I control the flow of the program with a pseudo "binary" file that is basically a script that says "push this data into this function's address, call this function address, push the returned value into this function address" and so on...

Basically I need to process pseudo "machine" code to access and dynamically control a collection of interconnected objects and static logic through their memory addresses.

Thanks!

EDIT: Please, post a few code snippets before closing my question as a duplicate to a question that doesn't really provide the specific information I need.

EDIT2: Added this from a comment below, possibly it will bring more clarification to my question:

Instead of compiling entire programs I try to use pre-compiled components to create dynamic objects on the go. The trees of objects can serialize to disk and be recreated in an instant (allocating the entire tree in a pool rather than object by object) so I have a way to create a dynamic program, save it to disk and reconstruct it in memory, with all the design time identifiers substituted with their addresses. NOW all I need is a way to make that entire program structure run.

2

There are 2 answers

1
jim mcnamara On

This is essentially what the Java Virtual Machine does, which I believe is what others are referring to as a machine. It is an interpreter, which is probably what you need to construct.

Assuming I understand what you said - the answer is no, not like that. You will have to do something to create program state and control logic flow, which is what interpreters and "machines" do.

You will need a front end that reads your code file, the file that tells the program how to run. Your front end reads the "logic file", then runs through the logic your front end just learned to call each of your objects. You can create this by linking smart plugins that do the same things, ie. construct a program state from your objects. But then you need a bunch of different plugins, one for each instance.

In any event there is no way to link your objects and then expect them to know who runs in what order, and who does not run. You have to transfer the "smarts" of your file into action somehow. Linking alone will not do it, because you want the smarts to be external to your object library.

5
user1118321 On

It sounds like you need to write an emulator for the machine whose language you want to interpret. Here is a link that explains how to do it.