flat assembler
Message board for the users of flat assembler.

Index > Main > Make 2 variables share the same address

Author
Thread Post new topic Reply to topic
patulinu



Joined: 21 Jun 2016
Posts: 15
patulinu 16 Jul 2016, 16:36
I'm learning Assembly. All I know is Delphi/Pascal. Before posting this question, I searched on the web (including this forum), but no success.

I'm trying to declare 2 variables (of different types) so that they use the same memory address.
In FASM, I'd like to do something like this:


Code:
 section '.data' data readable writeable
   wcStruc WNDCLASS ?
   wcRaw   db sizeof.WNDCLASS @wcStruc dup (0)    

Obviously, the @wcStruc part is pseudo-code. That's what I'm trying to tell the compiler but I don't know how.
Is that possible? If so, How?
Post 16 Jul 2016, 16:36
View user's profile Send private message Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 16 Jul 2016, 19:15
Code:
section '.data' data readable writeable
   wcStruc WNDCLASS ?
virtual at wcStruc
   wcRaw   db sizeof.WNDCLASS dup 0
end virtual
    

_________________
smaller is better
Post 16 Jul 2016, 19:15
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15
patulinu 16 Jul 2016, 19:25
CandyMan wrote:
Code:
section '.data' data readable writeable
   wcStruc WNDCLASS ?
virtual at wcStruc
   wcRaw   db sizeof.WNDCLASS dup 0
end virtual    

Thank you so much!

Now, just out of curiosity:
Since all I wanted was initialize the entire structure as raw bytes (and not I can), does the code below do the same or it just initializes the first byte (or field)?

Code:
 wc   WNDCLASSEX (0)    
Post 16 Jul 2016, 19:25
View user's profile Send private message Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 16 Jul 2016, 20:07
see fasmw's EXAMPLES directory

Code:
/*
struct WNDCLASS
  style         dd ?
  lpfnWndProc   dd ?
  cbClsExtra    dd ?
  cbWndExtra    dd ?
  hInstance     dd ?
  hIcon         dd ?
  hCursor       dd ?
  hbrBackground dd ?
  lpszMenuName  dd ?
  lpszClassName dd ?
ends
*/
 wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE+1,NULL,_class    

_________________
smaller is better
Post 16 Jul 2016, 20:07
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15
patulinu 16 Jul 2016, 20:31
CandyMan wrote:
see fasmw's EXAMPLES directory
Code:
 ...
 wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE+1,NULL,_class    

Yes, I have already done that. The example above illustrates how to initialize a structure field by field. The "virtual" approach, on the other hand, allows me to pre-initialize to zero all those fields I'm not interested in, making easier the job of setting the required fields (by code, as I prefer).

In Delphi/Pascal I'm used to the FillChar() function, which doesn't care about the type of the variable. Now I know how to do so in FASM. Wink

Thank you again.
Post 16 Jul 2016, 20:31
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 17 Jul 2016, 00:26
patulinu
Quote:
The "virtual" approach, on the other hand, allows me to pre-initialize to zero all those fields I'm not interested in

Firstly, "the virtual approach" in the way shown by CandyMan does not allow you to do anything except for declaring the wcRaw label. The zeroes put into the virtual addressing space have no effect on the content of wcStruc.

Secondly, it doesn't make sense to try to initialize the structure with zeroes explicitly, because this is done implicitly anyway. The only difference is the output file size.

Thirdly, "the virtual approach" would work the other way around, i.e. if you put the wcRaw array first, and then put wcStruc declaration in a virtual section with its base at wcRaw, but a bit simpler way is to forget about wcRaw and just straightforwardly store zeroes:
Code:
wcStruc WNDCLASS
times sizeof.WNDCLASS store byte 0 at wcStruc+(%-1)    

But again, this brings nothing, but a (potentially) increased file size.

_________________
Faith is a superposition of knowledge and fallacy
Post 17 Jul 2016, 00:26
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15
patulinu 17 Jul 2016, 02:19
l_inc wrote:

Firstly, "the virtual approach" in the way shown by CandyMan does not allow you to do anything except for declaring the wcRaw label. The zeroes put into the virtual addressing space have no effect on the content of wcStruc.

Secondly, it doesn't make sense to try to initialize the structure with zeroes explicitly, because this is done implicitly anyway. The only difference is the output file size.

Thirdly, "the virtual approach" would work the other way around, i.e. if you put the wcRaw array first, and then put wcStruc declaration in a virtual section with its base at wcRaw, but a bit simpler way is to forget about wcRaw and just straightforwardly store zeroes:
Code:
wcStruc WNDCLASS
times sizeof.WNDCLASS store byte 0 at wcStruc+(%-1)    

But again, this brings nothing, but a (potentially) increased file size.

Thank you for the update. I'm happy to see this forum is a valuable source of knowledge for FASM/Assembler newbies like me.
Post 17 Jul 2016, 02:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.