News:

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

Can an address be negative?

Started by jj2007, September 02, 2008, 12:55:26 PM

Previous topic - Next topic

jj2007

This is probably a newbie question: Is there any chance that the address of a variable is negative?

- a SysAlloc string seems to be typically in the 00164000 range
- a LOCAL variable is in the 13FF range
- a global variable is in the 404000 range

That's all pretty far from 80000000h, so would it be correct to say that addresses used by Windows can't be negative? If not, what are the exceptions?

Background is if I could pass
- a negative parameter to tell my proc "go and get one or more addresses from table XX"
- a positive parameter to tell my proc "this is the one and only address"

Thanks in advance.

zooba

Addresses are unsigned, so no, the address of a variable will never be negative.

It may, however, have the topmost bit set. This will look like a negative number if treated as twos-complement (or ones, for that matter, or sign-magnitude). These addresses are generally reserved by the operating system (I say generally because 64-bit changes this, as does a certain 3GB boot option).

You would be better off guaranteeing that actual addresses are DWORD aligned, which will give you the 2 least significant bits to play with.

Cheers,

Zooba :U

hutch--

JJ,

If the address is above 2 gig which is possible with OS DLLs for example AND you test it as a signed number, then YES you can get negative addresses but you are safer dealing with addresses as unsigned 32 bit integers "DWORD" and using unsigned comparisons with them.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

OK, it means I cannot use this mechanism. Thanks to both of you :U

Rainstorm

zooba wrote..
QuoteYou would be better off guaranteeing that actual addresses are DWORD aligned, which will give you the 2 least significant bits to play with.
how could you play with them.., or how could you use that to your advantage ?


jj2007

Quote from: Rainstorm on September 02, 2008, 03:08:47 PM
zooba wrote..
QuoteYou would be better off guaranteeing that actual addresses are DWORD aligned, which will give you the 2 least significant bits to play with.
how could you play with them.., or how could you use that to your advantage ?

I had intended to use

aFlag=-1 (or any other number with bit 31 set)
invoke MyFunc, aFlag
vs
invoke MyFunc, addr MyString

If it could be ensured that the programmer never passes odd addresses, then Bit 0 could be used as a flag, instead of bit 31. But that's not realistic - few people would clutter their data sections with align 4 statements between all strings.

PBrennick

JJ,
The least significant bit are what everyone is already using, bit 1=0 (FALSE or EVEN), bit 1=1 (TRUE or ODD). If you mask all the other bits you will get this.

-- Paul
The GeneSys Project is available from:
The Repository or My crappy website