News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Why to push into the stack?

Started by mikichi, January 03, 2017, 08:00:34 AM

Previous topic - Next topic

mikichi

Hello,
I'm newbie in asm. I'm try to learn from the tutorial folder from masm32 SDK, and in tutorial/console/demo6/address.asm have this lines:

    LOCAL cnt   :DWORD          ; allocate a loop counter

    push esi
    push edi

    mov cnt, 10                 ; set the number of loop iterations
    mov esi, array              ; put array address into ESI

When I'm trying to comment the push statements the program run perfectly without them, so why we need to write it?
Thanks,
Michael

jj2007

Hi Michael,

First of all, welcome to the forum :icon14:

Pushing esi and edi on the stack keeps their values for other procedures, including those of Windows itself. More here in the section The "register gets trashed" trap

Sometimes the program works without push esi + pop esi, but it just means you were lucky - you cannot rely on this behaviour. The rules are clear...

mikichi

Thanks!
P.s: why I', always should answer those questions?  Type the letters shown in the picture:,  Was Musolini German, Italian, Russian or Chinese ?:,  If English is one of the 2 main languages in Canada, what is the other ?: etc.. LOL

ragdog

Hi

The other way is but, same effect

main proc  uses esi edi

After compile it have you again

push esi
push edi




0040100C  /$  55            PUSH EBP
0040100D  |.  8BEC          MOV EBP,ESP
0040100F  |.  83C4 FC       ADD ESP,-4
00401012  |.  56            PUSH ESI                                      <<<<<<<<<<
00401013  |.  57            PUSH EDI                                      <<<<<<<<<<
00401014  |.  C745 FC 0A000>MOV [LOCAL.1],0A



jj2007

Quote from: mikichi on January 03, 2017, 08:47:10 AMWas Musolini German, Italian, Russian or Chinese ?:,  If English is one of the 2 main languages in Canada, what is the other ?: etc.. LOL

That will stop after a few posts, don't worry :biggrin:

main proc  uses esi edi

What ragdog means is: With this shorter notation, Masm will insert the push/pop to preserve the registers. Easy and convenient. You will often see sometest proc uses esi edi ebx arg1, arg2

mikichi

Very informative! thank you again guys!