Register most likely not to be changed by calling apis?

Started by ThoughtCriminal, August 26, 2005, 05:48:42 PM

Previous topic - Next topic

ThoughtCriminal

I'm looking for regs that I can be sure wont be changed after making calls to apis like this:

invoke [reg]+ExitProcess

Where:

ExitProcess = 8 ;Or what ever dword offset it is in my vtable.

I understand that VC likes to use ecx for this  purpose.

I don't use stack frames so ebp is good.  I could use one more reg that is unliky to change.

Thanks.




comrade

you can use ebx esi edi, they are guaranteed not to be changed

all others, you cannot guarantee, and they will probably be change
"unlikely" to be changed is not a viable solution

don't be obsessed about squeezing out every little byte

ThoughtCriminal

Thanks.

This is not about sqezing out every little byte but reducing dependcies.  I want to do a large project with many souce files.  So passing pointers to vtables reduces the amount of externdefs I need to do:

;This data section is in the same souce file as the functions _FetchApi etc.

_DATA SEGMENT
align 16
Initilization_func:
dd _FetchApi
dd _ParseCmdLine
dd _LoadSourceFile
_DATA ENDS

My file for externdefs:

externdef Initilization_func:NEAR

Equates for offsets:

FetchApi = 0
ParseCmdLine = 4
LoadSourceFile = 8

Some code in my main file:

mov ebp,offset Initilization_func

call dword ptr[ebp]+FetchApi

mov ebx,offset api

call dword ptr[ebp]+ParseCmdLine

call dword ptr[ebp]+LoadSourceFile


I find externdefing a pointer to a table easier than externdefing every thing.


comrade

Have you heard of import tables, import libraries, and linkers?

hutch--

ThoughtCriminal,

Just observe the normal Windows regsiter preservation rules for all of your code and you will not hae a problem. The OS expects EBX ESP EBP ESI EDI to be returned with the same values as they had when the function was called, you can trash EAX ECD EDX but so can enything else so you have a reciprocal position where you should preserve those values if you call another procedure or API that can trash them if you have values in them.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ThoughtCriminal

Thanks Hutch--

comrade

I do not use MASM32 and Windows.inc.  I do my API imports like this:

   externdef   _imp__GetProcAddress@8:NEAR
   externdef   _imp__LoadLibraryA@4:NEAR

   GetProcAddress TEXTEQU  <FP@8 PTR _imp__GetProcAddress@8>
   LoadLibrary TEXTEQU      <FP@4 PTR _imp__LoadLibraryA@4>

So I guess I know about import tables.

Most of the code is not far enouge along to be put into libs.   When parts get that far a I plan to.

My currrent code links fine using the pointer method for Apis and my own functions and data.

I like to only Include what I need.  I'm happy using a pointer to data and functions to reduce external dependancies.  I'm also finding the pointer method helps me better organize my code.

If you see a weakness, like perhaps my method wont work well with libs, please let me know.

Thanks.