The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ThoughtCriminal on August 26, 2005, 05:48:42 PM

Title: Register most likely not to be changed by calling apis?
Post by: ThoughtCriminal on August 26, 2005, 05:48:42 PM
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.



Title: Re: Register most likely not to be changed by calling apis?
Post by: comrade on August 26, 2005, 05:59:08 PM
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
Title: Re: Register most likely not to be changed by calling apis?
Post by: ThoughtCriminal on August 26, 2005, 06:28:46 PM
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.

Title: Re: Register most likely not to be changed by calling apis?
Post by: comrade on August 26, 2005, 07:44:10 PM
Have you heard of import tables, import libraries, and linkers?
Title: Re: Register most likely not to be changed by calling apis?
Post by: hutch-- on August 27, 2005, 04:17:08 AM
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.
Title: Re: Register most likely not to be changed by calling apis?
Post by: ThoughtCriminal on August 27, 2005, 05:12:20 AM
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.