News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How can save the ecx before the call ?

Started by seasea, April 26, 2018, 12:57:09 AM

Previous topic - Next topic

seasea

Hello, I want use the loop because it will dec ecx automaticly , but ,  the push/pop ecx before the call,  crash....
for example:

mov rcx 10
LOOP_BEGIN:
    push rcx
    invoke funcitonXXX
    pop rcx

loop LOOP_BEGIN

I'm confused about the stack...

jj2007

Try pushing two registers, in order to keep the align 16.

Vortex

Hi seasea,

Here is a quick example for you:

option casemap:none

EXTERN printf:PROC
EXTERN ExitProcess:proc

.data

string      db 'This is a test.',13,10,0

.code

main PROC

LOCAL dummy:QWORD
LOCAL counter:QWORD ; aligned variable

    sub     rsp,20h
    mov     rcx,10
    mov     counter,rcx

loop_begin:

    lea     rcx,string
    call    printf

    dec     counter
    jnz     loop_begin

    xor     ecx, ecx
    call    ExitProcess

main ENDP

END

hutch--

Answer is simple, don't use PUSH / POP in 64 bit. It can be done but you risk the problem you have. Use a LOCAL value to preserve the register.

LOCAL reg_ :QWORD

mov reg_, rcx
; do something with ECX
mov rcx, reg_

This completely avoids stack alignment issues.

Mikl__

Hi, seasea
it is the easy    push rcx
    push rcx
    invoke funcitonXXX
    pop rcx
    pop rcx

jj2007

It is that easy in general, but I wrote "try pushing" above because of the odd case where you bump into shadow space.

Mikl__

#6
Ciao, jj!
scusami, ma in qualche modo non ho notato la tua risposta ...

seasea

Sorry, I forgot it, the stack frame is aligned to a 16 byte. :redface:

Thank you  very much, jj2007, Vortex, hutch--, and others. :t :icon14: