Jump to content
  • Advertisement
Sign in to follow this  
owiley

im beginning... asm

This topic is 3456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i think im beginning to understand how to create classes in assembly. the way im seeing it is that we have structs representing the class and one to represent the vtable of that class. function are of a certain calling convection if it's not virtual else they point to the vtable varible pointer that represents that method right? is there any limits doing it this way? [Edited by - owiley on April 21, 2009 1:08:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
i will give an example.


you have the c++ class without virtual methods



//My Game Class in cpp
class Game
{
// here are the variables
int score;
int life;
// here are the methods
void getScore();
int getLife();
}




now here it is in asm



;My Game Class in asm
Game struct
;no vtable so dont include one
;here are the varibles
score dd 0
life dd 4
Game ends
;notice non virtual methods are defined out side the struct
;methods
getScore@Game proc $this:dword
;;insert some code
getScore@Game endp

getLife@Game proc $this:dword
;;insert some code
getLife@Game endp




this is right? Right?

[edited"for got the this pointer"]

Share this post


Link to post
Share on other sites
Why are you trying to do this? What is your motivation?

If you want to create c++ classes, then use c++.

If you want your asm code to interface with c++ code, then do so. It is quite likely that your assembler will have instructions to help you with that.



The C++ compiler does not create variables the way you do above.

Instead, they will use a block of memory directly, usually by simply adjusting the stack pointer by the size of all variables. In your case, they would be at +0 and +4. That's what your assembler does internally when it compiles the code, invisible to you.

The vtable a block of static data located far away, generally constructed by the compiler and kept as read-only. Again, the compiled c++ will access it by offset rather than by name.


I do not find that asm is hard or confusing. It is very useful to know when debugging your programs. Fortunately you won't be writing code in asm, just reading it after it has been generated.

Share this post


Link to post
Share on other sites
Quote:
Original post by owiley
is asm a little too hard for you guys or is it confusing?


There are many answers. You can use C++ memory model of classes. Or C model. Or Java model. Or your own.

Quote:
the way im seeing it is that we have structs representing the class and one to represent the vtable of that class.


That depends on language, compiler and platform. C++ is something like this (it varies):
struct Class {
static FUNCTIONS vtable[n];
int x;
int y;
};
So first member of a virtual class has a pointer to static array of function pointers.

Quote:
function are of a certain calling convection


In C++, functions are just pieces of code lying around, regardless of whether they are member or free functions. Calling conventions is a contract between caller on how to pass parameters (registers, stack, ...). They are not separate constructs.

Quote:
they point to the vtable varible pointer that represents that method right


In C++, non-virtual classes do not have a vtable. Linker takes care of replacing symbols with actual function addresses.

Quote:
is there any limits doing it this way?

Define limits.

But as far as assembly is concerned, even if generated from C++, there are no higher level constructs. You have memory and stack - then it's up to code to determine what means what, and meaning may depend on current state of application.

Byte-code (Java, C#) languages however have strict structure, which defines classes, methods, functions and members upfront, and they can be manipulated in a consistent manner. These can be seen as output of C/C++ compiler before linking.

Share this post


Link to post
Share on other sites
doing compiler study for my own scripting language im not going to do it that way but it was some thing i was thinking about. i was kinda curios

Share this post


Link to post
Share on other sites
You are creating a compiled scripting language? I'd like to hear more about it. Its an interesting exercise in it's own right, even if you have no other reason behind doing so.

Share this post


Link to post
Share on other sites
from most the scripting language out there they all seem to take the from of c not c++ so i started think about how c++ classes was implemented in asm. I which know a small amount started to look around for writing of asm classes. then i came across asm and oop which lead me to my anwser which is just to feed my George.

and nope im not doing compiled scripts was just curious about how it was done. Im doing semi-compiled

Share this post


Link to post
Share on other sites
Quote:
Original post by owiley
is asm a little too hard for you guys or is it confusing?

I actually programmed in ASM for about 5 years before graphics cards actually did "graphics" so I don't consider it too hard. However with the current technology I do consider it a waste of time, unless you are trying to get some extra performance out of a problem area of your code. What's worse your code becomes non-portable and may not take advantage of advances in the next generation of processor technology. Your code may actually be slower in some cases. Some compilers are pretty good at optimization.

On the other hand knowing ASM gives you some insight on how computers work at a lower level and this can be invaluable in some circumstances. Just don't go hog wild and start writing everything in ASM because someone told you that's what the cool hackers do.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!