The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: jckl on April 06, 2006, 04:10:13 AM

Title: unsigned long to long
Post by: jckl on April 06, 2006, 04:10:13 AM
I been searching how to convert an unsigned long to long but cant seem to get it working... Can anyone get me started or know how to do it?
Title: Re: unsigned long to long
Post by: jckl on April 06, 2006, 04:14:32 AM
nevermind i no longer need this..
Title: Re: unsigned long to long
Post by: hutch-- on April 06, 2006, 04:23:30 AM
Jusat to add some confusion, there is no conversion needed when the value is in either a register or a memory operand as the storage of the value is neither signed or unsigned. What makes the difference is how you evaluate the value, you have signed or unsigned jumps, signed or unsigned extension and so on that draw the distinction for you.

About the only time in MASM that you need to make a pseudo high level data type is if you are using the high level syntax where it need to know the difference between a signed or unsigned value. In these contexts you use stuff like,

.if SDWORD PTR myvar == -1
  etc ....
Title: Re: unsigned long to long
Post by: jckl on April 06, 2006, 04:50:25 AM
well like if i read 4 bytes from a file that in hex comes out as 7D 37 59 FE it will equal -27707523 as a signed long but equals 4267259773 as unsigned long..

first i was using this to convert to ascii and it was only coming up with the unsigned value.

    div Base
    add dl, 30h
Title: Re: unsigned long to long
Post by: Mincho Georgiev on April 06, 2006, 02:43:55 PM

                           mov eax,0FE59377dh
   cdq


Will give you -1 in edx. It means that you have negative value in eax!

Title: Re: unsigned long to long
Post by: Mark Jones on April 06, 2006, 03:28:35 PM
If this is useful, the sign bit can be "toggled":


    mov eax,0FE59377Dh
    and eax,07FFFFFFFh    ; strip off sign bit

    mov eax,07F59377Dh
    or  eax,080000000h    ; tack on sign bit

    and eax,080000000h
    cmp eax,080000000h
    jz SignBitSet         ; test sign bit
Title: Re: unsigned long to long
Post by: daydreamer on April 06, 2006, 04:40:34 PM
Quote from: Mark Jones on April 06, 2006, 03:28:35 PM
If this is useful, the sign bit can be "toggled":


    mov eax,0FE59377Dh
    and eax,07FFFFFFFh    ; strip off sign bit

    mov eax,07F59377Dh
    or  eax,080000000h    ; tack on sign bit

    and eax,080000000h
    cmp eax,080000000h
    jz SignBitSet         ; test sign bit

no it doesnt work that way, you need to 2complement(NEG) it to change sign, I already checked because I wanted to find a way to switch sign quickly in parallel code, with AND
instead I have to perform NOT followed by add 1 = NEG
and instead mask FF and 01, with pand and result from a packed comparision
Title: Re: unsigned long to long
Post by: jckl on April 06, 2006, 11:17:01 PM
i used neg to change sign also.
Title: Re: unsigned long to long
Post by: Ratch on April 07, 2006, 12:41:26 AM
 jckl,

Quote....unsigned long to long....

     Since this is the MASM forum, you should use MASM terminology, not that of C,C++.  The correct word is DWORD.

Quotei used neg to change sign also.

     NEG will not change the sign of zero. 

Mark Jones,

Quote    and eax,080000000h
    cmp eax,080000000h
    jz SignBitSet         ; test sign bit

     The following is more direct and does not change EAX.

  TEST EAX,EAX       ;if needed
  JZ xxxx                  ;test for zero if interested, because zero is neither positive or negative
  JS/JNS yyyy           ;test for sign/no sign

To all Interesteds,
     If all you want to do is toggle the sign, use XOR EAX,080000000H.  The result will not be the number multiplied by -1.  If you want to multiply a 1's complement number by -1, use NOT EAX.  This will flip all the bits of the number. If you want to multiply a 2's complement number (INTEL uses 2's complement)  by -1, use NEG EAX.  This will flip all the bits and add 1.  Ratch

     
Title: Re: unsigned long to long
Post by: Mark Jones on April 07, 2006, 12:51:55 AM
Thanks Ratch.
Title: Re: unsigned long to long
Post by: jckl on April 07, 2006, 10:17:23 PM
If there is no sign i skip the converion and if its a zero i dont run that on it.