News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

The smallest way to un-zero a register safely?

Started by Alloy, April 23, 2005, 02:58:04 AM

Previous topic - Next topic

Alloy

 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.

We all used to be something else. Nature has always recycled.

sluggy

How about this:

   stc
   setc al


And of course there is always mov al, 1.

GregL


hutch--

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
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MichaelW

If you know the register is zero, you could use the 1-byte instruction inc reg.

eschew obfuscation

hutch--

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
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Alloy

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.
We all used to be something else. Nature has always recycled.


hutch--

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

raymond


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
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

AeroASM


raymond


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
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Alloy

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.
We all used to be something else. Nature has always recycled.

doomsday

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

Tedd

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.
No snowflake in an avalanche feels responsible.