Author Topic: records  (Read 332 times)

nidud

  • Member
  • *****
  • Posts: 2311
    • https://github.com/nidud/asmc
records
« on: November 02, 2021, 12:36:48 AM »
Added anonymous records to structs.

COMSTAT     STRUC
RECORD
 fCtsHold   dd :  1 ?
 fDsrHold   dd :  1 ?
 fRlsdHold  dd :  1 ?
 fXoffHold  dd :  1 ?
 fXoffSent  dd :  1 ?
 fEof       dd :  1 ?
 fTxim      dd :  1 ?
 fReserved  dd : 25 ?
ENDS
cbInQue     dd ?
cbOutQue    dd ?
COMSTAT     ENDS
.data
xom COMSTAT { { 1, 1, 0, 0, 0, 0, 0, 0 }, 10, 11 }

Note that the record field names here are not global (Masm design flaw) and there are no packing as the size is predefined similar to C bitfields.

Example no how to extract and modify bitfields:

.template float
    RECORD
     mantissa dd : 24 ?
     exponent dd :  7 ?
     sign     dd :  1 ?
    ENDS
   .static get item:abs {
        mov eax,this
        and eax,.mask float.item
        shr eax,float.item
        }
   .static set item:abs, value:dword {
        mov eax,this
        and eax,not .mask float.item
        shl value,float.item
        or  eax,value
        mov this,eax
        }
   .ends
...
   .new f:float = -1.0

    f.get(sign)
*  mov eax,f
*  and eax,.mask float.sign (80000000)
*  shr eax,float.sign (31)

    f.set(sign, 0)
*  mov eax,f
*  and eax,not .mask float.sign (7FFFFFFF)
*  shl edx,float.sign (31)
*  or  eax,edx
*  mov f,eax


nidud

  • Member
  • *****
  • Posts: 2311
    • https://github.com/nidud/asmc
Re: records
« Reply #1 on: November 02, 2021, 09:05:33 PM »
The first field in the declaration always goes into the most significant bits of the record. Subsequent fields are placed to the right in the succeeding bits. If the fields do not total exactly 8, 16, or 32 bits as appropriate, the entire record is shifted right, so the last bit of the last field is the lowest bit of the record. Unused bits in the high end of the record are initialized to 0.

So this is not the case for embedded C-style records as the first field in the declaration always goes into the least significant bits of the record.

ARGB1   STRUC
blue    db ?
green   db ?
red     db ?
alpha   db ?
ARGB1   ENDS

ARGB2   STRUC
RECORD
 blue   dd:8 ?
 green  dd:8 ?
 red    dd:8 ?
 alpha  dd:8 ?
ENDS
ARGB2   ENDS

ARGB3   RECORD r_alpha:8, r_red:8, r_green:8, r_blue:8

%echo @CatStr(%ARGB1.red)
%echo @CatStr(%ARGB2.red/8)
%echo @CatStr(%r_red/8)

Combination of flags and bit-fields:

typedef struct PROCESS_MITIGATION_ASLR_POLICY {
    union {
        DWORD Flags;
        struct {
            DWORD EnableBottomUpRandomization : 1;
            DWORD EnableForceRelocateImages : 1;
            DWORD EnableHighEntropy : 1;
            DWORD DisallowStrippedImages : 1;
            DWORD ReservedFlags : 28;
        };
    };
};

PROCESS_MITIGATION_ASLR_POLICY  STRUC
RECORD Flags
 EnableBottomUpRandomization    dd :  1 ?
 EnableForceRelocateImages      dd :  1 ?
 EnableHighEntropy              dd :  1 ?
 DisallowStrippedImages         dd :  1 ?
 ReservedFlags                  dd : 28 ?
ENDS
PROCESS_MITIGATION_ASLR_POLICY  ENDS

Greenhorn

  • Member
  • **
  • Posts: 204
Re: records
« Reply #2 on: November 03, 2021, 05:56:46 AM »
Note that the record field names here are not global (Masm design flaw) and there are no packing as the size is predefined similar to C bitfields.


ARGB1   STRUC
blue    db ?
green   db ?
red     db ?
alpha   db ?
ARGB1   ENDS

ARGB2   STRUC
RECORD
 blue   dd:8 ?
 green  dd:8 ?
 red    dd:8 ?
 alpha  dd:8 ?
ENDS
ARGB2   ENDS


Great news  :thumbsup:
Thanks a lot for implementing this.

Kind regards
Greenhorn

JK

  • Member
  • **
  • Posts: 156
Re: records
« Reply #3 on: November 27, 2021, 02:25:28 AM »
How would i initialize a struct containing bit fields to zero?

while this works
Code: [Select]
mytype STRUCT
X BYTE ?
Y WORD ?
Z SDWORD ?
mytype ENDS

...
var mytype {}

this doesn´t
Code: [Select]
mytype STRUCT
X BYTE ?
Y WORD ?
Z SDWORD ?
  RECORD ;_recname
  b1 DB : 1 ?
  b2 DB : 2 ?
  b5 DB : 5 ?
ENDS
mytype ENDS

...
var mytype {}
i keep getting an error regardless, if the record gets a name or not

What is the correct syntax in such a case - thanks


JK

nidud

  • Member
  • *****
  • Posts: 2311
    • https://github.com/nidud/asmc
Re: records
« Reply #4 on: November 27, 2021, 04:29:44 AM »
It only worked (and tested) on expanded declarations:

var mytype { 0, 0, 0, { 1, 1, 1 } }

This is fixed in version 2.33.16

JK

  • Member
  • **
  • Posts: 156
Re: records
« Reply #5 on: November 27, 2021, 08:37:07 PM »
Thanks!  :thumbsup: