开发者

INVOKE MASM saving registers automatically?

开发者 https://www.devze.com 2023-01-25 02:23 出处:网络
Is it possible to save cpu registers automatically when I use the Invoke directive开发者_JAVA技巧 in masm? Look at the example I posted there: selection sort in assembly language

Is it possible to save cpu registers automatically when I use the Invoke directive开发者_JAVA技巧 in masm?


Look at the example I posted there: selection sort in assembly language

The USES directive in the target PROC is what you are looking for.
USES EAX ESI EDI will automatically save those registers upon PROC entry and restore them upon exit (even if you have multiple RET points, and even if having multiple ret points is not recommended). IOW, it will generate PUSHes upon PROC entry and consistently matched (reverse order) POPs before each and every RET. The idea is that since this is assembly, you have full control and responsability upon those registers you modify and want to preserve.
And contrary to what was suggested somewhere else, declaring stdcall doesn't preserve anything automatically for you in MASM. It just determines whether the caller (code generated for INVOKE) or the callee (code generated in the PROC) POPs the parms.


"stdcall" calling convention guarantees that function won't spoil any registers but eax, edx, ecx. If you want to save edx and ecx - write a macro.


Not sure exactly what you meant, and since your tag is masm32 I'll assume Windows x86.

What is perfectly possible is to push all the arguments that an API call requires and then you just call the desired function. What I mean is that when you're promming in assembly for Windows you don't need to use registers to "invoke" the APIs, you have to push the arguments and then call (or invoke) the API.

For example, this:

push 0
push DWORD PTR SS:[EBP+8]
push 0
push 0
push 80000000h
push 80000000h
push 80000000h
push 80000000h
push 0CF0000h
push offset AppName
push offset ClassName
push 0h
call CreateWindowExA

is exactly equal to this (in fact just the values of the parameters are different):

invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
       WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
       CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\
       hInst,NULL

Is that what you meant with your question?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号