The MASM Forum

General => The Campus => Topic started by: mikichi on January 03, 2017, 08:00:34 AM

Title: Why to push into the stack?
Post by: mikichi on January 03, 2017, 08:00:34 AM
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
Title: Re: Why to push into the stack?
Post by: jj2007 on January 03, 2017, 08:37:04 AM
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 (http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm) 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...
Title: Re: Why to push into the stack?
Post by: mikichi on January 03, 2017, 08:47:10 AM
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
Title: Re: Why to push into the stack?
Post by: ragdog on January 03, 2017, 08:49:53 AM
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


Title: Re: Why to push into the stack?
Post by: jj2007 on January 03, 2017, 09:01:26 AM
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
Title: Re: Why to push into the stack?
Post by: mikichi on January 03, 2017, 09:10:35 AM
Very informative! thank you again guys!