flat assembler
Message board for the users of flat assembler.

Index > Main > How does OOP concept works at assembly level?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 15 Jul 2010, 13:06
?

_________________
Sorry if bad english.
Post 15 Jul 2010, 13:06
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 15 Jul 2010, 13:33
from what i understand, objects are defined with data structures.
and calling an object is like calling a method, using the object pointer as parameter, and work on datas composing the object.
Post 15 Jul 2010, 13:33
View user's profile Send private message Visit poster's website Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko 15 Jul 2010, 13:40
Our lecturer said: "Control's properties are nob-hiden metods".
Post 15 Jul 2010, 13:40
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 15 Jul 2010, 13:41
you have set of data structure declarations ("classes" in OOPspeak), and you have set of procedures ("methods") that work with these structures. Procedure always gets a pointer to structure in memory that it works with (instance of structure declaration, "class instance" or "object" in OOP) as an argument. Point is that these procedures never touch anything else besides the class they work with and supplied arguments. You try to create a procedure for anything that ever is done with the structure, and never touch its data members directly outside these procedures.

This way, you have a very clean interface to work with your data structures, with minimal side effects. There are some extra useful things this provides, but that is not so important now.

Also see: http://x86asm.net/articles/oop-from-low-level-perspective/index.html (But note l am no longer happy with approach taken in this article, and it is unfinished)

edit: link fixed


Last edited by vid on 15 Jul 2010, 14:22; edited 1 time in total
Post 15 Jul 2010, 13:41
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko 15 Jul 2010, 13:44
i remember myself so happy i could change constant variable with a built-in-asm
though delphi allowed doing it too
oop are the blocks you can sharpen with asm

<vid>, dead link
Post 15 Jul 2010, 13:44
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 15 Jul 2010, 13:46
edemko wrote:
... i could change constant variable ...
Which is it, constant or variable? Can't be both. Wink
Post 15 Jul 2010, 13:46
View user's profile Send private message Visit poster's website Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko 15 Jul 2010, 13:49
sure, missed
variable constant(fickle HLL Smile as it can be changed Smile
Post 15 Jul 2010, 13:49
View user's profile Send private message Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko 15 Jul 2010, 13:50
asm is stable, hll is fickle

EDIT:
once Borland mentioned Objects were the hill
now Bor-land is dead
Bor, by the way, means pine forest
Post 15 Jul 2010, 13:50
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 15 Jul 2010, 20:00
And it gets really gross once you start using virtual tables (yuk)
Post 15 Jul 2010, 20:00
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 15 Jul 2010, 22:43
thanks guys.
Post 15 Jul 2010, 22:43
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 16 Jul 2010, 02:38
gcc -S -masm=intel -o whateverexe whatevercpp.cpp
Post 16 Jul 2010, 02:38
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 28 May 2011, 14:17
@Tyler, nice tip Very Happy

i'm doing some tests using OOP (based on vid's link).

Is my code below an inheritance example?

Code:
format PE CONSOLE
include 'win32a.inc'
entry _start
section '.code' code readable executable 

_start:
        mov     esi,ENEMY       ; you can change to HERO
        mov     ebx,[esi]       ; load .type (CHARACTER)
        call    dword[ebx+4]    ; call char_walk(+4) or char_attack(+0)

        invoke  ExitProcess,0 


msg_walk db '%s walking',0
char_walk:
        lea     ebx,[esi+4]
        cinvoke printf,msg_walk,ebx
        ret

msg_attack db '%s attacking',0
char_attack:
        lea     ebx,[esi+4]
        cinvoke printf,msg_attack,ebx
        ret



section '.data' data readable writeable 

CHARACTER:
        .attack dd char_attack
        .walk   dd char_walk

HERO:
        .type   dd CHARACTER
        .name   db 'hero',0
ENEMY:
        .type   dd CHARACTER
        .name   db 'enemy',0



section '.idata' import data readable 

        library kernel32,'kernel32.dll', msvcrt,'MSVCRT.DLL'

        import msvcrt,printf,'printf',scanf,'scanf'
        import kernel32,ExitProcess,'ExitProcess'    


Lets say i will need a N# of enemies. How do i create them? (equivalent to new ENEMY() in high-level)

_________________
Sorry if bad english.
Post 28 May 2011, 14:17
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 28 May 2011, 17:48
You get(allocate/static store/wtf else) N ENEMY structures and call the ENEMY constructor (which you don't have, nor need, for now).
Post 28 May 2011, 17:48
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 28 May 2011, 17:53
I prefer C, if you don't mind(IMO, it's more efficient for examples and usually trivial to translate):
Code:
struct ENEMY{};

enemy_constructor(ENEMY *e)
{
     // initialize e
}

int main()
{
     ENEMY enemies[] = malloc(sizeof(ENEMY * N));
     for(int i = 0;i < N;i++)
          enemy_constructor(&enemies[i]);
     // you now have N enemies
     return 0;
}    
Post 28 May 2011, 17:53
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 28 May 2011, 18:25
okay, i can understand that, but i don't know how to translate to asm.
Post 28 May 2011, 18:25
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 28 May 2011, 18:39
Code:
ENEMY:
    .size = $-ENEMY

enemy_constructor:
    ret 4

_main:
    sub esp,ENEMY.size * N
    mov ecx, N
@@: push [esp+ENEMY.size*ecx]
    call enemy_constructor
    dec ecx
    jnz @b
    ret    


maybe something like that? (i didnt compile it)

but i have trouble when working with dynamic memory (malloc?), i mean, how to manage it. ( PS: how das malloc works? )

I think the most difficult i have is understand the dynamic/static memory, i mean, not teoricaly, but at practic. So i get confused when need to do arrays of unknow-size data.

_________________
Sorry if bad english.
Post 28 May 2011, 18:39
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 28 May 2011, 19:03
I suggest to practice with malloc / free first, and only then move on to OOP.
Post 28 May 2011, 19:03
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 28 May 2011, 19:05
in asm you mean? how can i do that?
Post 28 May 2011, 19:05
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 28 May 2011, 19:39
Doesn't matter. If you don't wish to use malloc() from libc, you can call directly system API (eg. HeapAlloc in Windows, etc.). Without understanding dynamic memory well, you really shouldn't approach OOP.
Post 28 May 2011, 19:39
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 28 May 2011, 19:46
well, i know that functions return a handle to the first memory place. And then i need to fill that memory space with my structures. But what if there is no more memory there anymore, i will need to realloc more and copy all the last data to the new handle, or i keep tha last buffer but use the new one?
Post 28 May 2011, 19:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.