News:

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

problem about structure indirect reference

Started by qklxtlx, July 22, 2010, 09:48:47 AM

Previous topic - Next topic

qklxtlx

Now I have a structureplayerstate struct
....
playerstate ends
player1 playerstate<>
player2 playerstate<>

I want to use "player" to represent the two symbols.
like:
lea player, player1
...(like mov player.X, 200)
lea player, player2
...(do the same thing~mov player.X, 200)~~

But I cannot come up with the code that can run ::)
can any help?
PS:(playerstate PTR player).X works but I write a lot of .IF and this cannot work in directives~~~

oex

I think this should work....

player1 equ <playerstate>
player2 equ <playerstate>

Oh maybe you mean

lea esi, player1
[esi].playerstate.xxx
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

ecube

or

mystruct struct
val1 dd ?
val2 dd ?
mbuff db 512 dup (?)
mystruct ends

mstruct mystruct <>

lea edi,mstruct
assume edi:ptr mystruct
mov [edi].val1,5

mov ecx,[edi].val2

lea esi,[edi].mbuff

assume edi:NOTHING
etc...

qklxtlx

Quote from: oex on July 22, 2010, 09:51:00 AM
I think this should work....

player1 equ <playerstate>
player2 equ <playerstate>
to oex:
sorry but I cannot understand clearly~~~
afterplayer1 equ <playerstate>
player2 equ <playerstate>

how to use "player"  then? like player = player1; player.X = ...

also, lea esi, player1
[esi].playerstate.xxx
cannot work...
Sorry about being new in this field

qklxtlx

Quote from: E^cube on July 22, 2010, 10:04:49 AM
or

mystruct struct
val1 dd ?
val2 dd ?
mbuff db 512 dup (?)
mystruct ends

mstruct mystruct <>

lea edi,mstruct
assume edi:ptr mystruct
mov [edi].val1,5

mov ecx,[edi].val2

lea esi,[edi].mbuff

assume edi:NOTHING
etc...


Thanks , E^cube~~~
I have tried "assume" before and this works .But it is not fit for me.
My code was not so good because I found that I coded only one "player" in it.(I want to make a ftg for fun)  :(
Because "two players" needs almost the same procedure so I just want to use "player" to represent the different two players~~~

registers are used so I don't think assume esi is fit for me here~~~

BogdanOntanu



PLAYER STRUC
pos_x dd ?
pos_y dd ?
state dd ?
PLAYER ENDS

NR_PLAYERS EQU 8
PLAYER_STATE_ALIVE EQU 1

.data?

players PLAYER NR_PLAYERS dup <?>

.code

; assume that you have initialized the players array above

; move all players
mov ecx, NR_PLAYERS
mov esi,offset players
loop_players:
 ; move only alive players
.if [esi + PLAYER.state] == PLAYER_STATE_ALIVE
  add [esi +PLAYER.x],8 ; move player 8 pixels to the right
  add [esi+PLAYER.y],4 ;move player 4 pixels down
.endif
; next player
add esi, size PLAYER
dec ecx
jnz loop_players
...


You must use a register for indirect addressing and moving around in an players array. Otherwise you would have to copy data around and that is not a decent option. A memory variable can NOT be used for referencing like a register.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

qklxtlx

thanks~BogdanOntanu...
I am replacing my code now...awful

oex

yep sorry very long night I had meant to show you assume as e^cube did....

I tend to prefer to use (x PTR [reg]).xxx as you have been and have the same problem with conditionals....

I use:

mov reg, (x PTR [reg]).xxx
.if reg == 123
.elseif reg = 124
etc
.endif

When I need conditionals....

It looks like Bogdan has a better method using register pointers I shall need to update my code also it seems :lol
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

qklxtlx

I have changed my code with BogdanOntanu's method and works well!!!
Thank you all~~~
:8) :8) :8)
(I think the way of writing [esi+structure offset] is wonderful~~~)