The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Alloy on April 23, 2005, 02:58:04 AM

Title: The smallest way to un-zero a register safely?
Post by: Alloy on April 23, 2005, 02:58:04 AM
 I pondered this out of curiosity too much so I'll go ahead and ask for any other ideas. The best I've thought up was to copy the stack pointer such as  mov eax,esp.

Title: Re: The smallest way to un-zero a register safely?
Post by: sluggy on April 23, 2005, 03:35:44 AM
How about this:

   stc
   setc al


And of course there is always mov al, 1.
Title: Re: The smallest way to un-zero a register safely?
Post by: GregL on April 23, 2005, 04:52:54 AM
How about:

or eax, 1

Title: Re: The smallest way to un-zero a register safely?
Post by: hutch-- on April 23, 2005, 05:19:30 AM
lahf

I am not sure if there is a condition that will have all the flags zero at one time.

If you have not cleared EBX, try XLATB
Title: Re: The smallest way to un-zero a register safely?
Post by: MichaelW on April 23, 2005, 05:32:30 AM
If you know the register is zero, you could use the 1-byte instruction inc reg.

Title: Re: The smallest way to un-zero a register safely?
Post by: hutch-- on April 23, 2005, 05:43:37 AM
I think lahf is safe.

            xor eax, eax  ; clear eax
            sahf           ; load AX into flags
            lahf            ; load flags into AX

            fn MessageBox,hWnd,str$(eax),"Title",MB_OK
Title: Re: The smallest way to un-zero a register safely?
Post by: Alloy on April 23, 2005, 08:36:57 AM
Thanks for the replies. My goal originally was to find the smallest way to unzero a register in sort of the opposite to how xor eax,eax zero's a register. Obviously it would be some sort of operation that involves registers only and since the stack pointer is rarely ever zero it seemed to make the most sense to use.
Title: Re: The smallest way to un-zero a register safely?
Post by: roticv on April 23, 2005, 08:39:03 AM
Read the xor part of http://www.win32asmcommunity.net/phpwiki/index.php/BitOperations
Title: Re: The smallest way to un-zero a register safely?
Post by: hutch-- on April 23, 2005, 09:08:11 AM
lahf is 1 byte which is half of 2 bytes.
Title: Re: The smallest way to un-zero a register safely?
Post by: raymond on April 23, 2005, 03:21:59 PM

Quotelahf is 1 byte which is half of 2 bytes.

But it can only work on the EAX (AH) register. You can't use it for un-zeroing other registers.

I always use or  reg,1

Raymond
Title: Re: The smallest way to un-zero a register safely?
Post by: AeroASM on April 23, 2005, 05:34:38 PM
Why would you want to unzero a register?
Title: Re: The smallest way to un-zero a register safely?
Post by: raymond on April 24, 2005, 02:09:37 AM

QuoteWhy would you want to unzero a register?

One example would be to indicate success or failure when returning from a proc. With HLLs, you can generally use only EAX for returning something. In assembly, you can use any register. In some cases, it may be usefull to return a value in one register and the success/failure code in another.

Raymond
Title: Re: The smallest way to un-zero a register safely?
Post by: Alloy on April 24, 2005, 11:05:47 AM
Quote from: AeroASM on April 23, 2005, 05:34:38 PM
Why would you want to unzero a register?

Thanks for the replies.

I use it with the xchg flag, ecx instruction to both unzero a thread synchronization flag and to check if the flag was zero.
Title: Re: The smallest way to un-zero a register safely?
Post by: doomsday on April 24, 2005, 02:54:53 PM
While it's not directly related to the question asked, since we're talking about return values, I feel it may be worth mentioning the SALC (D6h) instruction which sets AL to FFh if the CF is set or AL to 0h if it's not.

regards,
-Brent
Title: Re: The smallest way to un-zero a register safely?
Post by: Tedd on April 25, 2005, 09:36:26 AM
Quote from: Alloy on April 24, 2005, 11:05:47 AM
I use it with the xchg flag, ecx instruction to both unzero a thread synchronization flag and to check if the flag was zero.

Just thought I'd mention the BTS instruction - which is designed for this purpose.