flat assembler
Message board for the users of flat assembler.

Index > Windows > How to handle bit fields in FASM structs?

Author
Thread Post new topic Reply to topic
fatygant



Joined: 12 Sep 2011
Posts: 30
Location: Poznan, Poland
fatygant 08 Jun 2016, 08:51
Hi there!

I am working on a little Modbus utility which uses COM ports. I have to use the functions which use Win32 DCB structure which is defined like this:
Code:
typedef struct _DCB {
  DWORD DCBlength;
  DWORD BaudRate;
  DWORD fBinary  :1;
  DWORD fParity  :1;
  DWORD fOutxCtsFlow  :1;
  DWORD fOutxDsrFlow  :1;
  DWORD fDtrControl  :2;
  DWORD fDsrSensitivity  :1;
  DWORD fTXContinueOnXoff  :1;
  DWORD fOutX  :1;
  DWORD fInX  :1;
  DWORD fErrorChar  :1;
  DWORD fNull  :1;
  DWORD fRtsControl  :2;
  DWORD fAbortOnError  :1;
  DWORD fDummy2  :17;
  WORD  wReserved;
  WORD  XonLim;
  WORD  XoffLim;
  BYTE  ByteSize;
  BYTE  Parity;
  BYTE  StopBits;
  char  XonChar;
  char  XoffChar;
  char  ErrorChar;
  char  EofChar;
  char  EvtChar;
  WORD  wReserved1;
} DCB, *LPDCB;
    

I tried to translate it to FASM and created this:
Code:
struct DCB
  DCBlength dd
  BaudRate  dd
  union
    fBinary           dd
    fParity           dd
    fOutxCtsFlow      dd
    fOutxDsrFlow      dd
    fDtrControl       dd
    fDsrSensitivity   dd
    fTXContinueOnXoff dd
    fOutX             dd
    fInX              dd
    fErrorChar        dd
    fNull             dd
    fRtsControl       dd
    fAbortOnError     dd
    fDummy2           dd
  ends
  wReserved  dw
  XonLim     dw
  XoffLim    dw
  ByteSize   db
  Parity     db
  StopBits   db
  XonChar    db
  XoffChar   db
  ErrorChar  db
  EofChar    db
  EvtChar    db
  wReserved1 dw
ends
    

And I guess this is correct when it comes to the structure size and its fields offsets. But how to handle (in easy way) the bits packed in DWORD union - do I need to use some bit logic operators every time I want to set/reset/check the flag?

Thank you for your help!
Post 08 Jun 2016, 08:51
View user's profile Send private message Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 257
Trinitek 08 Jun 2016, 09:59
You can use the bit test instructions.

bts [example], 3 will set the fourth bit, for example.
Post 08 Jun 2016, 09:59
View user's profile Send private message Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 257
Trinitek 08 Jun 2016, 10:15
I dug through a project and found my implementation for creating enums and bitfields.

The macros...
Code:
macro enum eName*, [members*] {
    common
        local i
        i = 0
    forward
        eName#.#members = i
        i = i + 1
}

macro bitfield bName*, [members*] {
    common
        enum bName#.bit, members
        local i
        i = 1
    forward
        bName#.#members = i
        i = i * 2
}    
Defining a bitfield...
Code:
bitfield r,\
    HMIN,\
    HMAX,\
    VMIN,\
    VMAX,\
    VPADDLELEFT,\
    VPADDLERIGHT,\
    HPADDLE    
Using the bitfield labels to test against the variable containing the union... Note that the AX register contains the union.
Code:
; The macros allow you to perform bitwise operations using the traditional OR, AND, TEST, etc. instructions, like so...
test ax, r.HMIN or r.HMAX
jz .horizontalPaddle        ; are at least one of the horizontal bits set?

; And the n-th bit offset is accessible for use with the 386 bit test instructions, like so...
bt ax, r.bit.HPADDLE        ; is the horizontal paddle collision bit set?
jnc .verticalPaddle    
Post 08 Jun 2016, 10:15
View user's profile Send private message Reply with quote
fatygant



Joined: 12 Sep 2011
Posts: 30
Location: Poznan, Poland
fatygant 08 Jun 2016, 13:12
Hi Trinitek,

So, if I get it right (FASM macros are still the topic I have not handled yet...), first I should define the bitfield:

bitfield dcbFLAGS, fBinary,fParity,fOutxCtsFlow,fOutxDsrFlow,fDtrControl,fDsrSensitivity,fTXContinueOnXoff,fOutX,fInX,fErrorChar,fNull,fRtsControl,fAbortOnError,fDummy2

and then use it when defining DCB structure? Like this:
Code:
struct DCB
  DCBlength dd 
  BaudRate  dd
  flags dcbFLAGS
  wReserved dw
  ...
ends
    

And what about fDtrControl (and fRtsControl) which takes 2 bits? Should I create two enums like fDtrControl1 and fDtrControl2?

Thank you for your input - it helped a lot.
Post 08 Jun 2016, 13:12
View user's profile Send private message Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 257
Trinitek 08 Jun 2016, 16:28
Your bitfield definition is good, but in your struct, you would still define 'flags' as a dd type. You need to try to remember that the symbols created by my bitfield macro are just a collection of assembly-time equates that are assigned to numbers. It does not create a new type. Here's another example using your struct...
Code:
; Let's set the fParity flag
bts [DCB.flags], dcbFLAGS.bit.fParity    


Last edited by Trinitek on 08 Jun 2016, 16:59; edited 1 time in total
Post 08 Jun 2016, 16:28
View user's profile Send private message Reply with quote
fatygant



Joined: 12 Sep 2011
Posts: 30
Location: Poznan, Poland
fatygant 08 Jun 2016, 16:57
Trinitek,

somethink like this works perfectly well:
Code:
macro enum name*,[stuff] {
  common
        local x
        x=0
        struc name \{. dd ?\}
  forward
        x=x+1
        stuff=x }
    

and then
Code:
enum FLOWCONTROL,NoFlowControl,CtsRtsFlowControl,CtsDtrFlowControl,DsrRtsFlowControl,DsrDtrFlowControl,XonXoffFlowControl

struct CONFIG
        mask            dd ?,?
        pszPortName     dq ?
        cchTextMax      dd ?
        dwBaudRate      dd ?
        bParity         db ?
        bDataBits       db ?
        bStopBits       db ?
        fDiscardNull    db ?
        flowControl     FLOWCONTROL
ends
    


but indeed - my enum macro is different than yours.

And what do you mean when saying: there are no structs in FASM??
Post 08 Jun 2016, 16:57
View user's profile Send private message Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 257
Trinitek 08 Jun 2016, 17:06
Completely forgot that structs are provided by struct.inc. I don't use it often. I went to correct myself but you beat me to it.
Post 08 Jun 2016, 17:06
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.