My program is split between two files. File 1 has a call to a funtion in file 2. Once the function ends, the return is back to code defined in file 1.

Now I have a memory defined variable use in file1 and file 2, but the the address of the variable changes when the code rets backs to code in file 1.

I am not getting any missing external errors and such. The variable exists in both file(both objs also), but a diffrent addresses!!! I cut and pasted this stuff together so I missing something about why my variable exists at two diffrent addresses.

example:
file1----------------------------

var (address = 1000)

call func

file2-----------------------------

func


var (address = 2000)
mov var, 4

ret
-----------------------------------

It links properly, but when the program is run the value of var is always diffrent between file 1 and file 2 becuase the address is diffrent in each obj.

Any ideas?
Posted on 2002-06-06 21:25:06 by ThoughtCriminal
ThoughtCriminal,

If I have understood your question properly, you have a variable of the same name in 2 seperate modules and you wish to use the same variable in both modules to transfer data.

With the seperate modules, you do not normally have the scope to do this as the content of one is fully isolated from the other.

I would be tempted to try the older MASM syntax
COMM (Communal variable)
or
PUBLIC

to make the same variable accessible across two or more modules.

Regards,

hutch@movsd.com
Posted on 2002-06-06 21:57:17 by hutch--
hutch--, thanks for the help I'll try that. Let me explain a little better. There are 3 files.

file1.asm
file2.asm
var.inc <---var is defined here ie. var DWORD 0.

var. inc is included in both file1.asm and file2.asm. So when I assemble, I get file1.obj and file2.obj. Link, and get an exe where the variable named var exists in 2 places(2 differnt addressses depending on what obj it is referenced in).

I was wondering if I am missing a linker flag or something to resolve same named variables, in diffrent objs, to the same address?
Posted on 2002-06-06 22:43:29 by ThoughtCriminal
With the link options that display with the linker from the win98ddk, there is no option to specify a PUBLIC or COMMON variable, its something you must do in your code.

Regards,

hutch@movsd.com
Posted on 2002-06-07 02:46:19 by hutch--
ThoughtCriminal,

I remember having this problem. It seems you can declare the same variable name in every module you have if you want, as long as you don't make one of them public, the assembler makes a separate one for each module. This is as it should be, I guess, to preserve black box opacity.

You can't declare a variable in an inc file, or it gets declared in every module that includes that file. However, you CAN declare a variable EXTRN in an inc file. Then every module that includes that file can import the address. You also have to actually declare it and define it in ONE module, and then declare it public in that module.

Here's how:

Wumpus.inc:

extrn NumberOfBatsInTheCave: dword

Wumpus1.asm:

public NumberOfBatsInTheCave

NumberOfBatsInTheCave dd 0


If Both Wumpus1.asm and Wumpus2.asm include Wumpus.inc, you don't even have to worry about declaring it in Wumpus2.asm. But you can use it in either module.
Posted on 2002-06-07 15:34:03 by Jimmus
I've always been sucessful by splitting my work like so: Code goes into .asm files. Definitions go into .inc files.

To get all code modules to use the same variable, I'll define it in one asm as externdef in addition to the normal declare.

Inside the inc file is all the info I want 'public.' Users of this info get it by including that .inc file.

If I have 'friend' modules, they share another .inc file with the family information.
Posted on 2002-06-07 20:27:01 by Ernie
w00t!! I found a simple way to take care of this problem.

Now my object files can still be compiled with the same-variable at different addresses, yet be at the SAME address once linked!!

And I get to do one of my favorite things to do when things dont fit right!!

/FORCE it!!!

now when I:

EXTERNDEF foo:dword

I don't get:

error 'foo' defined already defined in module 'bar'

Of coure I'll have to EXTERNDEF all my globals, but it is worth it.
Posted on 2002-06-20 00:13:55 by ThoughtCriminal
TC, "FORCE" is a pretty ugly thing to rely on... if you construct your
code the way jimmus says, you should be able to avoid such an ugly hack.
Posted on 2002-06-20 09:25:21 by f0dder
TC, "FORCE" is a pretty ugly thing to rely on...


So I should not use the /FORCE because it will turn me to the dark side? :tongue:

I knew you would feel /FORCEd to reply to this one f0dder. :grin:

I program weird, and I walk funny(don't look funny though).

/FORCE seems to have effectively made and variable I EXTERNDEF a global variable. The dangerous this is that any variable I do not EXTERNDEF and is used in more than 1 file, will either change address or have its contents trash as the code moves between source files. Any variable used in more that 1 file should be EXTERNDEFed for this reason. As long as you know where the code will tranfer between souce files is, this is easy to debug.

All these variables are defined on heap, but the obj files seem to act like each has their own heap. But at link time, the heap becomes a global concept that all the obj files share. I dont like having to litter my asm files with PUBLIC and EXTERN directives. What if you need to do that for a 100 variables?? PUBLIC and EXTERN seem to be there to make the assembler happy. The .data sections in reality should be global to all object files IMO, because thats the way it is in the final exe. The linker with /FORCE does this, I have not found a way with the assembler to do this easily, without a lot of overhead.

In other words:

I feel that every variable delared in .data, should be a global variable.

Now how's that for weird :alright:
Posted on 2002-06-20 22:40:55 by ThoughtCriminal