Author Topic: STRUCts and dereferencing them from a pointer  (Read 138 times)

Offline waltje

  • Jr. Member
  • *
  • Posts: 2
STRUCts and dereferencing them from a pointer
« on: December 24, 2020, 08:16:31 PM »
Hi all,

Just finished porting several projects from Fasm to Nasm, for several reasons.
These are realmode (BIOS and DOS) projects, so all is 16bit here.

Only thing left to do is fixing the structure accessing.

Case:

We have a struct, defined as

struc foo
  .bar  db ?
  .bla  db 20 dup(?)
  .erm dw ?
endstruc

Now, we get called from another task, and they pass us a
pointer to 'foo' in es:bx.

The code then wants to do

    mov  al, es:[bx].bar

but it chokes, as it doesnt know what to do with the '.bar' tag.

What is the proper syntax to "cast" the es:bx pointer to be of
type 'foo' ?  I also tried

    mov al, es:[bx]foo.bar

but still nada...

Thankyou,

Fred

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2473
  • Country: us
Re: STRUCts and dereferencing them from a pointer
« Reply #1 on: December 24, 2020, 09:49:34 PM »
Hi Fred.

Welcome to the forum.

Nasm likes everything inside the square brackets...
Code: [Select]
mov al, [es: bx + foo.bar]
... should work.

Best,
Frank

P. S. Nasm doesn't do "dup". "times 20 0"
« Last Edit: December 24, 2020, 09:52:25 PM by Frank Kotler »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 128
  • Country: br
Re: STRUCts and dereferencing them from a pointer
« Reply #2 on: December 24, 2020, 10:58:29 PM »
P. S. Nasm doesn't do "dup". "times 20 0"

Or using res? like:

Code: [Select]
struc foo
.bar:  resb 1
.bla:  resb 20
.erm: resw 1
endstruc

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2473
  • Country: us
Re: STRUCts and dereferencing them from a pointer
« Reply #3 on: December 24, 2020, 11:08:25 PM »
Yeah that would be better. But not "dup".

Best,
Frank


Offline waltje

  • Jr. Member
  • *
  • Posts: 2
Re: STRUCts and dereferencing them from a pointer
« Reply #4 on: December 25, 2020, 12:39:46 AM »
> Yeah that would be better. But not "dup".
From the manual (and my code ;-) :

"Since NASM 2.15, the MASM syntax of using ? and DUP in the Dx directives is also supported. Thus, the above example could also be written:"

At any rate.... learned that the displacement has to come BEFRORE the pointer,
as in:

      mov al, es:foo.bar[bx]

and that indeed works. Yay!

That said, it would be more syntax-consistent to have:

    mov al, foo ptr es:[bx].bar

akin to using 'word ptr' and the like, which pretty much
tell the assemble about the 'type' of the operand ?

Fred

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2473
  • Country: us
Re: STRUCts and dereferencing them from a pointer
« Reply #5 on: December 25, 2020, 12:51:15 AM »
I stand corrected.

Best,
Frank