flat assembler
Message board for the users of flat assembler.

Index > Windows > do i need to adjust the stack here?

Author
Thread Post new topic Reply to topic
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible 05 Nov 2010, 21:07
ok so i have this function that i toy with from time to time to learn assembly. i i want to stop using the 'proc' macro. if i do this do i need to adjust the stack after i call it?

here is the function:
Code:
SeviePrimesUpTo:
     mov ebp,esp
     label upto dword at ebp + 4
     label @arg2 dword at ebp + 8
     sub esp,12
     label p dword at ebp - 4
     label i dword at ebp - 8
     label List dword at ebp - 12

     mov [p],2
     mov [i],0
     mov [List],0

     push [upto]
     call [malloc]
     mov [List],eax
     add esp, 4        ;<-- here what dose this do? why is it here?

     initFor:
        mov eax,[i]
        cmp eax,[upto]
        jnl endFor

            mov eax, [List]
            mov edx, [i]
            mov byte[eax+edx-1],1

        inc [i]
        jmp initFor
     endFor:

     initMain:

        mov eax,[p]
        imul eax,eax
        cmp eax,[upto]

        jnle endMain

             mov eax,[List]
             mov edx,[p]
             cmp byte[eax+edx-1], 0
             je endIfMain

                 mov eax,[p]
                 imul eax,eax
                 mov [i],eax

                 initForMain:
                      mov eax,[i]
                      cmp eax,[upto]

                      jnle endForMain

                          mov eax,[List]
                          mov edx,[i]
                          mov byte[eax+edx-1],0

                      mov eax,[i]
                      add eax,[p]
                      mov [i],eax
                      jmp initForMain

                   endForMain:

             endIfMain:

          inc [p]
          jmp initMain
     endMain:

     mov [i],2
     initPrint:
        mov eax,[i]
        cmp eax,[upto]
            jnl endPrint

                mov eax,[List]
                mov edx,[i]
                cmp byte[eax+edx-1],0

                je endPrintIf

                     push [i]
                     push isprime
                     call[printf]
                     add esp,8

                endPrintIf:

            inc [i]
            jmp initPrint
     endPrint:


     push [List]
     call [free]
     add esp,4
     push [@arg2]
     push isprime
     call[printf]
     add esp,8
     add esp,12
     ret 
    


the object of this function is to take 'upto' as a parameter and print a list of all the prime numbers upto that number. i added in the @arg2 thing to test if i understood how to get the arguments from a function correctly. what im not sure about is if i have to adjust the stack after i call this function. i assume i do because i made no effort to account for where those numbers would be after i call it.

edit: this is kinda secondary but how do i make a function pointer? do i just use a label as a variable and use "lea some_pointer,[some_label]"
Post 05 Nov 2010, 21:07
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 06 Nov 2010, 00:42
Well you didn't show all the code so I have to assume you are using c-call functions, right? If so, then all your stack adjustments look fine to me.

And to get a pointer to a function simply:
Code:
mov eax,some_label    
Post 06 Nov 2010, 00:42
View user's profile Send private message Visit poster's website Reply with quote
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible 06 Nov 2010, 03:46
no i don't mean the the function calls in this function but would i have to adjust the stack after calling the function i showed you. i did however figure out that i do need to do that. i was pretty sure that it would leave the arguments on the stack so i checked it by calling printf without passing the argument and it printed the values i had just previously passed. thanks anyway however Smile i have a confident understanding of functions now.
Post 06 Nov 2010, 03:46
View user's profile Send private message Reply with quote
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible 06 Nov 2010, 03:53
i do have a question about cdecl calling convection however, i would like everything to follow one single convention but i'm not sure what specifically the cdecl calling convention intells. i looked on Wikipedia but only found conventions dword and floating point. what if the return type is larger than 4 bytes? what then? i always assumed that the best way to get the return value would just be to pop the value to where you want to return it.
Post 06 Nov 2010, 03:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 06 Nov 2010, 03:59
The difference between them:

stdcall: callee pops arguments off the stack (eg. ret 4)

ccall: caller pops arguments off the stack (eg. ret)

Other than that everything else is the same.
Post 06 Nov 2010, 03:59
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 06 Nov 2010, 04:03
ishkabible wrote:
no i don't mean the the function calls in this function but would i have to adjust the stack after calling the function i showed you.
Yes, that is what I answered, you have properly adjusted the stack after each call. But that is only assuming you are using c-call functions/library, you had not stated whether that was the case here.
Post 06 Nov 2010, 04:03
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 06 Nov 2010, 09:22
ishkabible,

Agner Fog compiled various sources in one document about calling conventions, you may find it useful (along with optimization manuals there Wink).
Post 06 Nov 2010, 09:22
View user's profile Send private message Reply with quote
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible 07 Nov 2010, 01:46
i have a question now again when the function returns ebp is set to a place in the stack that it is not supposed to be, do i need to use another register to store the previous value of ebp and move it back at the end of the function or is there a better way to handle this?
Post 07 Nov 2010, 01:46
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 07 Nov 2010, 02:20
Push ebp onto the stack. Use the stack Luke.
Post 07 Nov 2010, 02:20
View user's profile Send private message Visit poster's website 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.