NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: waltje on December 24, 2020, 08:16:31 PM

Title: STRUCts and dereferencing them from a pointer
Post by: waltje 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
Title: Re: STRUCts and dereferencing them from a pointer
Post by: Frank Kotler 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"
Title: Re: STRUCts and dereferencing them from a pointer
Post by: fredericopissarra 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
Title: Re: STRUCts and dereferencing them from a pointer
Post by: Frank Kotler on December 24, 2020, 11:08:25 PM
Yeah that would be better. But not "dup".

Best,
Frank

Title: Re: STRUCts and dereferencing them from a pointer
Post by: waltje 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
Title: Re: STRUCts and dereferencing them from a pointer
Post by: Frank Kotler on December 25, 2020, 12:51:15 AM
I stand corrected.

Best,
Frank