2

I've read the documentation of Fasm, but I can't figure out this. In Nasm I'd first declare a struct in ".bss" and then define it in ".data":

    section ".bss"

    struc my_struct
        .a resw 1
        .b resw 1
        .c resb 1
        .d resb 1
    endstruc

    section ".data"

    my_struct_var1 istruc my_struct
        at my_struct.a, dw 123
        at my_struct.b dw, 0x123
        at my_struct.c db, "fdsfds"
        at my_struct.d db 2222
    endstruc

How can I do this in FASM exactly?

; declaring

struct my_struct
    .a rw 1
    .b rw 1
    .c rb 1
    .d rb 1
ends

; or maybe this way?
; what's the difference between these 2?

struct my_struct
    .a dw ?
    .b dw ?
    .c db ?
    .d db ?
ends

1) Firstly, is that correct? Or should I use the macros "sturc { ... }" If so, how exactly?

2) Second, how can I initialize it in ".data"?

3) also there's a question in my code

Note it's an application for Linux 64

Frank C.
  • 7,758
  • 4
  • 35
  • 45
Torito
  • 305
  • 1
  • 11
  • 1
    I would recommend the FASM mesage board https://board.flatassembler.net for a possibly better answer – Slai Jan 30 '17 at 04:44
  • I don't know nasm, but usually "rb/rw/rd" just "reserves" a byte/word/doubleword, not touching it at all (uninitialized). the same does "db ?/dw ?/dd ?". To initialize it, you have to use "db/dw/dd value", e.g. `dw 2000` (word with VALUE 2000) or `db 20` (byte 20). `rw 2000` would reserve a block of 2000 words – Tommylee2k Jan 30 '17 at 08:24
  • @Torito I'm not familliar with all assmblers, so I don't generalize. I can only assume it's the same for all ( most?) of them – Tommylee2k Jan 30 '17 at 08:35
  • @Tommylee2k, I think you're right (wrong?). – Torito Jan 30 '17 at 08:38

1 Answers1

2

The struc in FASM is almost the same as macro, only named with a label at the front.

The struct is actually a macro, that makes definitions easier.

If you are using FASM include files where struct macro is defined, following code will allow you to initialize structures:

; declaring (notice the missing dots in the field names!)

struct my_struct
    a dw ?
    b dw ?
    c db ?
    d db ?
ends

; using:

MyData my_struct 123, 123h, 1, 2

You can read more about FASM struct macro implementation in the Windows programming headers manual.

If you prefer to not use the FASM struct macro, you still can define initialized structures using the native FASM syntax, following way:

; definition (notice the dots in the field names!)

struc my_struct a, b, c, d {
    .a dw a
    .b dw b
    .c db c
    .d db d
}

; in order to be able to use the structure offsets in indirect addressing as in:
; mov  al, [esi+mystruct.c]

virtual at 0
  my_struct my_struct ?, ?, ?, ?
end virtual

; using:

MyData my_struct 1, 2, 3, 4
johnfound
  • 6,857
  • 4
  • 31
  • 60