News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

UNION - how good they are?

Started by frktons, January 26, 2013, 09:23:16 AM

Previous topic - Next topic

frktons

I'm thinking about using the UNION structure in order to solve some
assignment problems.

When we deal with qwords, for example, we can have the need to
address low or high dword, and use them as they were independent
variables.

I'm not sure, for the time being, what the correct sintax is, but as an
example I'd define:

Size64 UNION
  FileSize64    QWORD ?
  FileSizeH32   DWORD ?
  FileSizeL32   DWORD ?
Size64 ENDS

and later...


.data?

  SizeFile1  Size64 <>


and later...


.code
start:

  CALL  FileSizePROC

   .if  SizeFile1.FileSizeH32 == 0 ; OR can I use [eax].FileSizeH32 OR other way to address the var?

        print " The file is not bigger than 4 Gb",13,10

  .else

        print " File bigger than 4 Gb",13,10

  .endif

   inkey

   exit

end start


Is the syntax correct? What other use is possible with UNION structures?
Any practical example?

Thanks

Frank

There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

see if this works   :P
MYSTRUCT STRUCT
  Member0 dd ?
  Member1 dd ? ;example
  UNION
    STRUCT
      FileSizeLo DWORD ?
      FileSizeHi DWORD ?
    ENDS
    FileSize64 QWORD ?
  ENDS
ENDS

frktons

Quote from: dedndave on January 26, 2013, 09:31:34 AM
see if this works   :P
MYSTRUCT STRUCT
  UNION
      STRUCT
        FileSizeLo DWORD ?
        FileSizeHi DWORD ?
      ENDS
    FileSize64 QWORD ?
  ENDS
ENDS


The Low dword comes first?
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

sure thing - intel uses little endian

i know that ms reverses them in some structures
but, that would be the way to do it if you want to use it as an atomic QWORD

dedndave

oh - you can also do it this way.....
FileSize64 LABEL QWORD
FileSizeLo DWORD ?
FileSizeHi DWORD ?


look, ma ! no structure   :biggrin:

dedndave

but, you can't use LABEL inside a structure

you can always use size override operators, though
FileSize64 QWORD ?

    mov     eax,dword ptr FileSize64    ;lo dword
    mov     edx,dword ptr FileSize64+4  ;hi dword


or, not as neat...
FileSizeLo DWORD ?
FileSizeHi DWORD ?

and use "qword ptr FileSizeLo" when you want to reference it as a qword

frktons

Quote from: dedndave on January 26, 2013, 09:39:57 AM
oh - you can also do it this way.....
FileSize64 LABEL QWORD
FileSizeLo DWORD ?
FileSizeHi DWORD ?


look, ma ! no structure   :biggrin:

Well, I see you read the e-book I gave you.  :lol: Yes this version is probably the one I like most.  :t

The example #1 in practice:

;==============================================================================
; Test_Union.asm
; -----------------------------------------------------------------------------
; Test to verify Union structure.
;
; Frktons 26-jan-2013 @Masm32 Forum
;==============================================================================
include \masm32\include\masm32rt.inc
;==============================================================================
.nolist
.686
.xmm

Size64 UNION
  FileSize64    QWORD ?
  STRUCT
     FileSizeL32   DWORD ?
     FileSizeH32   DWORD ?
  ENDS   
Size64 ENDS


.data?

  SizeFile1  Size64 <>

;==============================================================================
.data

  align 4
  Area64  DD  0A0B12C7H,00000000H,0



.code

;==============================================================================
Main proc

  align 4
  lea eax, SizeFile1.FileSize64
  lea ebx, Area64

  movq mm0, qword ptr  [ebx]
  movq qword ptr [eax], mm0

   .if  SizeFile1.FileSizeH32 == 0 

        print " The file is not bigger than 4 Gb",13,10
        print " High dword contains "
        print str$(SizeFile1.FileSizeH32),13,10
        print " Low  dword contains "
        print str$(SizeFile1.FileSizeL32),13,10,13,10       

  .else

        print " File bigger than 4 Gb",13,10
        print " High dword contains "
        print str$(SizeFile1.FileSizeH32),13,10
        print " Low  dword contains "
        print str$(SizeFile1.FileSizeL32),13,10,13,10         

  .endif


  ret

Main endp

;==============================================================================
start:
;==============================================================================
  call Main

  inkey
  exit
;==============================================================================
end start



Quote
The file is not bigger than 4 Gb
High dword contains 0
Low  dword contains 168497863

Press any key to continue ...
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

i used that method a lot in the old days for words/bytes   :P

dedndave

if you use the LABEL
FileSize64 LABEL QWORD
FileSizeLo DWORD ?
FileSizeHi DWORD ?
;
;
;
    .if FileSizeHi
        ;print error message
    .endif


frankly, i would probably never store the hi dword if i wasn't going to support large files

frktons

Quote from: dedndave on January 26, 2013, 09:56:14 AM
if you use the LABEL
FileSize64 LABEL QWORD
FileSizeLo DWORD ?
FileSizeHi DWORD ?
;
;
;
    .if FileSizeHi
        ;print error message
    .endif


frankly, i would probably never store the hi dword if i wasn't going to support large files

Yes Dave, just in case you need to know how big a file is before accepting to process it.

There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama