Message Board


Message Board > C/C++ > simulating the DIV's frame() thing in c++

October 1, 2006, 10:45
OScoder
None
1338 posts
Hi,
I'm currently sttempting c++ game programing, and I'm wondering: would it be possible to simulate the div 'frame' statement in c++?

I wondered about the following:
- class 1 calls this.frame
- frame jumps to class 2's frame
- class 2's frame returns into class
- class 2 calls this.frame
- frame jumps to class 1's frame
- class 1's frame returns

But then of course, since goto statements must have their labels in the same function, one would have to mess with inline asm to get it working!

Another idea I had was to have a frame statement that messed with the stack:
- class 1 calls frame()
- frame() puts return data into a structure, and gets class 2's off
- frame() returns (into class 2)
- class 2 calls frame()
- frame() puts return data into a structure, and gets class 1's off

Any other suggestions?
____________
om
#
October 1, 2006, 13:01
PEader
お前はもう死んでいる
1486 posts

The first idea is ill advised due to the ability to randomly jump from one bit of code to any other bit which would make debuging a nightmare.

What you are trying to implement is a coroutine. There is a good article on Wikipedia about them. Several years ago I tried to do something similar to this but it was a lot of hassle. I got most of my ideas then from an article on GameDev.net called Scripting in C using Co-Routines.

It is doable to emulate the frame function and it's intresting to mess around with set_jmp but there must be a better way to implement frame in C++?!

[Edited on October 1, 2006 by PEader]
____________
I see 57,005 people.
#
October 1, 2006, 13:09
yonni
None
420 posts
Wasn't fenix writen in C/C++? And can't you get the source code at sourceforge? Presumably looking at the way they did it would be best?
____________
#
October 1, 2006, 14:06
PEader
お前はもう死んでいる
1486 posts

Quoting yonni:
Wasn't fenix writen in C/C++? And can't you get the source code at sourceforge? Presumably looking at the way they did it would be best?

Yes, Fenix was programmed using C. In its case they don't implement a frame in C they implement it in their own language, Fenix. Fenix is an interpretted language so it's easy for the programmers to jump around the stack and restore processes. They just point the stack pointer wherever they want and it starts executing that code. All processes in Fenix are kept on the stack all the time. However I'm going by my knowledge of Div here and assuming Fenix works the same way.

So the way it is implemented in Fenix is not really feasable unless you create your own C++ compiler/interpretter.
____________
I see 57,005 people.
#
October 1, 2006, 20:09
OScoder
None
1338 posts
Quote:
The first idea is ill advised due to the ability to randomly jump from one bit of code to any other bit which would make debuging a nightmare.

Ok. Maybe using some seperate (not inline) assembly could be helpfull?

Like:
Code:
 _frame:
  pop eax ;pop the return address (where the last proc left off)
  mov [proc1_ptr], eax ;store it
  mov eax, [proc2_ptr] ;get the pointer for proc 2
  push eax ;put it onto the stack (to be popped off by the ret)
 ret

Not quite functional - but gives the general idea. The return addresses could be put in a linked list.

[Edited on October 1, 2006 by OScoder]
____________
om
#
October 1, 2006, 21:37
PEader
お前はもう死んでいる
1486 posts

Quoting OScoder:
Like:
Code:
 _frame:
  pop eax ;pop the return address (where the last proc left off)
  mov [proc1_ptr], eax ;store it
  mov eax, [proc2_ptr] ;get the pointer for proc 2
  push eax ;put it onto the stack (to be popped off by the ret)
 ret

Not quite functional - but gives the general idea. The return addresses could be put in a linked list.

My asm was never great and is a bit rusty to say the least but I believe you would need to store and restore the stack for each function you are jumping into rather then just the code pointer(?).
____________
I see 57,005 people.
#
October 1, 2006, 22:53
OScoder
None
1338 posts
Quote:
I believe you would need to store and restore the stack for each function you are jumping into rather then just the code pointer(?).

Um. Yes.

well that screws that up!

What level of stack manipulation can userlevel programs do?
____________
om
#
October 1, 2006, 23:08
PEader
お前はもう死んでいる
1486 posts

Quoting OScoder:
Quote:
I believe you would need to store and restore the stack for each function you are jumping into rather then just the code pointer(?).
Um. Yes.

well that screws that up!

What level of stack manipulation can userlevel programs do?

Read the article about coroutines. He stores and restores the stack in that.
____________
I see 57,005 people.
#

Message Board > C/C++ > simulating the DIV's frame() thing in c++

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!