flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Reserving strucs, then accessing fields using struc pointer

Author
Thread Post new topic Reply to topic
VileR



Joined: 29 Nov 2018
Posts: 7
Location: B800:0000
VileR 26 Jan 2019, 17:38
I turned up many questions/answers about this general topic, but unfortunately they haven't really helped clear up what I'd like to achieve.

Let's say I need to reserve space for a few structures, all of the same type (which includes fields specified w/local labels). I also have a near pointer to specify which of those structures I'll be accessing. Once this pointer contains the address of a structure, how can the individual fields be accessed by their labels (instead of specifying their numeric offsets)?

From the docs I understand that this requires "virtual", but also, that "virtual" doesn't actually allocate space for my structure. So how would this be done? and does it require a separate "virtual" block for every structure declared?

Context: a simple 16-bit program in fasm1 for DOS.

Here's an abridged/simplified version of what I'm trying to do:

Code:
;------ Structure definition -----

  struc dude
  {
    .height:     db ?
    .weight:     db ?
    .shortname:  times 12   db ?
    .longname:   times 128  db ?
    .portrait:   times 8192 db ?
  }

;-------- Code ------

  ; [...]
  mov ax,mister_x
  mov [curr_dude_ptr],ax
  ; [...]
  mov di,[curr_dude_ptr]    ; Retrieving a pointer to some "dude" struc
  ; [...]                   ; Now, how are fields accessed by their labels?

;------- Uninitialized data ("BSS"): -------

  curr_dude_ptr: dw ?       ; Near pointer to some dude
  mister_x       dude       ; This space should be reserved, but specific
  mister_y       dude       ;      fields should be accessible from outside
  more_data:                ; ...     
[/i]
Post 26 Jan 2019, 17:38
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 26 Jan 2019, 17:48
The general approach looks like this:
Code:
;------ Structure definition -----

  struc dude
  {
    .height:     db ?
    .weight:     db ?
    .shortname:  times 12   db ?
    .longname:   times 128  db ?
    .portrait:   times 8192 db ?
  }

  virtual at 0
        dude    dude
  end virtual

;-------- Code ------

  ; [...]
  mov ax,mister_x
  mov [curr_dude_ptr],ax
  ; [...]
  mov di,[curr_dude_ptr]    ; Retrieving a pointer to some "dude" struc
                            ; Now, how are fields accessed by their labels?
  mov cl,[di+dude.height]   ; Like this, for example

  ; [...]

;------- Uninitialized data ("BSS"): -------

  curr_dude_ptr: dw ?       ; Near pointer to some dude
  mister_x       dude       ; This space should be reserved, but specific
  mister_y       dude       ;      fields should be accessible from outside
  more_data:                ; ...    
But you also have an option of defining labels for specific use:
Code:
  mov di,[curr_dude_ptr]

  virtual at di
        di_dude    dude
  end virtual

  mov cl,[di_dude.height]    
Also, with "virtual at 0" approach you can easily add a size definition and then use it when reserving data:
Code:
  struc dude
  {
    .height:     db ?
    .weight:     db ?
    .shortname:  times 12   db ?
    .longname:   times 128  db ?
    .portrait:   times 8192 db ?
  }

  virtual at 0
        dude    dude
        sizeof.dude = $
  end virtual

  ; [...]

  array rb 100*sizeof.dude      ; reserve space for 100 structures    
Post 26 Jan 2019, 17:48
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20459
Location: In your JS exploiting you and your system
revolution 26 Jan 2019, 17:58
Tomasz Grysztar wrote:
Also, with "virtual at 0" approach you can easily add a size definition
It doesn't have to be at 0 either
Code:
virtual at some_offset
 dude  dude
 sizeof.dude = $ - $$
end virtual    
Post 26 Jan 2019, 17:58
View user's profile Send private message Visit poster's website Reply with quote
VileR



Joined: 29 Nov 2018
Posts: 7
Location: B800:0000
VileR 26 Jan 2019, 20:22
Very nice - works for me, thank you both.
Post 26 Jan 2019, 20:22
View user's profile Send private message Visit poster's website 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.