The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ragdog on February 28, 2010, 05:11:06 PM

Title: C++ to asm question
Post by: ragdog on February 28, 2010, 05:11:06 PM
Hi

I have a little question to translate from c++ to asm

xxx_xxx buffer;

xxx_xxx is a structur
Title: Re: C++ to asm question
Post by: qWord on February 28, 2010, 05:28:07 PM
some struct
foo dd ?
some ends

.data
WhatEver some <>
.code
Title: Re: C++ to asm question
Post by: ragdog on February 28, 2010, 05:53:20 PM
Ahh ok thanks :U
Title: Re: C++ to asm question
Post by: ragdog on March 01, 2010, 08:34:35 PM
Hi

I have now a question to this struct
what is Record? and can any tranlate this correct please

CDROM_READ_TOC_EX   struct
CDROM_READ_TOC_EX_R0   RECORD   Format:4,Reserved1:3,Msf:1
   CDROM_READ_TOC_EX_R0 <>
SessionTrack   UCHAR   ?
Reserved2   UCHAR   ?
Reserved3   UCHAR   ?
CDROM_READ_TOC_EX   ends

;typedef struct _CDROM_READ_TOC_EX {
;    UCHAR Format    : 4;
;    UCHAR Reserved1 : 3; // future expansion
;    UCHAR Msf       : 1;
;    UCHAR SessionTrack;
;    UCHAR Reserved2;     // future expansion
;    UCHAR Reserved3;     // future expansion
;} CDROM_READ_TOC_EX, *PCDROM_READ_TOC_EX;

c++ code

  tocex.Format = CDROM_READ_TOC_EX_FORMAT_TOC;
    tocex.Msf = 1;

If this correct?

mov tocex.Format,CDROM_READ_TOC_EX_FORMAT_TOC;
mov tocex.Msf,1

Thanks
Title: Re: C++ to asm question
Post by: jj2007 on March 01, 2010, 08:43:48 PM
http://www.masm32.com/board/index.php?topic=13394.0
Title: Re: C++ to asm question
Post by: redskull on March 01, 2010, 08:46:04 PM
Be aware, MSVC and ML have opposite endians styles for bitrecords.  Define it in MASM in the opposite order as C

-r
Title: Re: C++ to asm question
Post by: ragdog on March 01, 2010, 09:14:44 PM
Hi Thanks for you answere

I have look it on this link

I cannot mov in this structur why?

Here is my result


.data

tocex CDROM_READ_TOC_EX <>

.code

       lea edi,tocex
        assume edi: ptr CDROM_READ_TOC_EX
       
          mov  [edi].CDROM_READ_TOC_EX_R0.Format,CDROM_READ_TOC_EX_FORMAT_TOC
          mov  [edi].CDROM_READ_TOC_EX_R0.Mfs,1
          ;tocex.Format = CDROM_READ_TOC_EX_FORMAT_TOC;
          ;tocex.Msf = 1;
        assume edi: NOTHING
Title: Re: C++ to asm question
Post by: redskull on March 01, 2010, 09:22:26 PM
MOV works on bytes only (and words and dwords), nothing smaller (it's helpful to think of the structure name resolving down to just a single address).  You either AND with a 0 to clear a bit, or OR with a 1 to set a bit.  MASM has keyworks to automatically create said bitmasks, using MASK.  Check the programmers guide

-r
Title: Re: C++ to asm question
Post by: dedndave on March 01, 2010, 09:27:04 PM
you have already told it that EDI is a structure pointer
try this

        assume edi: ptr CDROM_READ_TOC_EX
       
          mov  [edi].Format,CDROM_READ_TOC_EX_FORMAT_TOC ;not sure - this must be an equate ?
          mov  [edi].Mfs,1
Title: Re: C++ to asm question
Post by: ragdog on March 01, 2010, 09:53:10 PM
@dedndave

I have already try it this give me the same result

error A2006: undefined symbol
error A2006: undefined symbol : Mfs

@RedSkull
How i can make this?
(it's helpful to think of the structure name resolving down to just a single address)
Title: Re: C++ to asm question
Post by: jj2007 on March 01, 2010, 09:54:13 PM
Full example attached.
.code
start:
SetField tocex.CDROM_READ_TOC_EX.Format, CDROM_READ_TOC_EX_FORMAT_TOC
SetField tocex.CDROM_READ_TOC_EX.Msf, 1


EDIT: More size-efficient version of SetField added (v2).
Title: Re: C++ to asm question
Post by: ragdog on March 01, 2010, 10:08:48 PM
Ahh this mean you  :wink

Thanks it works

Why if this in c++ a little line in masm a little mor code  :bg
Title: Re: C++ to asm question
Post by: ragdog on March 01, 2010, 10:13:20 PM
I have this Dissasembled

this macro make this

.text:004010DE                 lea     edi, dword_403000
.text:004010E4                 mov     eax, 0
.text:004010E9                 shl     eax, 4
.text:004010EC                 or      dword_403000, eax
.text:004010F2                 mov     eax, 1
.text:004010F7                 or      dword_403000, eax

This dword_403000 if CDROM_READ_TOC_EX?
0 =CDROM_READ_TOC_EX_FORMAT_TOC
1 = Msf
4 =Format


               lea     edi, tocex
                mov     eax, 0
                 shl     eax, 4
                 or      tocex, eax
                 mov     eax, 1
                 or    tocex, eax
if this crorrect?
Title: Re: C++ to asm question
Post by: dedndave on March 01, 2010, 10:29:42 PM
i see a possible typo
is it Mfs or Msf ?

Quoteerror A2006: undefined symbol : Mfs
Quote
          mov  [edi].Msf,1
Title: Re: C++ to asm question
Post by: ragdog on March 01, 2010, 11:20:19 PM
Is Msf!

Thanks i have i solved it with the bitfield macro

Now is my question to the dissasembled snipped in my last post
Title: Re: C++ to asm question
Post by: jj2007 on March 01, 2010, 11:35:54 PM
Yes, the disassembly is correct. Here is a variant:

start: mov edi, offset tocex
nop
SetField [edi.CDROM_READ_TOC_EX].Format, CDROM_READ_TOC_EX_FORMAT_TOC
nop
SetField tocex.CDROM_READ_TOC_EX.Msf, 1
nop
print str$(GetField(tocex.CDROM_READ_TOC_EX.Format, CDROM_READ_TOC_EX_FORMAT_TOC)), 9, "Format", 13, 10
print str$(GetField(tocex.CDROM_READ_TOC_EX.Msf, CDROM_READ_TOC_EX_FORMAT_TOC)), 9, "Msf", 13, 10
getkey
exit


If you have several fields to load, [edi.CD....] is slightly shorter. The code above disassembles as follows:
<ModuleEntryPo>/$  BF 00204000              mov edi, 00402000
00401005       |.  90                       nop
00401006       |.  B8 00000000              mov eax, 0
0040100B       |.  C1E0 04                  shl eax, 4
0040100E       |.  0907                     or [edi], eax
00401010       |.  90                       nop
00401011       |.  B8 01000000              mov eax, 1
00401016       |.  0905 00204000            or [402000], eax
0040101C       |.  90                       nop


The forceAnd parameter is needed if the field has already a value different from zero, and you want to overwrite it.
Title: Re: C++ to asm question
Post by: ragdog on March 01, 2010, 11:56:20 PM
Ahh ok this  is fine i have add this dissasembled code

I have this c++ code dissasembled and now have i the next problem
  pTrack = (PTRACK_DATA)(&toc.TrackData[0]);

00405473  |>   LEA EAX,DWORD PTR SS:[EBP-338]     
00405479  |.    ADD EAX,4                         
0040547C  |.    MOV DWORD PTR SS:[EBP-340],EAX   

xyz proc
LOCAL pTrack:PTRACK_DATA       ;    PTRACK_DATA  pTrack;

;pTrack = (PTRACK_DATA)(&toc.TrackData[0]);
lea     eax,pTrack
add     eax,4 ;toc.TrackData
mov     pTrack, eax


Is This Correct?


Title: Re: C++ to asm question
Post by: ragdog on March 04, 2010, 08:30:21 PM
Hi

I need help Once again

memset(&Xptr, 0, sizeof(Xptr));   << fill this the struct with zeros same as zeromemory?

And can any translate this please

  const int i_header_size = __MAX( 4, MINIMUM_SIZE );
   uint8_t header[i_header_size];

  const int i_text = 2 + (header[0] << 8) + header[1];

    /*-------------- */
    uint8_t *p_text = calloc( 1, i_text );

Thanks
Title: Re: C++ to asm question
Post by: qWord on March 04, 2010, 09:28:16 PM
Quote from: ragdog on March 04, 2010, 08:30:21 PMmemset(&Xptr, 0, sizeof(Xptr)); << fill this the struct with zeros same as zeromemory?
yes.

_MAX macro a,b
IF a GE b
EXITM <&a>
ELSE
EXITM <&b>
ENDIF
endm

MINIMUM_SIZE EQU ???
.const
; may not needed/used ?
;const int i_header_size = __MAX( 4, MINIMUM_SIZE );
i_header_size SDWORD _MAX(4,MINIMUM_SIZE)

;const int i_text = 2 + (header[0] << 8) + header[1];
; fill at runtime
i_text SDWORD ?
.data?
;uint8_t header[i_header_size];
header db _MAX(4,MINIMUM_SIZE) dup (?)
align 4
p_text dd ?
.code
...
; header[0,1] must be filled
...

;const int i_text = 2 + (header[0] << 8) + header[1];
movzx eax,header[0]
movzx ecx,header[1]
shl eax,8
lea eax,[eax+ecx+2]
mov i_text ,eax

;uint8_t *p_text = calloc( 1, i_text );
mov p_text,alloc(eax)
Title: Re: C++ to asm question
Post by: ragdog on March 04, 2010, 10:04:03 PM
Hi thanks

If i add this to my code crash this

movzx eax,header[0]
movzx ecx,header[1]
shl eax,8
lea eax,[eax+ecx+2]
mov i_text ,eax
Title: Re: C++ to asm question
Post by: qWord on March 04, 2010, 10:35:26 PM
ups... :red, you must declare i_text in .data section, not in .const