im beginning... asm

Started by
9 comments, last by Matt_D 14 years, 11 months ago
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]
Bring more Pain
Advertisement
i will give an example.


you have the c++ class without virtual methods

//My Game Class in cppclass 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 asmGame struct;no vtable so dont include one;here are the variblesscore dd 0life  dd 4Game ends;notice non virtual methods are defined out side the struct;methodsgetScore@Game proc $this:dword;;insert some codegetScore@Game endpgetLife@Game proc $this:dword;;insert some codegetLife@Game endp


this is right? Right?

[edited"for got the this pointer"]
Bring more Pain
is asm a little too hard for you guys or is it confusing?
Bring more Pain
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.
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.
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
Bring more Pain
thx for the feed back
Bring more Pain
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.
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
Bring more Pain
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.

This topic is closed to new replies.

Advertisement