flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > baby step for object oriented programming

Author
Thread Post new topic Reply to topic
taeyun



Joined: 12 Jan 2014
Posts: 42
Location: south korea
taeyun 16 Mar 2014, 07:43
this program imitate very simple object oriented feature, the method calling.
1. there is a struct. (you can call this class if you want)
2. there is a initialize function. (you can call this 'new' or 'instantiate' function)
3. there is a function which takes structure.(you can call this as method which takes 'this' pointer)

I labeled the strucure(class) as 'Point'
I labeled the instantiate function as 'Initialize'
I labeled the method as 'MoveRight'

MoveRight method is just for test. it adds 1 to a variable of class(or structure).

I instantiate first object and call MoveRight once.
then I instantiate second object and call MoveRight 3 times.
lastly, I call printf to check the value.
it (seems) work!


Code:
format PE console

entry start
include 'win32a.inc'

Point:
        dd 0
        dd 0
.size = $-Point

Initialize:
        push ebp
        mov ebp, esp
        sub esp, 40
        ;[ebp+12] = size of struct
        ;[ebp+8] = address of struct
        invoke malloc, dword [ebp+12]
        add esp, 4
        mov [ebp-4], eax ;new address
        mov esi, [ebp+8]
        mov edi, [ebp-4]
        mov ecx, [ebp+12]
        cld
        rep movsb
        mov eax, [ebp-4]
        mov esp,ebp
        pop ebp
        ret

MoveRight:
        push ebp
        mov ebp, esp
        ;[ebp+8] = this ptr
        mov eax, [ebp+8]
        inc dword [eax]
        mov esp, ebp
        pop ebp
        ret


msg: db '%d',10,0
start:
        push ebp
        mov ebp, esp
        sub esp, 40
        ;;;;;;;;;;;;;;;;;first obj
        push Point.size
        push Point
        call Initialize
        add esp, 4
        mov [ebp-4], eax;new object's address

        push eax
        call MoveRight
        add esp, 4
        ;;;;;;;;;;;;;;;;;;;; second obj
        push Point.size
        push Point
        call Initialize
        add esp, 4
        mov [ebp-8], eax;new object's address

        push eax
        call MoveRight
        add esp, 4
        push eax
        call MoveRight
        add esp, 4
        push eax
        call MoveRight
        add esp, 4
        ;;;;;;;;;;;;;;;;;;;;;;;printf to check obj's value
        mov eax, [ebp-4]
        invoke printf,msg,[eax]
        add esp, 8
        mov eax, [ebp-8]
        invoke printf,msg,[eax]
        add esp, 8
        invoke getch

        mov esp, ebp
        pop ebp
        invoke exit

data import
     library msvcrt, 'msvcrt'
     import msvcrt, printf,'printf',exit,'exit',getch,'_getch', malloc,'malloc',free,'free'
end data
    

_________________
influenced by
elements(Euclid)
rules for the direction of the mind(Descartes)
Post 16 Mar 2014, 07:43
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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.