flat assembler
Message board for the users of flat assembler.

Index > Main > structures, pointers, ...

Author
Thread Post new topic Reply to topic
x0r



Joined: 12 Oct 2004
Posts: 2
x0r 18 Oct 2004, 10:33
Hi all,

Why this code won't compile ??

Code:
include '%fasminc%/win32ax.inc'

.data
struc tmp
{
  .x db ?
  .y db ?
}

fred tmp

.code

  start:
        mov [fred.x],'A'
        lea eax, [fred]
        mov [eax + tmp.y],'A' ;ERROR - Symbol undefined
        invoke  ExitProcess,0

.end start 
    


Another question:
How can I create a variable pointing to the struct tmp ? Like this in C:
Code:
struct test
{
  char a,b;
};

ptest=&test;
ptest->a='A';
    


Thanks for this great assembler (just a little confusing) Smile

Cool
Post 18 Oct 2004, 10:33
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 18 Oct 2004, 10:53
x0r wrote:
Why this code won't compile ??


Check for macroses struct...ends in FASM package includes. Actually in is included in win32ax.inc

Use it this way:

Code:
include '%fasminc%/win32ax.inc'

.data
struct tmp
  .x db ?
  .y db ?
ends

fred tmp
    


Another question:
How can I create a variable pointing to the struct tmp ? Like this in C:

You can't (of course) because structures are abstractions and pointer to it is not actually possible. On other hand, FASM doesn't use type checking and casts, so you simply don't need such features.
If you have instance of given structure, you can get pointer to it very easy, using the nama of the field:

Code:
struct test
  a db ?
  b db ?
ends

testinstance test ; variable of type "test"

mov  eax, testinstance.a  ; now eax contains pointer to testinstance.
lea  eax, [testinstance.b]  ; the same using lea instructions. btw, if testinstance is local variable of the procedure, this is the only way.

mov  [eax], 'A'  ; store character "A" in the testinstance.b

; Another way, more readable, especially for complex structures:

lea  eax, [testinstance]  ; pointer to the whole structure.
mov  [eax+test.b], "A"   ; offset to needed field.
    


Regards
Post 18 Oct 2004, 10:53
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7103
Location: Slovakia
vid 18 Oct 2004, 11:17
without macro features:

Code:
;defines struct
struct tmp
{
.x dd ?
.y dd ?
}

;defines virtual structure "tmp" named "tmp"
;so labels "tmp.x" and "tmp.y" get defined, tmp.x = 0, tmp.y = 4
virtual at 0
  tmp tmp
  sizeof.tmp = $ ;size of tmp
end virtual

;defines static structure
fred tmp

;defines pointer to structure
ptr_tmp dd 0 ;pointer is just normal dword like any other

;code
mov [tmp.x],0
mov eax,[ptr_tmp] ;load address of structure from pointer
mov [eax + tmp.x],5       ;eg. mov [eax + 0],5
mov [eax + tmp.y],10      ;eg. mov [eax + 4],10
    
Post 18 Oct 2004, 11:17
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
x0r



Joined: 12 Oct 2004
Posts: 2
x0r 18 Oct 2004, 12:30
Ok thanks for your help, my problem was on the structure declaration.

@+
Post 18 Oct 2004, 12:30
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.