News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Pointer to structure or class memeber

Started by kohlyang, June 27, 2010, 09:20:03 AM

Previous topic - Next topic

kohlyang

in masm32, we can do like:

item struct
   year dword ?
   month dword ?
   day dword ?
   next dword ?
item ends

addItem proc year:dword, month:dword, day:dword
        local cell: item
       
        m2m cell.year, year
   m2m cell.month, month
   m2m cell.day, day
   m2m cell.next, 0
...
        ret
addItem endp

but what about if under this condition:
local p: dword
invoke crt_malloc, sizeof(item)
mov p, eax
m2m p->year, year
m2m p->month, month

at this time, p is a pointer, and in C we can use ->, but what about in asm. as i know will meet error if call "m2m p->year, year"

can anybody help on this one, i'm struggling to write code which can be similar to C code, thanks very much

i know this can be done like:
mov eax, p
mov [eax], year
mov 4[eax], month
but if do like that, will add inconvenience to read the code

jj2007

An alloc for "local" purposes looks a bit odd. Why not something like this?

addItem proc year:dword, month:dword, day:dword
local LocItem:item
  m2m LocItem.year, year
...
  DoSomethingUsefulWithItem
  ret

kohlyang

hi, jj2007, thanks

what i really want is to assign value to a structure's member, but now can only get the pointer to that structure, so i want to follow the way in C language, such as p->year=year, ...

but seems there's no such a way in masm32.

currently, i assign value to local variable, then crt_memcpy to the pointer, although can work, but feel ugly,
below is the code how to add an item to the list (which will be sorted)


addItem proc year:dword, month:dword, day:dword
   local p: dword
   local cell: item
   local one: item
   local t1: dword
   local t2: dword
   
   m2m cell.year, year
   m2m cell.month, month
   m2m cell.day, day
   m2m cell.next, 0
   invoke crt_malloc, sizeof item
   mov p, eax
   invoke crt_memcpy, p, addr cell, sizeof cell
   
   .if head == 0
      m2m head, p
   .else
      m2m t1, head
      m2m t2, t1
      .while TRUE
         invoke crt_memcpy, addr one, t1, sizeof item
         cmpvalue one.year, cell.year
         .if sdword ptr eax<0
            .break
         .else
            cmpvalue one.month, cell.month
            .if sdword ptr eax<0
               .break
            .else
               cmpvalue one.day, cell.day
               .if sdword ptr eax<0
                  .break;
               .endif
            .endif
         .endif
         m2m t2, t1
         m2m t1, one.next
         .if one.next == 0
            .break
         .endif
      .endw
      
      cmpvalue t1, head
      .if eax == 0
         ;m2m 12[p], head
         mov eax, p
         m2m 12[eax], head
         m2m head, p
      .else
         ;m2m 12[t2], p
         ;m2m 12[p], t1
         mov eax, t2
         m2m 12[eax], p
         mov eax, p
         m2m 12[eax], t1
      .endif
   .endif
   
   ret
addItem endp


greatly appreciate if can provider me easy ways to add item into a sorted list.

box

Read 'Working with Structures' in \masm32\help\asmintro.chm


    m2m (item ptr p).year, cell.year
    m2m (item ptr p).month, cell.month
    m2m (item ptr p).day, cell.day
    m2m (item ptr p).next, cell.next

; - OR -
    mov eax, p
;    add eax, sizeof(item)*index
    ASSUME eax:PTR item

    m2m [eax].year, cell.year
    m2m [eax].month, cell.month
    m2m [eax].day, cell.day
    m2m [eax].next, cell.next

    ASSUME eax:nothing


You might also benefit from reading "Memory Functions" in \masm32\help\masmlib.chm
and "Memory Allocation" under "Macro Categories" in \masm32\help\hlhelp.chm

jj2007

Quote from: kohlyang on June 27, 2010, 11:30:59 AM
but seems there's no such a way in masm32.
It would really surprise me if you could do something in C that you cannot do in assembler. After all, C produces assembler, not exe...

Another syntax variant (also applicable to local structures):
include \masm32\include\masm32rt.inc

item struct
   year dword ?
   month dword ?
   day dword ?
   next dword ?
item ends

.data?
MyItem item <?>

.code
start: mov edi, offset MyItem
mov [edi.item.year], 2010
mov [edi.item.month], 06
mov [edi.item.day], 27
exit
end start

Tedd

There isn't really a 'pretty' way to do it in assembler (macros could help, maybe), as in using the -> operator, but you do it in exactly the same way as the compiler produces its code: first get the pointer, then offset it to whichever structure member you want -- there isn't a single instruction to do this.
So yes it is 'ugly' but you're working at the low-level, everything is ugly :P
No snowflake in an avalanche feels responsible.

dedndave

Quotemov [eax], year
mov 4[eax], month
but if do like that, will add inconvenience to read the code

i don't see anything wrong with it
it may not look like C - but, that's because it isn't C   :P
you might "pretty it up" a little like this
yearOfs  equ 0
monthOfs equ 4
.
.
        mov     yearOfs[eax], year
        mov     monthOfs[eax],month

or even
yearOfs  textequ <[eax]>
monthOfs textequ <[eax+4]>
.
.
        mov     yearOfs, year
        mov     monthOfs,month


to me, [eax] and [eax+4] seem perfectly natural
but then, i was never brainwashed by all those C programming books   :lol

jj2007

Quote from: dedndave on June 28, 2010, 11:33:52 AM
you might "pretty it up" a little like this

There are indeed many ways to make it look "prettier". And most of them will make it indeed prettier than its C equivalent. Personally, I find m2m p->year, year pretty ugly :toothy

Quoteinclude \masm32\include\masm32rt.inc

item struct
   year dword ?
   month dword ?
   day dword ?
   next dword ?
item ends

.data?
MyItem   item <?>

.code
start:   mov edi, offset MyItem
   TheItem equ [edi.item]
   mov TheItem.year, 2010  ; "pretty" version
   mov [edi.item].month, 06   ; classical assembler
   mov TheItem.day, 27
   exit
end start

kohlyang

 :bg :thumbu

many thanks for all of the help for brothers,

i haven't verified all of these, but it's really very helpful,  :clap: :bg, and i think i know now how to deal with the problems, thanks.